From 4b86dd2925a9e48b51971567b5029e4605314154 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Sat, 1 Feb 2025 15:48:13 -0600 Subject: [PATCH] some hacks for the resource container, not sure it even serves a purpose anymore... --- src/Version/Definition/Type.php | 7 ++++--- .../versions/types/class_resource_container.php | 16 ++++++++++------ .../serialization/json/unserialize/header.php | 17 +++++++++++------ .../serialization/xml/unserialize/header.php | 17 +++++++++++------ 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Version/Definition/Type.php b/src/Version/Definition/Type.php index 3d2e53ba..3ba9308e 100644 --- a/src/Version/Definition/Type.php +++ b/src/Version/Definition/Type.php @@ -878,8 +878,8 @@ public function getDirectlyImplementedInterfaces(): array ->getFullyQualifiedNamespace(false); } } - } else if ($this->isResourceType()) { - if (!$this->hasResourceTypeParent()) { + } else if ($this->isResourceType() || $this->getKind()->isResourceContainer($this->_version)) { + if (!$this->hasResourceTypeParent() && !$sourceMeta->isDSTU1()) { $interfaces[PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE] = $coreFiles ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE) ->getFullyQualifiedNamespace(false); @@ -913,6 +913,7 @@ public function getDirectlyImplementedInterfaces(): array public function getDirectlyUsedTraits(): array { $traits = []; + $sourceMeta = $this->_version->getSourceMetadata(); $parentType = $this->getParentType(); $coreFiles = $this->_version->getConfig()->getCoreFiles(); @@ -926,7 +927,7 @@ public function getDirectlyUsedTraits(): array } // these must only be added if the type has local properties - if (($this->isResourceType() || $this->getVersion()->getSourceMetadata()->isDSTU1()) && $this->hasLocalProperties()) { + if (($this->isResourceType() || $sourceMeta->isDSTU1() || $this->_kind->isResourceContainer($this->_version)) && $this->hasLocalProperties()) { $traits[PHPFHIR_TRAIT_SOURCE_XMLNS] = $coreFiles ->getCoreFileByEntityName(PHPFHIR_TRAIT_SOURCE_XMLNS) ->getFullyQualifiedNamespace(false); diff --git a/template/versions/types/class_resource_container.php b/template/versions/types/class_resource_container.php index c87805e6..ba6bcd26 100644 --- a/template/versions/types/class_resource_container.php +++ b/template/versions/types/class_resource_container.php @@ -117,7 +117,7 @@ public function setContainedType(null|children() as $child) { /** @var getFullyQualifiedName(true); ?> $class */ $class = getEntityName(); ?>::getContainedTypeClassNameFromXML($child); - $type->setContainedType($class::xmlUnserialize($child, null, $config)); + $type->setContainedType($class::xmlUnserialize($child, $config)); break; } return $type; @@ -137,8 +137,11 @@ public function xmlSerialize(null|getEntityName(); ? if (null !== $containedType) { return $containedType->xmlSerialize($xw, $config); } + if (null === $config) { + $config = (new ())->getConfig()->getSerializeConfig(); + } if (null === $xw) { - $xw = new getEntityName(); ?>(); + $xw = new getEntityName(); ?>($config); } if (!$xw->isOpen()) { $xw->openMemory(); @@ -147,12 +150,9 @@ public function xmlSerialize(null|getEntityName(); ? $docStarted = true; $xw->startDocument(); } - if (null === $config) { - $config = (new ())->getConfig()->getSerializeConfig(); - } if (!$xw->isRootOpen()) { $rootOpened = true; - $xw->openRootNode($config, '', $this->_getSourceXMLNS()); + $xw->openRootNode('', $this->_getSourceXMLNS()); } if (isset($rootOpened) && $rootOpened) { $xw->endElement(); @@ -173,6 +173,10 @@ public function xmlSerialize(null|getEntityName(); ? ] ); ?> + /** @var getFullyQualifiedName(true); ?> $class */ + $class = getEntityName(); ?>::getContainedTypeClassNameFromArray($json); + $type->setContainedType($class::jsonUnserialize($json)); + return $type; } public function __toString(): string diff --git a/template/versions/types/serialization/json/unserialize/header.php b/template/versions/types/serialization/json/unserialize/header.php index d434e8d9..fae5d622 100644 --- a/template/versions/types/serialization/json/unserialize/header.php +++ b/template/versions/types/serialization/json/unserialize/header.php @@ -21,13 +21,18 @@ $sourceMeta = $version->getSourceMetadata(); +$isResource = $type->isResourceType() + || $type->hasResourceTypeParent() + || $sourceMeta->isDSTU1() + || $type->getKind()->isResourceContainer($version); + $config = $version->getConfig(); $coreFiles = $config->getCoreFiles(); $constantsClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_CONSTANTS); $unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG); -if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) { +if ($isResource) { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); } else { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE); @@ -38,15 +43,15 @@ ob_start(); ?> /** - * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\stdClass|array $json - * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $config + * @param string|\stdClass|array $json + * @param null|getFullyQualifiedName(true); ?> $config * @param null|getFullyQualifiedClassName(true); ?> $type * @return getFullyQualifiedClassName(true); ?> * @throws \Exception */ - public static function jsonUnserialize(isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\stdClass|array $json, - isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getEntityName() ?> $configisResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = null, + public static function jsonUnserialize(string|\stdClass|array $json, + null|getEntityName() ?> $config = null, null|getEntityName(); ?> $type = null): self { isAbstract()) : // abstract types may not be instantiated directly ?> @@ -63,7 +68,7 @@ public static function jsonUnserialize(isResourceType() || $typ get_class($type) )); } -isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> + if (null === $config) { $config = (new getEntityName(); ?>())->getConfig()->getUnserializeConfig(); } diff --git a/template/versions/types/serialization/xml/unserialize/header.php b/template/versions/types/serialization/xml/unserialize/header.php index 0aa0b0c9..5b49ad4c 100644 --- a/template/versions/types/serialization/xml/unserialize/header.php +++ b/template/versions/types/serialization/xml/unserialize/header.php @@ -21,12 +21,17 @@ $sourceMeta = $version->getSourceMetadata(); +$isResource = $type->isResourceType() + || $type->hasResourceTypeParent() + || $sourceMeta->isDSTU1() + || $type->getKind()->isResourceContainer($version); + $config = $version->getConfig(); $coreFiles = $config->getCoreFiles(); $unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG); -if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) { +if ($isResource) { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); } else { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE); @@ -37,15 +42,15 @@ ob_start(); ?> /** - * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\SimpleXMLElement $element - * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $config + * @param string|\SimpleXMLElement $element + * @param null|getFullyQualifiedName(true); ?> $config * @param null|getFullyQualifiedClassName(true); ?> $type * @return getFullyQualifiedClassName(true); ?> * @throws \Exception */ - public static function xmlUnserialize(isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\SimpleXMLElement $element, - isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getEntityName() ?> $configisResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = null, + public static function xmlUnserialize(string|\SimpleXMLElement $element, + null|getEntityName() ?> $config = null, null|getEntityName(); ?> $type = null): self { isAbstract()) : // abstract types may not be instantiated directly ?> @@ -62,7 +67,7 @@ public static function xmlUnserialize(isResourceType() || $type get_class($type) )); } -isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> + if (null === $config) { $config = (new getEntityName(); ?>())->getConfig()->getUnserializeConfig(); }