Skip to content

Commit

Permalink
dstu1 hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Feb 1, 2025
1 parent 14672fa commit bfc43d2
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 78 deletions.
1 change: 1 addition & 0 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 8 additions & 2 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
);
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/NameUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class NameUtils
'xml' => 'XML',
'http' => 'HTTP',
'curl' => 'CURL',
'dstu1' => 'DSTU1',
];

/** @var array */
Expand Down
26 changes: 21 additions & 5 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -858,17 +868,23 @@ 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()) {
$interfaces[PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE] = $coreFiles
->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);
Expand Down Expand Up @@ -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);
Expand Down
71 changes: 71 additions & 0 deletions template/core/types/interface_dstu1_primitive_container_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

/*
* 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\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$coreFiles = $config->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 '<?php ';?>declare(strict_types=1);

namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

<?php echo ImportUtils::compileImportStatements($imports); ?>

interface <?php echo $coreFile->getEntityName(); ?> extends <?php echo $valueContainerInterface->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|<?php echo $xmlWriterClass->getFullyQualifiedName(true); ?> $xw
* @param null|<?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|<?php echo $serializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $xmlValueLocationEnum->getEntityName(); ?> $valueLocation = null): <?php echo $xmlWriterClass->getEntityName() ?>;
}
<?php return ob_get_clean();
46 changes: 4 additions & 42 deletions template/core/types/interface_dstu1_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@

$coreFiles = $config->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();
Expand All @@ -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 <?php echo $coreFile->getEntityName(); ?> extends <?php echo $typeInterface->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|<?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 $unserializeConfigClass->getEntityName() ?> $config = null,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;

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

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

/**
* @param string|array|\stdClass $json Raw or already un-encoded JSON
* @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 $unserializeConfigClass->getEntityName(); ?> $config = null,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;
}

<?php return ob_get_clean();
1 change: 0 additions & 1 deletion template/core/types/interface_element_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ public static function jsonUnserialize(array $json,
<?php echo $unserializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;
}

<?php return ob_get_clean();
1 change: 0 additions & 1 deletion template/core/types/interface_primitive_container_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public function xmlSerialize(<?php echo $xmlWriterClass->getEntityName(); ?> $xw
<?php echo $serializeConfigClass->getEntityName(); ?> $config,
null|<?php echo $xmlValueLocationEnum->getEntityName(); ?> $valueLocation = null): void;
}

<?php return ob_get_clean();
1 change: 0 additions & 1 deletion template/core/types/interface_resource_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,4 @@ public static function jsonUnserialize(string|array|\stdClass $json,
null|<?php echo $unserializeConfigClass->getEntityName(); ?> $config = null,
null|<?php echo $coreFile->getEntityName(); ?> $type = null): self;
}

<?php return ob_get_clean();
1 change: 0 additions & 1 deletion template/core/types/interface_value_container_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ interface <?php echo $coreFile->getEntityName(); ?>
*/
public function _getFormattedValue(): string;
}

<?php return ob_get_clean();
3 changes: 1 addition & 2 deletions template/core/types/trait_comment_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,4 @@ public function _addFHIRComment(string $fhirComment): self
return $this;
}
}
<?php
return ob_get_clean();
<?php return ob_get_clean();
2 changes: 1 addition & 1 deletion template/versions/types/class_resource_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function setContainedType(null|<?php echo $versionContainedTypeInterface-

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

{
$containedType = $this->getContainedType();
Expand Down
3 changes: 1 addition & 2 deletions template/versions/types/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@
{<?php if ([] !== $traits) : ?>

use <?php echo implode(",\n ", array_keys($traits)); ?>;
<?php endif; ?>


<?php endif; ?>
// name of FHIR type this class describes
public const FHIR_TYPE_NAME = <?php echo $type->getTypeNameConst(true); ?>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,15 +38,15 @@

ob_start(); ?>
/**
* @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 <?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 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()) : ?>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;?>,
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;?>,
null|<?php echo $typeInterface->getEntityName(); ?> $type = null): self
{
<?php if ($type->isAbstract()) : // abstract types may not be instantiated directly ?>
Expand All @@ -61,7 +63,7 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
get_class($type)
));
}
<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>
<?php if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>
if (null === $config) {
$config = (new <?php echo $versionClass->getEntityName(); ?>())->getConfig()->getUnserializeConfig();
}
Expand Down
4 changes: 3 additions & 1 deletion template/versions/types/serialization/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/** @var \DCarbone\PHPFHIR\Version $version */
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type; */

$sourceMeta = $version->getSourceMetadata();

$typeKind = $type->getKind();

ob_start();
Expand Down Expand Up @@ -66,7 +68,7 @@
]
);

if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>
if ($type->isResourceType() || $type->hasResourceTypeParent() || $sourceMeta->isDSTU1()) : ?>
if (isset($rootOpened) && $rootOpened) {
$xw->endElement();
}
Expand Down
Loading

0 comments on commit bfc43d2

Please sign in to comment.