Skip to content

Commit

Permalink
better...
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 31, 2025
1 parent 555b8b1 commit d5715e8
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 46 deletions.
8 changes: 5 additions & 3 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_VERSION_CLASSNAME_VERSION);
} else {
$valProp = $propertyType->isValueContainer()
? $propertyType->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)
: $propertyType->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME);
$valProp = match(true) {
$propertyType->isPrimitiveContainer() || $propertyType->isValueContainer() => $propertyType->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME),
$propertyType->hasPrimitiveContainerParent() || $propertyType->hasValueContainerParent() => $propertyType->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME),
default => null,
};

if (null !== $valProp) {
$valType = $valProp->getValueFHIRType();
Expand Down
13 changes: 8 additions & 5 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu

if ($type->isPrimitiveOrListType() || $type->hasPrimitiveOrListParent()) {
$hintTypes = $type->getPrimitiveType()->getPHPReceiveValueTypeHints();
} else if ($type->isValueContainer() || $type->hasValueContainerParent()) {
$valProp = $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME) ?? $type->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME);
} else if ($type->isValueContainer() || $type->hasValueContainerParent() || $type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) {
$valProp = match (true) {
$type->isValueContainer() || $type->isPrimitiveContainer() => $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME),
$type->hasValueContainerParent() || $type->hasPrimitiveContainerParent() => $type->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME),
default => null,
};
$ptp = $valProp->getValueFHIRType();
$hintTypes = [];
if ($ptp->isPrimitiveOrListType() || $ptp->hasPrimitiveOrListParent()) {
Expand Down Expand Up @@ -264,8 +268,7 @@ public static function buildSetterParameterDocHint(Version $version,

$hintTypes = self::buildBaseHintParts($version, $pt, true);


if ($pt->isValueContainer() || $pt->hasValueContainerParent()) {
if ($pt->isValueContainer() || $pt->hasValueContainerParent() || $pt->isPrimitiveContainer() || $pt->hasPrimitiveContainerParent()) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME) ?? $pt->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand Down Expand Up @@ -311,7 +314,7 @@ public static function buildSetterParameterHint(Version $version,
} else {
$hintTypes = self::buildBaseHintParts($version, $pt, false);

if ($pt->isValueContainer() || $pt->hasValueContainerParent()) {
if ($pt->isValueContainer() || $pt->hasValueContainerParent() || $pt->isPrimitiveContainer() || $pt->hasPrimitiveContainerParent()) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME) ?? $pt->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace DCarbone\PHPFHIR\Utils;
namespace DCarbone\PHPFHIR\Utilities;

/*
* Copyright 2025 Daniel Carbone ([email protected])
Expand Down Expand Up @@ -33,13 +33,12 @@ public static function determineDefaultLocation(Type $type, Property $property,
};
} else if ($property->isValueProperty()) {
$case = match (true) {
$type->isQuantity() || $type->hasQuantityParent() => 'CONTAINER_ATTRIBUTE',
$type->isValueContainer() || $type->hasValueContainerParent() => 'CONTAINER_ATTRIBUTE',
$type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent() => 'CONTAINER_ATTRIBUTE',
default => 'ELEMENT_ATTRIBUTE'
};
} else {
$case = match (true) {
$propType->isValueContainer() || $propType->hasValueContainerParent() => 'CONTAINER_ATTRIBUTE',
$propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent() => 'CONTAINER_ATTRIBUTE',
default => 'ELEMENT_ATTRIBUTE',
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Version/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ public function isSerializableAsXMLAttribute(): bool
}
return $propType->hasPrimitiveOrListParent()
|| $propType->isPrimitiveOrListType()
|| $propType->hasValueContainerParent()
|| $propType->isValueContainer();
|| $propType->hasPrimitiveContainerParent()
|| $propType->isPrimitiveContainer();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Version/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ public static function setContainedTypeFlag(Config $config, Version $version, Ty
}

/**
* Once parsing has been completed, we must do a final pass to identify types that are primitive containers by
* another name.
* Primitive containers are Element types that contain a "value" element that is, itself, a primitive.
*
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
Expand Down
13 changes: 3 additions & 10 deletions template/core/types/interface_primitive_container_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

$coreFiles = $config->getCoreFiles();

$elementInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE);
$valueContainerInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_VALUE_CONTAINER_TYPE);
$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
$xmlWriterClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_XML_WRITER);
$xmlValueLocationEnum = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION);

$imports = $coreFile->getimports();

$imports->addCoreFileImports(
$elementInterface,
$valueContainerInterface,
$serializeConfigClass,
$xmlWriterClass,
$xmlValueLocationEnum,
Expand All @@ -46,16 +46,9 @@

<?php echo ImportUtils::compileImportStatements($imports); ?>

interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $elementInterface->getEntityName(); ?>
interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $valueContainerInterface->getEntityName(); ?>

{
/**
* Must return the appropriate "formatted" stringified version of this type's contained primitive type's value
*
* @return string
*/
public function _getFormattedValue(): string;

/**
* Must return true if this primitive container type has a field set other than "value". This is used during
* serialization.
Expand Down
16 changes: 10 additions & 6 deletions template/core/types/interface_value_container_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

$coreFiles = $config->getCoreFiles();

$primitiveContainerInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE);
$elementInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE);
$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
$xmlWriterClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_XML_WRITER);
$xmlValueLocationEnum = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION);

$imports = $coreFile->getimports();

$imports->addCoreFileImports(
$primitiveContainerInterface,
$elementInterface,
$serializeConfigClass,
$xmlWriterClass,
$xmlValueLocationEnum,
Expand All @@ -47,13 +47,17 @@
<?php echo ImportUtils::compileImportStatements($imports); ?>

/**
* This indicates an Element type that contains a "value" property, but must be serialized differently than
* a primitive container
* This indicates an Element type that contains a "value" property
*/
interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $primitiveContainerInterface->getEntityName(); ?>
interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $elementInterface->getEntityName(); ?>

{

/**
* Must return the appropriate "formatted" stringified version of this type's contained primitive type's value
*
* @return string
*/
public function _getFormattedValue(): string;
}

<?php return ob_get_clean();
6 changes: 3 additions & 3 deletions template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Utils\XMLValueLocationUtils;
use DCarbone\PHPFHIR\Utilities\XMLValueLocationUtils;
use DCarbone\PHPFHIR\Utilities\DocumentationUtils;
use DCarbone\PHPFHIR\Utilities\TypeHintUtils;

Expand Down Expand Up @@ -53,7 +53,7 @@

$propertyType = $property->getValueFHIRType(); ?>
public const <?php echo $property->getFieldConstantName(); ?> = '<?php echo $property->getName(); ?>';
<?php if (null !== $propertyType && ($propertyType->isPrimitiveContainer() || $propertyType->isValueContainer())) :
<?php if (null !== $propertyType && ($propertyType->isPrimitiveContainer() || $propertyType->hasPrimitiveContainerParent())) :
?> public const <?php echo $property->getFieldConstantName(); ?>_EXT = '<?php echo $property->getExtName(); ?>';
<?php endif;
endforeach;
Expand Down Expand Up @@ -196,7 +196,7 @@ public function _getResourceType(): string
);
endif;

if ($type->isValueContainer() || $type->hasValueContainerParent()) : ?>
if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>

/* <?php echo basename(__FILE__) . ':' . __LINE__; ?> */
public function _nonValueFieldDefined(): bool
Expand Down
16 changes: 9 additions & 7 deletions template/versions/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Utils\XMLValueLocationUtils;
use DCarbone\PHPFHIR\Utilities\XMLValueLocationUtils;
use DCarbone\PHPFHIR\Utilities\DocumentationUtils;
use DCarbone\PHPFHIR\Utilities\TypeHintUtils;

Expand Down Expand Up @@ -120,7 +120,9 @@ public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUti
}
<?php
endif;
if ($propType->isValueContainer() || $propType->hasValueContainerParent() || $propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()) : ?>
if ($propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()
|| $propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent()
|| $propType->isValueContainer() || $propType->hasValueContainerParent()) : ?>
if (!($<?php echo $propertyName; ?> instanceof <?php echo $propTypeClassname; ?>)) {
$<?php echo $propertyName; ?> = new <?php echo $propTypeClassname; ?>(value: $<?php echo $propertyName; ?>);
}
Expand Down Expand Up @@ -173,7 +175,7 @@ public function set<?php echo ucfirst($propertyName); ?>(<?php echo TypeHintUtil
return $this;
}
<?php
if ($propType->isValueContainer()) : ?>
if ($propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent()) : ?>
$this-><?php echo $propertyName; ?> = [];
foreach($<?php echo $propertyName; ?> as $v) {
if ($v instanceof <?php echo $propTypeClassname; ?>) {
Expand Down Expand Up @@ -217,28 +219,28 @@ public function _get<?php echo ucfirst($propertyName); ?>ValueXMLLocation() : <?
*/
public function _set<?php echo ucfirst($propertyName); ?>ValueXMLLocation(<?php echo $valueXMLLocationEnum->getEntityName(); ?> $valueXMLLocation) : self
{
<?php if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>
<?php if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>
if (<?php echo $valueXMLLocationEnum->getEntityName(); ?>::PARENT_ATTRIBUTE === $valueXMLLocation) {
throw new \InvalidArgumentException(sprintf(
'Cannot set "%s" as value XML serialize location for property "<?php echo $propertyName; ?>" on value container type "<?php echo $type->getFHIRName(); ?>"',
$valueXMLLocation->name,
));
}
<?php elseif ($propType->isPrimitiveOrListType() || $propType->hasprimitiveType()) : ?>
<?php elseif ($propType->isPrimitiveOrListType() || $propType->hasprimitiveType()) : ?>
if (<?php echo $valueXMLLocationEnum->getEntityName(); ?>::CONTAINER_ATTRIBUTE === $valueXMLLocation) {
throw new \InvalidArgumentException(sprintf(
'Cannot set "%s" as value XML serialize location for primitive property "<?php echo $propertyName; ?>" on type "<?php echo $type->getfhirName(); ?>"',
$valueXMLLocation->name,
));
}
<?php endif; ?>
<?php endif; ?>
$this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>] = $valueXMLLocation;
return $this;
}
<?php endif;
endforeach;

if (($type->isValueContainer() && !$type->hasValueContainerParent()) || ($type->isQuantity() && !$type->hasQuantityParent())):
if (($type->isPrimitiveContainer() && !$type->hasPrimitiveContainerParent()) || ($type->isValueContainer() && !$type->hasValueContainerParent())):
$valueProp = $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
?>

Expand Down
8 changes: 4 additions & 4 deletions template/versions/types/serialization/xml/serialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$propType = $property->getValueFHIRType();

if ($property->isValueProperty()) :
if ($type->isValueContainer() || $type->hasValueContainerParent()) : ?>
if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>
if (isset($this-><?php echo $property->getName(); ?>) && <?php echo $xmlLocationEnum->getEntityName(); ?>::CONTAINER_ATTRIBUTE === $valueLocation) {
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $this-><?php echo $property->getName(); ?>->_getFormattedValue());
}
Expand All @@ -46,7 +46,7 @@
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $this-><?php echo $property->getName(); ?>->_getFormattedValue());
}
<?php endif;
elseif ($propType->isValueContainer() || $propType->hasValueContainerParent() || $propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()) : ?>
elseif ($propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent() || $propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()) : ?>
if (isset($this-><?php echo $property->getName(); ?>) && <?php echo $xmlLocationEnum->getEntityName(); ?>::PARENT_ATTRIBUTE === $this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>]) {
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $this-><?php echo $property->getName(); ?>->_getFormattedValue());
}
Expand Down Expand Up @@ -74,7 +74,7 @@
$propTypeKind = $propType->getKind();

// value property start
if (!$property->isCollection() && $property->isValueProperty() && ($type->isValueContainer() || $type->hasValueContainerParent())) : ?>
if (!$property->isCollection() && $property->isValueProperty() && ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent())) : ?>
if (isset($this-><?php echo $property->getName(); ?>)) {
if (<?php echo $xmlLocationEnum->getEntityName(); ?>::CONTAINER_VALUE === $valueLocation) {
$xw->text($this-><?php echo $property->getName(); ?>->_getFormattedValue());
Expand Down Expand Up @@ -136,7 +136,7 @@
// primitive type end

// value container start
elseif ($propType->isValueContainer() || $propType->hasValueContainerParent()) :
elseif ($propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent()) :
if ($property->isCollection()) : ?>
if (isset($this-><?php echo $property->getName(); ?>) && [] !== $this-><?php echo $property->getName(); ?>) {
foreach($this-><?php echo $property->getName(); ?> as $v) {
Expand Down

0 comments on commit d5715e8

Please sign in to comment.