diff --git a/files/constants.php b/files/constants.php index 598e0817..35211860 100644 --- a/files/constants.php +++ b/files/constants.php @@ -108,6 +108,7 @@ // Core types entities const PHPFHIR_TYPES_INTERFACE_TYPE = 'TypeInterface'; const PHPFHIR_TYPES_INTERFACE_DSTU1_TYPE = 'DSTU1TypeInterface'; +const PHPFHIR_TYPES_INTERFACE_DSTU1_PRIMITIVE_CONTAINER_TYPE = 'DSTU1PrimitiveContainerTypeInterface'; const PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE = 'PrimitiveTypeInterface'; const PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE = 'ElementTypeInterface'; const PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE = 'PrimitiveContainerTypeInterface'; diff --git a/src/Utilities/ImportUtils.php b/src/Utilities/ImportUtils.php index a4aeb09a..4098967e 100644 --- a/src/Utilities/ImportUtils.php +++ b/src/Utilities/ImportUtils.php @@ -69,6 +69,8 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo $logger->debug(sprintf('Compiling imports for Type "%s"...', $type->getFHIRName())); + $sourceMeta = $version->getSourceMetadata(); + $imports = $type->getImports(); // immediately add self @@ -110,7 +112,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo $imports->addCoreFileImportsByName( PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE, ); - } else { + } else if (!$sourceMeta->isDSTU1()) { $imports->addCoreFileImportsByName( PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE, ); @@ -128,6 +130,10 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo $imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS); } + if ($sourceMeta->isDSTU1()) { + $imports->addCoreFileImportsByName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); + } + if ($typeKind->isResourceContainer($type->getVersion())) { $imports->addVersionCoreFileImportsByName( $type->getVersion(), @@ -181,7 +187,7 @@ 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 = match(true) { + $valProp = match (true) { $propertyType->isPrimitiveContainer() => $propertyType->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME), $propertyType->hasPrimitiveContainerParent() => $propertyType->getParentProperty(PHPFHIR_VALUE_PROPERTY_NAME), default => null, diff --git a/src/Utilities/NameUtils.php b/src/Utilities/NameUtils.php index e671c08e..29073692 100644 --- a/src/Utilities/NameUtils.php +++ b/src/Utilities/NameUtils.php @@ -118,6 +118,7 @@ class NameUtils 'xml' => 'XML', 'http' => 'HTTP', 'curl' => 'CURL', + 'dstu1' => 'DSTU1', ]; /** @var array */ diff --git a/src/Version/Definition/Type.php b/src/Version/Definition/Type.php index 77fab403..3d2e53ba 100644 --- a/src/Version/Definition/Type.php +++ b/src/Version/Definition/Type.php @@ -848,6 +848,16 @@ public function getDirectlyImplementedInterfaces(): array $interfaces = []; $coreFiles = $this->_version->getConfig()->getCoreFiles(); $versionCoreFiles = $this->_version->getCoreFiles(); + $sourceMeta = $this->_version->getSourceMetadata(); + + // dstu1 has its own special type interface + if ($sourceMeta->isDSTU1() && !$this->isPrimitiveOrListType() && !$this->hasPrimitiveOrListParent()) { + if (null === $this->getParentType()) { + $interfaces[PHPFHIR_TYPES_INTERFACE_DSTU1_TYPE] = $coreFiles + ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_DSTU1_TYPE) + ->getFullyQualifiedNamespace(false); + } + } // first, determine which base type interface it must implement if ($this->isPrimitiveOrListType()) { @@ -858,9 +868,15 @@ public function getDirectlyImplementedInterfaces(): array } } else if ($this->isPrimitiveContainer()) { if (!$this->hasPrimitiveContainerParent()) { - $interfaces[PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE] = $coreFiles - ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE) - ->getFullyQualifiedNamespace(false); + if ($sourceMeta->isDSTU1()) { + $interfaces[PHPFHIR_TYPES_INTERFACE_DSTU1_PRIMITIVE_CONTAINER_TYPE] = $coreFiles + ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE) + ->getFullyQualifiedNamespace(false); + } else { + $interfaces[PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE] = $coreFiles + ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_CONTAINER_TYPE) + ->getFullyQualifiedNamespace(false); + } } } else if ($this->isResourceType()) { if (!$this->hasResourceTypeParent()) { @@ -868,7 +884,7 @@ public function getDirectlyImplementedInterfaces(): array ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE) ->getFullyQualifiedNamespace(false); } - } else if (!$this->hasParent()) { + } else if (!$this->hasParent() && !$sourceMeta->isDSTU1()) { $interfaces[PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE] = $coreFiles ->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE) ->getFullyQualifiedNamespace(false); @@ -910,7 +926,7 @@ public function getDirectlyUsedTraits(): array } // these must only be added if the type has local properties - if ($this->isResourceType() && $this->hasLocalProperties()) { + if (($this->isResourceType() || $this->getVersion()->getSourceMetadata()->isDSTU1()) && $this->hasLocalProperties()) { $traits[PHPFHIR_TRAIT_SOURCE_XMLNS] = $coreFiles ->getCoreFileByEntityName(PHPFHIR_TRAIT_SOURCE_XMLNS) ->getFullyQualifiedNamespace(false); diff --git a/template/core/types/interface_dstu1_primitive_container_type.php b/template/core/types/interface_dstu1_primitive_container_type.php new file mode 100644 index 00000000..b94cb017 --- /dev/null +++ b/template/core/types/interface_dstu1_primitive_container_type.php @@ -0,0 +1,71 @@ +getCoreFiles(); + +$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( + $valueContainerInterface, + $serializeConfigClass, + $xmlWriterClass, + $xmlValueLocationEnum, +); + +ob_start(); +echo 'declare(strict_types=1); + +namespace getFullyQualifiedNamespace(false); ?>; + +getBasePHPFHIRCopyrightComment(true); ?> + + + +interface getEntityName(); ?> extends getEntityName(); ?> + +{ + /** + * Must return true if this primitive container type has a field set other than "value". This is used during + * serialization. + * + * @return bool + */ + public function _nonValueFieldDefined(): bool; + + /** + * @param null|getFullyQualifiedName(true); ?> $xw + * @param null|getFullyQualifiedName(true); ?> $config + * @param null|getFullyQualifiedName(true); ?> $valueLocation + * @return getFullyQualifiedName(true); ?> + + */ + public function xmlSerialize(null|getEntityName(); ?> $xw, + null|getEntityName(); ?> $config, + null|getEntityName(); ?> $valueLocation = null): getEntityName() ?>; +} +getCoreFiles(); -$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE); -$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG); -$unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG); -$xmlWriterClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_XML_WRITER); +$resourceTypeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); $imports = $coreFile->getimports(); -$imports->addCoreFileImports($typeInterface, $serializeConfigClass, $unserializeConfigClass, $xmlWriterClass); +$imports->addCoreFileImports($resourceTypeInterface); ob_start(); @@ -48,44 +45,9 @@ * This is a special interface only applied when generating types for versions based on DSTU1. It is necessary * as Resources in DSTU1 are extensions of Elements. This is unique to DSTU1. */ -interface getEntityName(); ?> extends getEntityName(); ?>, \JsonSerializable -{ - /** - * Returns the root XMLNS value found in the source. Null indicates no "xmlns" was found. Only defined when - * unserializing XML, and only used when serializing XML. - * - * @return null|string - */ - public function _getSourceXMLNS(): null|string; - - /** - * @param string|\SimpleXMLElement $element - * @param null|getFullyQualifiedName(true); ?> $config - * @param null|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|getEntityName() ?> $config = null, - null|getEntityName(); ?> $type = null): self; - - /** - * @param null|getFullyQualifiedName(true); ?> $xw - * @param null|getFullyQualifiedName(true); ?> $config - * @return getFullyQualifiedName(true); ?> +interface getEntityName(); ?> extends getEntityName(); ?> - */ - public function xmlSerialize(null|getEntityName(); ?> $xw = null, - null|getEntityName(); ?> $config = null): getEntityName(); ?>; +{ - /** - * @param string|array|\stdClass $json Raw or already un-encoded JSON - * @param null|getFullyQualifiedName(true); ?> $config - * @param null|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|getEntityName(); ?> $config = null, - null|getEntityName(); ?> $type = null): self; } - getEntityName(); ?> $config, null|getEntityName(); ?> $type = null): self; } - getEntityName(); ?> $xw getEntityName(); ?> $config, null|getEntityName(); ?> $valueLocation = null): void; } - getEntityName(); ?> $config = null, null|getEntityName(); ?> $type = null): self; } - getEntityName(); ?> */ public function _getFormattedValue(): string; } - getEntityName(); ?> $xw = null, - null|getEntityName(); ?> $config = null): getEntityName(); ?>; + null|getEntityName(); ?> $config = null): getEntityName(); ?> { $containedType = $this->getContainedType(); diff --git a/template/versions/types/header.php b/template/versions/types/header.php index f5ef6953..67454670 100644 --- a/template/versions/types/header.php +++ b/template/versions/types/header.php @@ -50,9 +50,8 @@ { use ; - - + // name of FHIR type this class describes public const FHIR_TYPE_NAME = getTypeNameConst(true); ?>; diff --git a/template/versions/types/serialization/json/unserialize/header.php b/template/versions/types/serialization/json/unserialize/header.php index 1df14e39..d434e8d9 100644 --- a/template/versions/types/serialization/json/unserialize/header.php +++ b/template/versions/types/serialization/json/unserialize/header.php @@ -19,13 +19,15 @@ /** @var \DCarbone\PHPFHIR\Version $version */ /** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */ +$sourceMeta = $version->getSourceMetadata(); + $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()) { +if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); } else { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE); @@ -36,15 +38,15 @@ ob_start(); ?> /** - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>string|\stdClass|array $json - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>null|getFullyQualifiedName(true); ?> $config + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\stdClass|array $json + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $config * @param null|getFullyQualifiedClassName(true); ?> $type * @return getFullyQualifiedClassName(true); ?> * @throws \Exception */ - public static function jsonUnserialize(isResourceType() || $type->hasResourceTypeParent()) : ?>string|\stdClass|array $json, - isResourceType() || $type->hasResourceTypeParent()) : ?>null|getEntityName() ?> $configisResourceType() || $type->hasResourceTypeParent()) : ?> = null, + 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, null|getEntityName(); ?> $type = null): self { isAbstract()) : // abstract types may not be instantiated directly ?> @@ -61,7 +63,7 @@ public static function jsonUnserialize(isResourceType() || $typ get_class($type) )); } -isResourceType() || $type->hasResourceTypeParent()) : ?> +isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> if (null === $config) { $config = (new getEntityName(); ?>())->getConfig()->getUnserializeConfig(); } diff --git a/template/versions/types/serialization/xml.php b/template/versions/types/serialization/xml.php index c6fdce17..1a1e83d6 100644 --- a/template/versions/types/serialization/xml.php +++ b/template/versions/types/serialization/xml.php @@ -19,6 +19,8 @@ /** @var \DCarbone\PHPFHIR\Version $version */ /** @var \DCarbone\PHPFHIR\Version\Definition\Type $type; */ +$sourceMeta = $version->getSourceMetadata(); + $typeKind = $type->getKind(); ob_start(); @@ -66,7 +68,7 @@ ] ); - if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?> + if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> if (isset($rootOpened) && $rootOpened) { $xw->endElement(); } diff --git a/template/versions/types/serialization/xml/serialize/header.php b/template/versions/types/serialization/xml/serialize/header.php index 87ec7e10..7882c390 100644 --- a/template/versions/types/serialization/xml/serialize/header.php +++ b/template/versions/types/serialization/xml/serialize/header.php @@ -21,6 +21,8 @@ /** @var \DCarbone\PHPFHIR\Version $version */ /** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */ +$sourceMeta = $version->getSourceMetadata(); + $config = $version->getConfig(); $coreFiles = $config->getCoreFiles(); @@ -33,23 +35,23 @@ ob_start(); ?> /** - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>null|getFullyQualifiedName(true); ?> $xw - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>null|getFullyQualifiedName(true); ?> $config + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $xw + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $config isPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?> * @param null|getFullyQualifiedName(true); ?> $valueLocation -isResourceType() || $type->hasResourceTypeParent()) : ?> +isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> * @return getFullyQualifiedName(true); ?> */ - public function xmlSerialize(isResourceType() || $type->hasResourceTypeParent()) : ?>null|getEntityName(); ?> $xwisResourceType() || $type->hasResourceTypeParent()) : ?> = null, - isResourceType() || $type->hasResourceTypeParent()) : ?>null|getEntityName(); ?> $configisResourceType() || $type->hasResourceTypeParent()) : ?> = nullisResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getEntityName(); ?> $xwisResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = null, + isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getEntityName(); ?> $configisResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = nullisPrimitiveContainer() || $type->hasPrimitiveContainerParent()) : ?>, - null|getEntityName(); ?> $valueLocation = null): isResourceType() || $type->hasResourceTypeParent()) : echo $xmlWriterClass->getEntityName(); else : ?>void + null|getEntityName(); ?> $valueLocation = null): isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : echo $xmlWriterClass->getEntityName(); else : ?>void { -isResourceType() || $type->hasResourceTypeParent()) : ?> +isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> if (null === $config) { $config = (new getEntityName(); ?>())->getConfig()->getSerializeConfig(); } diff --git a/template/versions/types/serialization/xml/unserialize/header.php b/template/versions/types/serialization/xml/unserialize/header.php index 60d8de02..0aa0b0c9 100644 --- a/template/versions/types/serialization/xml/unserialize/header.php +++ b/template/versions/types/serialization/xml/unserialize/header.php @@ -19,12 +19,14 @@ /** @var \DCarbone\PHPFHIR\Version $version */ /** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */ +$sourceMeta = $version->getSourceMetadata(); + $config = $version->getConfig(); $coreFiles = $config->getCoreFiles(); $unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG); -if ($type->isResourceType() || $type->hasResourceTypeParent()) { +if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE); } else { $typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_ELEMENT_TYPE); @@ -35,15 +37,15 @@ ob_start(); ?> /** - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>string|\SimpleXMLElement $element - * @param isResourceType() || $type->hasResourceTypeParent()) : ?>null|getFullyQualifiedName(true); ?> $config + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\SimpleXMLElement $element + * @param isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|getFullyQualifiedName(true); ?> $config * @param null|getFullyQualifiedClassName(true); ?> $type * @return getFullyQualifiedClassName(true); ?> * @throws \Exception */ - public static function xmlUnserialize(isResourceType() || $type->hasResourceTypeParent()) : ?>string|\SimpleXMLElement $element, - isResourceType() || $type->hasResourceTypeParent()) : ?>null|getEntityName() ?> $configisResourceType() || $type->hasResourceTypeParent()) : ?> = null, + 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, null|getEntityName(); ?> $type = null): self { isAbstract()) : // abstract types may not be instantiated directly ?> @@ -60,7 +62,7 @@ public static function xmlUnserialize(isResourceType() || $type get_class($type) )); } -isResourceType() || $type->hasResourceTypeParent()) : ?> +isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> if (null === $config) { $config = (new getEntityName(); ?>())->getConfig()->getUnserializeConfig(); }