Skip to content

Commit

Permalink
more work on xml serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 29, 2025
1 parent 7b94f1e commit 6f361a1
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 77 deletions.
2 changes: 1 addition & 1 deletion files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
// Core types entities
const PHPFHIR_TYPES_INTERFACE_TYPE = 'TypeInterface';
const PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE = 'PrimitiveTypeInterface';
const PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE = 'PrimitiveContainerTypeInterface';
const PHPFHIR_TYPES_INTERFACE_VALUE_CONTAINER_TYPE = 'ValueContainerTypeInterface';
const PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE = 'ElementTypeInterface';
const PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE = 'ResourceTypeInterface';

Expand Down
37 changes: 37 additions & 0 deletions src/Enum/XMLValueLocationUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace DCarbone\PHPFHIR\Enum;

/*
* Copyright 2025 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use DCarbone\PHPFHIR\Version\Definition\Property;
use DCarbone\PHPFHIR\Version\Definition\Type;

class XMLValueLocationUtils
{
public static function determineDefaultLocation(Type $type, Property $property, bool $withClass): string
{
$case = match (true) {
$property->isValueProperty() => 'LOCAL_ATTRIBUTE',
default => 'ELEMENT',
};
if ($withClass) {
return sprintf('%s::%s', PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION, $case);
}
return $case;
}
}
4 changes: 1 addition & 3 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
$imports->addImport($namespace, $trait);
}

if ($type->isPrimitiveOrListType() || $type->hasPrimitiveOrListParent()) {

} else if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) {
if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) {
$imports->addCoreFileImportsByName(
PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE,
);
Expand Down
3 changes: 2 additions & 1 deletion src/Version/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,9 @@ public function isSerializableAsXMLAttribute(): bool
return false;
}
return $propType->hasPrimitiveOrListParent()
|| $propType->isPrimitiveOrListType()
|| $propType->isPrimitiveContainer()
|| $propType->isPrimitiveOrListType();
|| ($this->_memberOf->isValueContainer() && $this->isValueProperty());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,10 @@ public function getDirectlyImplementedInterfaces(): array
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE)
->getFullyQualifiedNamespace(false);
}
} else if ($this->isPrimitiveContainer()) {
if (!$this->hasPrimitiveContainerParent()) {
$interfaces[PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE)
} else if ($this->isValueContainer()) {
if (!$this->hasValueContainerParent()) {
$interfaces[PHPFHIR_TYPES_INTERFACE_VALUE_CONTAINER_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_VALUE_CONTAINER_TYPE)
->getFullyQualifiedNamespace(false);
}
} else if ($this->isResourceType()) {
Expand Down
28 changes: 14 additions & 14 deletions template/core/types/interface_element_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,33 @@ interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $typeInte
public function _getSourceXMLNS(): null|string;

/**
* @param string|\SimpleXMLElement $element
* @param \SimpleXMLElement $element Decoded XML
* @param <?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @param null|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @return static
*/
public static function xmlUnserialize(string|\SimpleXMLElement $element,
null|<?php echo $coreFile->getEntityName(); ?> $type = null,
null|<?php echo $unserializeConfigClass->getEntityName() ?> $config = null): self;
public static function xmlUnserialize(\SimpleXMLElement $element,
<?php echo $unserializeConfigClass->getEntityName() ?> $config,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;

/**
* @param null|<?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param null|<?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param <?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $config
* @return <?php echo $xmlWriterClass->getFullyQualifiedName(true); ?>

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

/**
* @param string|array|\stdClass $json Raw or already un-encoded JSON
* @param array $json Decoded JSON
* @param <?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @param null|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @return static
*/
public static function jsonUnserialize(string|array|\stdClass $json,
null|<?php echo $coreFile->getEntityName(); ?> $type = null,
null|<?php echo $unserializeConfigClass->getEntityName(); ?> $config = null): self;
public static function jsonUnserialize(array $json,
<?php echo $unserializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;
}

<?php return ob_get_clean();
12 changes: 6 additions & 6 deletions template/core/types/interface_resource_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public function _getSourceXMLNS(): null|string;

/**
* @param string|\SimpleXMLElement $element
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @param null|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @return static
*/
public static function xmlUnserialize(string|\SimpleXMLElement $element,
null|<?php echo $coreFile->getEntityName(); ?> $type = null,
null|<?php echo $unserializeConfigClass->getEntityName() ?> $config = null): self;
null|<?php echo $unserializeConfigClass->getEntityName() ?> $config = null,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;

/**
* @param null|<?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
Expand All @@ -73,13 +73,13 @@ public function xmlSerialize(null|<?php echo $xmlWriterClass->getEntityName(); ?

/**
* @param string|array|\stdClass $json Raw or already un-encoded JSON
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @param null|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $coreFile->getFullyQualifiedName(true); ?> $type Instance of this class to unserialize into. If left null, a new instance will be created.
* @return static
*/
public static function jsonUnserialize(string|array|\stdClass $json,
null|<?php echo $coreFile->getEntityName(); ?> $type = null,
null|<?php echo $unserializeConfigClass->getEntityName(); ?> $config = null): self;
null|<?php echo $unserializeConfigClass->getEntityName(); ?> $config = null,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;
}

<?php return ob_get_clean();
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public function _getFormattedValue(): string;
public function _nonValueFieldDefined(): bool;

