Skip to content

Commit

Permalink
Merge pull request #143 from dcarbone/dcarbone/xml-loc-improvement
Browse files Browse the repository at this point in the history
Improved default XML serialization location
  • Loading branch information
dcarbone authored Aug 30, 2024
2 parents 627f95b + 09fab7d commit 6680ff4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
18 changes: 12 additions & 6 deletions template/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ public function get<?php echo ucfirst($propertyName); ?>(): <?php echo TypeHintU
* @param <?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_XML_LOCATION_ENUM); ?> $xmlLocation
* @return static
*/
public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUtils::propertySetterTypeHint($config, $property, true); ?> $<?php echo $property; ?> = null, <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?> $xmlLocation = <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE): self
public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUtils::propertySetterTypeHint($config, $property, true); ?> $<?php echo $property; ?> = null, <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?> $xmlLocation = <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::<?php if ($propertyType->isValueContainer()): ?>ELEMENT<?php else : ?>ATTRIBUTE<?php endif; ?>): self
{
if (null !== $<?php echo $propertyName; ?> && !($<?php echo $propertyName; ?> instanceof <?php echo $propertyTypeClassName; ?>)) {
$<?php echo $propertyName; ?> = new <?php echo $propertyTypeClassName; ?>($<?php echo $propertyName; ?>);
}
$this->_trackValue<?php if ($isCollection) : ?>Added(<?php else : ?>Set($this-><?php echo $propertyName; ?>, $<?php echo $propertyName; endif; ?>);
if (!isset($this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>])) {
$this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] = [];
if (!isset($this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>])) {
$this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] = [];
}
$this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>]<?php if ($isCollection) : ?>[]<?php else : ?>[0]<?php endif; ?> = $xmlLocation;
<?php if ($isCollection) : ?>if ([] === $this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>]) {
$this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>][0] = $xmlLocation;
} else {
$this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>][] = <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT;
}<?php else : ?>
$this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>][0] = $xmlLocation;<?php endif; ?>

$this-><?php echo $propertyName; ?><?php echo $isCollection ? '[]' : ''; ?> = $<?php echo $propertyName; ?>;
return $this;
}
Expand All @@ -91,9 +97,9 @@ public function <?php echo $property->getSetterName(); ?>(<?php echo TypeHintUti
* @param <?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_XML_LOCATION_ENUM); ?> $xmlLocation
* @return static
*/
public function set<?php echo ucfirst($propertyName); ?>(array $<?php echo $propertyName; ?> = [], <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?> $xmlLocation = <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE): self
public function set<?php echo ucfirst($propertyName); ?>(array $<?php echo $propertyName; ?> = [], <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?> $xmlLocation = <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::<?php if ($propertyType->isValueContainer()): ?>ELEMENT<?php else : ?>ATTRIBUTE<?php endif; ?>): self
{
unset($this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>]);
unset($this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>]);
if ([] !== $this-><?php echo $propertyName; ?>) {
$this->_trackValuesRemoved(count($this-><?php echo $propertyName; ?>));
$this-><?php echo $propertyName; ?> = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

ob_start(); ?>
/** @var array */
private array $_primitiveXmlLocations = [];
private array $_xmlLocations = [];
<?php return ob_get_clean();
10 changes: 5 additions & 5 deletions template/types/serialization/xml/serialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
continue;
}
if ($pt->hasPrimitiveParent() || $pt->getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) : ?>
$locs = $this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
$locs = $this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
<?php if ($property->isCollection()) : ?>
if ([] === $locs && [] !== ($vs = $this-><?php echo $property->getGetterName(); ?>())) {
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $vs[0]->getFormattedValue());
Expand All @@ -52,7 +52,7 @@
}
<?php endif;
elseif ($pt->getKind() === TypeKind::PRIMITIVE_CONTAINER) : ?>
$locs = $this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
$locs = $this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
<?php if ($property->isCollection()) : ?>
if ([] === $locs && [] !== ($vs = $this-><?php echo $property->getGetterName(); ?>())) {
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $vs[0]->getValue()?->getFormattedValue());
Expand Down Expand Up @@ -99,7 +99,7 @@
}
<?php endif;
elseif ($pt->hasPrimitiveParent() || $ptk->isOneOf(TypeKind::LIST, TypeKind::PRIMITIVE)) : ?>
$locs = $this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
$locs = $this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
<?php if ($property->isCollection()) : ?>
if (([] === $locs || in_array(<?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT, $locs, true)) && [] !== ($vs = $this-><?php echo $property->getGetterName(); ?>())) {
foreach($vs as $i => $v) {
Expand All @@ -118,7 +118,7 @@
}
<?php endif;
elseif ($ptk === TypeKind::PRIMITIVE_CONTAINER) : ?>
$locs = $this->_primitiveXmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
$locs = $this->_xmlLocations[self::<?php echo $property->getFieldConstantName(); ?>] ?? [];
<?php if ($property->isCollection()) : ?>
if (([] === $locs || in_array(<?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT, $locs, true)) && [] !== ($vs = $this-><?php echo $property->getGetterName(); ?>())) {
foreach($vs as $i => $v) {
Expand Down Expand Up @@ -156,7 +156,7 @@
//
// Uncomment and implement properly if the need arises in the future.
/*?>
if (($this->_primitiveXmlLocations[self::FIELD_VALUE] ?? <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE) === <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT) {
if (($this->_xmlLocations[self::FIELD_VALUE] ?? <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE) === <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT) {
$xw->writeSimpleElement(self::FIELD_VALUE, $this->getFormattedValue());
}
<?php */ endif;
Expand Down
4 changes: 2 additions & 2 deletions template/types/tests/unit/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
]
);

if ($typeKind === TypeKind::PRIMITIVE) :
if ($typeKind === TypeKind::PRIMITIVE) {
echo require_with(
PHPFHIR_TEMPLATE_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType->value . DIRECTORY_SEPARATOR . 'body_primitive.php',
[
'config' => $config,
'type' => $type,
]
);
endif;
}

echo "}\n";
return ob_get_clean();

0 comments on commit 6680ff4

Please sign in to comment.