Skip to content

Commit

Permalink
almost done with this xml nonsense....i think....
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 30, 2025
1 parent 484d25a commit 0c0ec9e
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 180 deletions.
1 change: 0 additions & 1 deletion files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
// Core encoding entities
const PHPFHIR_ENCODING_CLASSNAME_XML_WRITER = 'XMLWriter';
const PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION = 'ValueXMLLocationEnum';
const PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION = 'ValueXMLLocationTrait';
const PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
const PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';

Expand Down
1 change: 0 additions & 1 deletion src/Enum/TypeKindEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ enum TypeKindEnum: string
case RESOURCE = 'Resource';
case RESOURCE_CONTAINER = 'ResourceContainer';
case RESOURCE_INLINE = 'Resource.Inline';
case QUANTITY = 'Quantity';

// this indicates a type that is an immediate child of a resource and not used elsewhere
case RESOURCE_COMPONENT = 'resource_component';
Expand Down
18 changes: 14 additions & 4 deletions src/Enum/XMLValueLocationUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ class XMLValueLocationUtils
{
public static function determineDefaultLocation(Type $type, Property $property, bool $withClass): string
{
$case = match (true) {
$property->isValueProperty() && !$type->isQuantity() && !$type->hasQuantityParent() => 'LOCAL_ATTRIBUTE',
default => 'ELEMENT',
};
$propType = $property->getValueFHIRType();
if ($propType->isPrimitiveOrListType() || $propType->hasPrimitiveContainerParent()) {
$case = match(true) {
$type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent() => 'CONTAINER_ATTRIBUTE',
default => 'ELEMENT_ATTRIBUTE',
};
} else if ($property->isValueProperty()) {
$case = match (true) {
$type->isQuantity() || $type->hasQuantityParent() => 'ELEMENT_ATTRIBUTE',
default => 'CONTAINER_ATTRIBUTE',
};
} else {
$case = 'ELEMENT_ATTRIBUTE';
}
if ($withClass) {
return sprintf('%s::%s', PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION, $case);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public function hasValueContainerParent(): bool
*/
public function isQuantity(): bool
{
return $this->_kind === TypeKindEnum::QUANTITY;
return $this->_fhirName === 'Quantity';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public static function setValueContainerFlag(Config $config, Types $types): void
}

// skip "quantity" types, as their "value" is always applied as an element
if ($type->getKind()->isOneOf(TypeKindEnum::QUANTITY)) {
if ($type->isQuantity()) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions template/core/encoding/enum_value_xml_location.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum <?php echo PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION; ?>

{
case PARENT_ATTRIBUTE;
case LOCAL_ATTRIBUTE;
case ELEMENT;
case CONTAINER_ATTRIBUTE;
case ELEMENT_ATTRIBUTE;
case ELEMENT_VALUE;
}
<?php return ob_get_clean();
59 changes: 0 additions & 59 deletions template/core/encoding/trait_value_xml_location.php

This file was deleted.

4 changes: 1 addition & 3 deletions template/core/types/interface_element_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public static function xmlUnserialize(\SimpleXMLElement $element,
/**
* @param <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param <?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $config
* @return <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?>

*/
public function xmlSerialize(<?php echo $xmlWriterClass->getEntityName(); ?> $xw,
<?php echo $serializeConfigClass->getEntityName(); ?> $config): <?php echo $xmlWriterClass->getEntityName(); ?>;
<?php echo $serializeConfigClass->getEntityName(); ?> $config): void;

/**
* @param array $json Decoded JSON
Expand Down
4 changes: 1 addition & 3 deletions template/core/types/interface_value_container_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public function _nonValueFieldDefined(): bool;
* @param <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param <?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $xmlValueLocationEnum->getFullyQualifiedName(true); ?> $valueLocation
* @return <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?>

*/
public function xmlSerialize(<?php echo $xmlWriterClass->getEntityName(); ?> $xw,
<?php echo $serializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $xmlValueLocationEnum->getEntityName(); ?> $valueLocation = null): <?php echo $xmlWriterClass->getEntityName(); ?>;
null|<?php echo $xmlValueLocationEnum->getEntityName(); ?> $valueLocation = null): void;
}

<?php return ob_get_clean();
2 changes: 0 additions & 2 deletions template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ public function _nonValueFieldDefined(): bool
'type' => $type,
]
);

echo "\n";
endif;

if ($type->hasLocalProperties()) :
Expand Down
85 changes: 63 additions & 22 deletions template/versions/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,22 @@ public function get<?php echo ucfirst($propertyName); ?>(): <?php echo TypeHintU
{
return $this-><?php echo $propertyName; ?> ?? <?php if ($property->isCollection()) : ?>[]<?php else : ?>null<?php endif; ?>;
}
<?php if ($isCollection) : ?>
<?php
// start collection iterator getter
if ($isCollection) : ?>