/**
* @param null|<?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param null|<?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $config
* @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(null|<?php echo $xmlWriterClass->getEntityName(); ?> $xw = null,
null|<?php echo $serializeConfigClass->getEntityName(); ?> $config = null,
public function xmlSerialize(<?php echo $xmlWriterClass->getEntityName(); ?> $xw,
<?php echo $serializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $xmlValueLocationEnum->getEntityName(); ?> $valueLocation = null): <?php echo $xmlWriterClass->getEntityName(); ?>;
}

Expand Down
5 changes: 3 additions & 2 deletions template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

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

Expand Down Expand Up @@ -90,11 +91,11 @@

/* <?php echo basename(__FILE__) . ':' . __LINE__; ?> */
private array $_valueXMLLocations = [
<?php foreach ($type->getAllPropertiesIndexedIterator() as $property) :
<?php foreach ($type->getProperties()->getIterator() as $property) :
if (!$property->isSerializableAsXMLAttribute()) {
continue;
} ?>
self::<?php echo $property->getFieldConstantName(); ?> => <?php echo $xmlLocationEnum->getEntityName(); ?>::ATTRIBUTE,
self::<?php echo $property->getFieldConstantName(); ?> => <?php echo XMLValueLocationUtils::determineDefaultLocation($type, $property, true); ?>,
<?php endforeach; ?>
];
<?php
Expand Down
13 changes: 11 additions & 2 deletions template/versions/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

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

Expand Down Expand Up @@ -54,6 +55,8 @@
if ($i > 0) {
echo "\n";
}

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

Expand Down Expand Up @@ -81,7 +84,12 @@ public function get<?php echo ucfirst($propertyName); ?>Iterator(): iterable
return new \ArrayIterator($this-><?php echo $propertyName; ?>);
}
<?php
endif;?>
endif;

// end getter methods

// start setter methods
?>

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

Expand All @@ -96,7 +104,8 @@ public function get<?php echo ucfirst($propertyName); ?>Iterator(): iterable
* @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 $valueXMLLocationEnum->getEntityName(); ?>::ATTRIBUTE<?php endif ?>): self
<?php echo str_repeat(' ', strlen($property->getSetterName()));
echo $valueXMLLocationEnum->getEntityName(); ?> $valueXMLLocation = <?php echo XMLValueLocationUtils::determineDefaultLocation($type, $property, true); endif ?>): self
{
<?php if (!$property->isCollection()) : ?>
if (null === $<?php echo $propertyName; ?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@

ob_start(); ?>
/**
* @param string|array|\stdClass $json
* @param <?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>string|\stdClass|<?php endif; ?>array $json
* @param <?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>null|<?php endif; echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $type->getFullyQualifiedClassName(true); ?> $type
* @param null|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @return <?php echo $type->getFullyQualifiedClassName(true); ?>

* @throws \Exception
*/
public static function jsonUnserialize(string|array|\stdClass $json,
null|<?php echo $typeInterface->getEntityName(); ?> $type = null,
null|<?php echo $unserializeConfigClass->getEntityName() ?> $config = null): self
public static function jsonUnserialize(<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>string|\stdClass|<?php endif; ?>array $json,
<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>null|<?php endif; echo $unserializeConfigClass->getEntityName() ?> $config<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?> = null<?php endif;?>,
null|<?php echo $typeInterface->getEntityName(); ?> $type = null): self
{
<?php if ($type->isAbstract()) : // abstract types may not be instantiated directly ?>
if (null === $type) {
Expand Down
3 changes: 2 additions & 1 deletion template/versions/types/serialization/xml/serialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@

// if this is the "value" property on a primitive container, the value may be serialized to the parent's root node,
// to the local node's attributes, or as an element on the local node.
if ($type->isPrimitiveContainer() && $property->isValueProperty()) : ?>
if ($type->isValueContainer() && $property->isValueProperty()) : ?>
if (isset($this-><?php echo $property->getName(); ?>)
&& <?php echo $xmlLocationEnum->getEntityName(); ?>::PARENT_ATTRIBUTE !== $valueLocation
&& (<?php echo $xmlLocationEnum->getEntityName(); ?>::LOCAL_ATTRIBUTE === $valueLocation
|| (null === $valueLocation && $this->_valueXMLLocations[self::<?php echo $property->getFieldConstantName(); ?>] === <?php echo $xmlLocationEnum->getEntityName(); ?>::LOCAL_ATTRIBUTE))) {
$xw->writeAttribute(self::<?php echo $property->getFieldConstantName(); ?>, $this-><?php echo $property->getName(); ?>->_getFormattedValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

*/
public function xmlSerialize(null|<?php echo $xmlWriterClass->getEntityName(); ?> $xw = null,
null|<?php echo $serializeConfigClass->getEntityName(); ?> $config = null<?php if ($type->isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>,
null|<?php echo $serializeConfigClass->getEntityName(); ?> $config = null<?php if ($type->isValueContainer() || $type->hasValueContainerParent()) : ?>,
null|<?php echo $xmlLocationEnum->getEntityName(); ?> $valueLocation = null<?php endif; ?>): <?php echo $xmlWriterClass->getEntityName(); ?>

{
Expand Down
Loading

0 comments on commit 6f361a1

Please sign in to comment.