Skip to content

Commit

Permalink
some hacks for the resource container, not sure it even serves a purp…
Browse files Browse the repository at this point in the history
…ose anymore...
  • Loading branch information
dcarbone committed Feb 1, 2025
1 parent bfc43d2 commit 4b86dd2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down
16 changes: 10 additions & 6 deletions template/versions/types/class_resource_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function setContainedType(null|<?php echo $versionContainedTypeInterface-
foreach ($element->children() as $child) {
/** @var <?php echo $versionContainedTypeInterface->getFullyQualifiedName(true); ?> $class */
$class = <?php echo $versionTypeMapClass->getEntityName(); ?>::getContainedTypeClassNameFromXML($child);
$type->setContainedType($class::xmlUnserialize($child, null, $config));
$type->setContainedType($class::xmlUnserialize($child, $config));
break;
}
return $type;
Expand All @@ -137,8 +137,11 @@ public function xmlSerialize(null|<?php echo $xmlWriterClass->getEntityName(); ?
if (null !== $containedType) {
return $containedType->xmlSerialize($xw, $config);
}
if (null === $config) {
$config = (new <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION; ?>())->getConfig()->getSerializeConfig();
}
if (null === $xw) {
$xw = new <?php echo $xmlWriterClass->getEntityName(); ?>();
$xw = new <?php echo $xmlWriterClass->getEntityName(); ?>($config);
}
if (!$xw->isOpen()) {
$xw->openMemory();
Expand All @@ -147,12 +150,9 @@ public function xmlSerialize(null|<?php echo $xmlWriterClass->getEntityName(); ?
$docStarted = true;
$xw->startDocument();
}
if (null === $config) {
$config = (new <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION; ?>())->getConfig()->getSerializeConfig();
}
if (!$xw->isRootOpen()) {
$rootOpened = true;
$xw->openRootNode($config, '<?php echo NameUtils::getTypeXMLElementName($type); ?>', $this->_getSourceXMLNS());
$xw->openRootNode('<?php echo NameUtils::getTypeXMLElementName($type); ?>', $this->_getSourceXMLNS());
}
if (isset($rootOpened) && $rootOpened) {
$xw->endElement();
Expand All @@ -173,6 +173,10 @@ public function xmlSerialize(null|<?php echo $xmlWriterClass->getEntityName(); ?
]
);
?>
/** @var <?php echo $versionContainedTypeInterface->getFullyQualifiedName(true); ?> $class */
$class = <?php echo $versionTypeMapClass->getEntityName(); ?>::getContainedTypeClassNameFromArray($json);
$type->setContainedType($class::jsonUnserialize($json));
return $type;
}

public function __toString(): string
Expand Down
17 changes: 11 additions & 6 deletions template/versions/types/serialization/json/unserialize/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -38,15 +43,15 @@

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

* @throws \Exception
*/
public static function jsonUnserialize(<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|\stdClass|<?php endif; ?>array $json,
<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|<?php endif; echo $unserializeConfigClass->getEntityName() ?> $config<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = null<?php endif;?>,
public static function jsonUnserialize(<?php if ($isResource) : ?>string|\stdClass|<?php endif; ?>array $json,
<?php if ($isResource) : ?>null|<?php endif; echo $unserializeConfigClass->getEntityName() ?> $config<?php if ($isResource) : ?> = null<?php endif;?>,
null|<?php echo $typeInterface->getEntityName(); ?> $type = null): self
{
<?php if ($type->isAbstract()) : // abstract types may not be instantiated directly ?>
Expand All @@ -63,7 +68,7 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
get_class($type)
));
}
<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>
<?php if ($isResource) : ?>
if (null === $config) {
$config = (new <?php echo $versionClass->getEntityName(); ?>())->getConfig()->getUnserializeConfig();
}
Expand Down
17 changes: 11 additions & 6 deletions template/versions/types/serialization/xml/unserialize/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,15 +42,15 @@

ob_start(); ?>
/**
* @param <?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|<?php endif; ?>\SimpleXMLElement $element
* @param <?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|<?php endif; echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param <?php if ($isResource) : ?>string|<?php endif; ?>\SimpleXMLElement $element
* @param <?php if ($isResource) : ?>null|<?php endif; echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $config
* @param null|<?php echo $type->getFullyQualifiedClassName(true); ?> $type
* @return <?php echo $type->getFullyQualifiedClassName(true); ?>

* @throws \Exception
*/
public static function xmlUnserialize(<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>string|<?php endif; ?>\SimpleXMLElement $element,
<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>null|<?php endif; echo $unserializeConfigClass->getEntityName() ?> $config<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?> = null<?php endif;?>,
public static function xmlUnserialize(<?php if ($isResource) : ?>string|<?php endif; ?>\SimpleXMLElement $element,
<?php if ($isResource) : ?>null|<?php endif; echo $unserializeConfigClass->getEntityName() ?> $config<?php if ($isResource) : ?> = null<?php endif;?>,
null|<?php echo $typeInterface->getEntityName(); ?> $type = null): self
{
<?php if ($type->isAbstract()) : // abstract types may not be instantiated directly ?>
Expand All @@ -62,7 +67,7 @@ public static function xmlUnserialize(<?php if ($type->isResourceType() || $type
get_class($type)
));
}
<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>
<?php if ($isResource) : ?>
if (null === $config) {
$config = (new <?php echo $versionClass->getEntityName(); ?>())->getConfig()->getUnserializeConfig();
}
Expand Down

0 comments on commit 4b86dd2

Please sign in to comment.