/**
* @return \ArrayIterator<<?php echo $propType->getFullyQualifiedClassName(true); ?>>
*/
public function get<?php echo ucfirst($propertyName); ?>Iterator(): iterable
{
if (!isset($this-><?php echo $propertyName; ?>) || [] === $this-><?php echo $propertyName; ?>) {
if (!isset($this-><?php echo $propertyName; ?>)) {
return new \EmptyIterator();
}
return new \ArrayIterator($this-><?php echo $propertyName; ?>);
}
<?php
// end collection iterator getter
endif;

// end getter methods
Expand All @@ -98,48 +101,61 @@ public function get<?php echo ucfirst($propertyName); ?>Iterator(): iterable

* @param <?php echo TypeHintUtils::buildSetterParameterDocHint($version, $property, !$property->isCollection(), true); ?> $<?php echo $propertyName; ?>

<?php if ($property->isSerializableAsXMLAttribute()) : ?>
<?php
if ($property->isSerializableAsXMLAttribute()) : ?>
* @param <?php echo $valueXMLLocationEnum->getFullyQualifiedName(true); ?> $valueXMLLocation
<?php endif; ?>
<?php
endif; ?>
* @return static
*/
public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUtils::buildSetterParameterHint($version, $property, !$property->isCollection(), true); ?> $<?php echo $property; if ($property->isSerializableAsXMLAttribute()) : ?>,
<?php echo str_repeat(' ', strlen($property->getSetterName()));
echo $valueXMLLocationEnum->getEntityName(); ?> $valueXMLLocation = <?php echo XMLValueLocationUtils::determineDefaultLocation($type, $property, true); endif ?>): self
{
<?php if (!$property->isCollection()) : ?>
<?php
if (!$property->isCollection()) : ?>
if (null === $<?php echo $propertyName; ?>) {
unset($this-><?php echo $propertyName; ?>);
return $this;
}
<?php endif;
if ($propType->isPrimitiveContainer() || $propType->isPrimitiveOrListType()) : ?>
<?php
endif;
if ($propType->isPrimitiveContainer() || $propType->isPrimitiveOrListType()) : ?>
if (!($<?php echo $propertyName; ?> instanceof <?php echo $propTypeClassname; ?>)) {
$<?php echo $propertyName; ?> = new <?php echo $propTypeClassname; ?>(value: $<?php echo $propertyName; ?>);
}
<?php
elseif ($propTypeKind->isResourceContainer($version)) : ?>
elseif ($propTypeKind->isResourceContainer($version)) : ?>
if ($<?php echo $propertyName; ?> instanceof <?php echo $versionContainerType->getClassName(); ?>) {
$<?php echo $propertyName; ?> = $<?php echo $propertyName; ?>->getContainedType();
}
<?php endif;
<?php
endif;
if ($property->isCollection()) : ?>
if (!isset($this-><?php echo $propertyName; ?>)) {
$this-><?php echo $propertyName; ?> = [];
}
<?php endif;
if ($propTypeKind === TypeKindEnum::PHPFHIR_XHTML) : ?>
<?php
endif;
if ($propTypeKind === TypeKindEnum::PHPFHIR_XHTML) : ?>
if (!($<?php echo $propertyName; ?> instanceof <?php echo $propTypeClassname; ?>)) {
$<?php echo $propertyName; ?> = new <?php echo $propTypeClassname; ?>($<?php echo $propertyName; ?>);
}
<?php endif; ?>
<?php
endif; ?>
$this-><?php echo $propertyName; echo $isCollection ? '[]' : ''; ?> = $<?php echo $propertyName; ?>;
<?php if ($property->isSerializableAsXMLAttribute()) : ?>
$this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>] = $valueXMLLocation;
<?php endif; ?>
<?php
if ($property->isSerializableAsXMLAttribute()) : ?>
if ($this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>] !== $valueXMLLocation) {
$this->_set<?php echo ucfirst($property->getName()); ?>ValueXMLLocation($valueXMLLocation);
}
<?php
endif; ?>
return $this;
}
<?php if ($isCollection) : ?>
<?php
// start collection setter method
if ($isCollection) : ?>

/**<?php if ('' !== $documentation) : ?>

Expand All @@ -152,7 +168,12 @@ public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUti
*/
public function set<?php echo ucfirst($propertyName); ?>(<?php echo TypeHintUtils::buildSetterParameterHint($version, $property, false, true); ?> ...$<?php echo $propertyName; ?>): self
{
<?php if ($propType->isValueContainer()) : ?>
if ([] === $<?php echo $propertyName; ?>) {
unset($this-><?php echo $propertyName; ?>);
return $this;
}
<?php
if ($propType->isValueContainer()) : ?>
$this-><?php echo $propertyName; ?> = [];
foreach($<?php echo $propertyName; ?> as $v) {
if ($v instanceof <?php echo $propTypeClassname; ?>) {
Expand All @@ -161,13 +182,18 @@ public function set<?php echo ucfirst($propertyName); ?>(<?php echo TypeHintUtil
$this-><?php echo $propertyName; ?>[] = new <?php echo $propTypeClassname; ?>(value: $v);
}
}
<?php else : ?>
<?php
else : ?>
$this-><?php echo $propertyName; ?> = $<?php echo $propertyName; ?>;
<?php endif; ?>
<?php
endif; ?>
return $this;
}
<?php endif;
if ($property->isSerializableAsXMLAttribute()) : ?>
<?php
// end collection setter method
endif;

if ($property->isSerializableAsXMLAttribute()) : ?>

/**
* Return the current location the "value" field of the <?php echo $property->getName(); ?> element will be placed
Expand All @@ -191,6 +217,21 @@ 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()) : ?>
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()) : ?>
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; ?>
$this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>] = $valueXMLLocation;
return $this;
}
Expand All @@ -211,7 +252,7 @@ public function _set<?php echo ucfirst($propertyName); ?>ValueXMLLocation(<?php
*/
public function _getFormattedValue(): string
{
return (string)($this->getValue()?->_getFormattedValue());
return isset($this-><?php echo $valueProp->getName(); ?>) ? $this-><?php echo $valueProp->getName(); ?>->_getFormattedValue() : '';
}
<?php endif;

Expand Down
4 changes: 3 additions & 1 deletion template/versions/types/serialization/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@
'type' => $type,
]
);
?>

if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>
if (isset($rootOpened) && $rootOpened) {
$xw->endElement();
}
if (isset($docStarted) && $docStarted) {
$xw->endDocument();
}
return $xw;
<?php endif; ?>
}
<?php return ob_get_clean();
Loading

0 comments on commit 0c0ec9e

Please sign in to comment.