Skip to content

Commit

Permalink
simplfying templates a bit, adding ID access for resources in client,…
Browse files Browse the repository at this point in the history
… few other small fixes
  • Loading branch information
dcarbone committed Dec 30, 2024
1 parent 368b742 commit 25bbc95
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 275 deletions.
2 changes: 1 addition & 1 deletion src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function getAllPropertiesIterator(): iterable
}
}
}
return new \ArrayIterator($properties);
return \SplFixedArray::fromArray($properties, preserveKeys: false);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Version/Definition/TypeImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,13 @@ private function buildImports(): void
$pns = $parentType->getFullyQualifiedNamespace(false);
$this->addImport($parentType->getClassName(), $pns);
} else {
$this->addImport(PHPFHIR_CLASSNAME_VALIDATOR, $configNS);
$this->addImport(PHPFHIR_TRAIT_SOURCE_XMLNS, $configNS);
}

if ($this->type->hasLocalPropertiesWithValidations()) {
$this->addImport(PHPFHIR_CLASSNAME_VALIDATOR, $configNS);
}

// determine if we need to import a restriction base
if ($restrictionBaseType = $this->type->getRestrictionBaseFHIRType()) {
$rns = $restrictionBaseType->getFullyQualifiedNamespace(false);
Expand Down
47 changes: 38 additions & 9 deletions template/versions/core/classes/class_version_api_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

/** @var \DCarbone\PHPFHIR\Version $version */

use DCarbone\PHPFHIR\Enum\TypeKindEnum;

$config = $version->getConfig();
$types = $version->getDefinition()->getTypes();
$namespace = $version->getFullyQualifiedName(false);
$bundleType = $types->getBundleType();

$idType = $types->getTypeByName('id');
$idPrimitiveType = $types->getTypeByName('id-primitive');

ob_start();
echo '<?php'; ?> declare(strict_types=1);

Expand All @@ -40,6 +45,7 @@
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_ENUM_API_SORT); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_INTERFACE_TYPE); ?>;
use <?php echo $idType->getFullyQualifiedClassName(false); ?>;

class <?php echo PHPFHIR_CLASSNAME_VERSION_API_CLIENT; ?>

Expand Down Expand Up @@ -70,7 +76,8 @@ public function __construct(<?php echo PHPFHIR_INTERFACE_API_CLIENT; ?> $client,
* @see https://www.hl7.org/fhir/http.html#read
*
* @param string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourceType
* @param int $count
* @param null|string|<?php echo $idType->getFullyQualifiedClassName(true); ?>|<?php echo $idPrimitiveType->getFullyQualifiedClassName(true); ?> $resourceID
* @param null|int $count
* @param null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort
* @param null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format
* @param null|bool $parseheaders
Expand All @@ -79,7 +86,8 @@ public function __construct(<?php echo PHPFHIR_INTERFACE_API_CLIENT; ?> $client,
* @throws \Exception
*/
public function readRaw(string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourceType,
int $count = 1,
null|string|<?php echo $idType->getClassName(); ?>|<?php echo $idPrimitiveType->getClassName(); ?> $resourceID = null,
null|int $count = null,
null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort = null,
null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format = null,
null|bool $parseheaders = null): <?php echo PHPFHIR_CLASSNAME_API_CLIENT_RESPONSE; ?>
Expand All @@ -92,7 +100,12 @@ public function readRaw(string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourc
$req = new <?php echo PHPFHIR_CLASSNAME_API_CLIENT_REQUEST; ?>();
$req->method = 'GET';
$req->path = "/{$resourceType}";
$req->count = $count;
if (null !== $resourceID) {
$req->path .= "/{$resourceID}";
}
if (null !== $count) {
$req->count = $count;
}
if (null !== $sort) {
$req->sort = $sort;
}
Expand All @@ -112,7 +125,8 @@ public function readRaw(string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourc
* @see https://www.hl7.org/fhir/http.html#read
*
* @param string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourceType
* @param int $count
* @param null|string|<?php echo $idType->getFullyQualifiedClassName(true); ?>|<?php echo $idPrimitiveType->getFullyQualifiedClassName(true); ?> $resourceID
* @param null|int $count
* @param null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort
* @param null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format
* @param null|bool $parseheaders
Expand All @@ -121,13 +135,14 @@ public function readRaw(string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourc
* @throws \Exception
*/
public function read(string|<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?> $resourceType,
int $count = 1,
null|string|<?php echo $idType->getClassName(); ?>|<?php echo $idPrimitiveType->getClassName(); ?> $resourceID = null,
null|int $count = null,
null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort = null,
null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format = null,
null|bool $parseheaders = null): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>

{
$rc = $this->readRaw($resourceType, $count, $sort, $format, $parseheaders);
$rc = $this->readRaw($resourceType, $resourceID, $count, $sort, $format, $parseheaders);
$this->_requireOK($rc);
return <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>::parse($this->_version, $rc->resp);
}
Expand All @@ -148,12 +163,26 @@ protected function _requireOK(<?php echo PHPFHIR_CLASSNAME_API_CLIENT_RESPONSE;
throw new <?php echo PHPFHIR_EXCEPTION_API_UNEXPECTED_RESPONSE_CODE; ?>($rc, self::_STATUS_OK);
}
}
<?php foreach($types->getChildrenOf('Resource') as $rsc) : if ($rsc->isAbstract()) { continue; } ?>
<?php foreach($types->getChildrenOf('Resource') as $rsc) :
$typeKind = $rsc->getKind();
if ($rsc->isRootType() || $typeKind->isContainer($version) || $typeKind->isOneOf(TypeKindEnum::RESOURCE_COMPONENT)) { continue; } ?>

public function read<?php echo $rsc->getFHIRName(); ?>Raw(int $count = 1, null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort = null, null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format = null, null|bool $parseheaders = null): <?php echo PHPFHIR_CLASSNAME_API_CLIENT_RESPONSE; ?>
/**
* Query for one or more <?php echo $rsc->getFHIRName(); ?> resources.
*
* @param null|string|<?php echo $idType->getFullyQualifiedClassName(true); ?>|<?php echo $idPrimitiveType->getFullyQualifiedClassName(true); ?> $resourceID
* @param null|int $count
* @param null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort
* @param null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format
* @param null|bool $parseheaders
* @return <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_API_CLIENT_RESPONSE); ?>

* @throws \Exception
*/
public function read<?php echo $rsc->getFHIRName(); ?>Raw(null|string|<?php echo $idType->getClassName(); ?>|<?php echo $idPrimitiveType->getClassName(); ?> $resourceID = null, null|int $count = null, null|<?php echo PHPFHIR_ENUM_API_SORT; ?> $sort = null, null|<?php echo PHPFHIR_ENUM_API_FORMAT; ?> $format = null, null|bool $parseheaders = null): <?php echo PHPFHIR_CLASSNAME_API_CLIENT_RESPONSE; ?>

{
return $this->readRaw(<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?>::<?php echo $rsc->getConstName(false); ?>, $count, $sort, $format, $parseheaders);
return $this->readRaw(<?php echo PHPFHIR_ENUM_VERSION_TYPE; ?>::<?php echo $rsc->getConstName(false); ?>, $resourceID, $count, $sort, $format, $parseheaders);
}
<?php endforeach; ?>

Expand Down
2 changes: 0 additions & 2 deletions template/versions/types/serialization/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
]
);

// when unserializing, each type handles _every_ field directly. this includes fields inherited from parents.

if (0 < count($properties)) :
echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'unserialize' . DIRECTORY_SEPARATOR . 'body.php',
Expand Down
103 changes: 65 additions & 38 deletions template/versions/types/serialization/xml/unserialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,79 @@
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */
/** @var \DCarbone\PHPFHIR\Version\Definition\Property[] $properties */

use DCarbone\PHPFHIR\Enum\TypeKindEnum;

$requireArgs = [
'version' => $version,
];

ob_start(); ?>
foreach ($element->children() as $n) {
$childName = $n->getName();
<?php foreach ($properties as $i => $property) {
if (null !== $property->getValueFHIRType()) {
echo require_with(
__DIR__ . DIRECTORY_SEPARATOR . 'body_parse_node_typed.php',
$requireArgs + [
'property' => $property,
'i' => $i,
]
);
} else {
echo require_with(
__DIR__ . DIRECTORY_SEPARATOR . 'body_parse_node_primitive.php',
$requireArgs + [
'property' => $property,
'i' => $i,
]
);
}
}; ?>
<?php foreach ($properties as $i => $property) :
$propConst = $property->getFieldConstantName();
$propType = $property->getValueFHIRType();
$setter = $property->getSetterName();

if ($i > 0) : ?> else<?php else : ?> <?php endif;
if (null !== $propType) :
$propTypeKind = $propType->getKind();
$propTypeClassname = $property->getMemberOf()->getImports()->getImportByType($propType);

if ($propTypeKind->isContainer($version)) : ?>
if (self::<?php echo $propConst; ?> === $childName) {
foreach ($n->children() as $nn) {
$typeClassName = <?php echo PHPFHIR_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromXML($nn);
$type-><?php echo $setter; ?>($typeClassName::xmlUnserialize($nn, null, $config));
}
}<?php
else : ?>if (self::<?php echo $propConst; ?> === $childName) {
$type-><?php echo $setter; ?>(<?php echo $propTypeClassname; ?>::xmlUnserialize($n, null, $config)<?php if ($propType->hasPrimitiveParent() || $propType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) : ?>, <?php echo PHPFHIR_ENUM_XML_LOCATION; ?>::ELEMENT<?php endif; ?>);
}<?php
endif;
else : ?>if (self::<?php echo $propConst; ?> === $childName) {
$valueAttr = $n->attributes()[self::FIELD_VALUE] ?? null;
if (null !== $valueAttr) {
$type->setValue((string)$valueAttr);
} elseif ($n->hasChildren()) {
$type->setValue($n->saveXML());
} else {
$type->setValue((string)$n);
}
}<?php
endif;
endforeach; ?>

}
$attributes = $element->attributes();
<?php foreach ($properties as $i => $property) {
if (null !== $property->getValueFHIRType()) {
echo require_with(
__DIR__ . DIRECTORY_SEPARATOR . 'body_parse_attr_typed.php',
$requireArgs + [
'property' => $property,
'i' => $i,
]
);
} else {
echo require_with(
__DIR__ . DIRECTORY_SEPARATOR . 'body_parse_attr_primitive.php',
$requireArgs + [
'property' => $property,
'i' => $i,
]
);
}
}
<?php foreach ($properties as $i => $property) :
$propConst = $property->getFieldConstantName();
$propType = $property->getValueFHIRType();
$setter = $property->getSetterName();

if (null !== $propType) :
$propTypeKind = $propType->getKind();
$propTypeClassname = $property->getMemberOf()->getImports()->getImportByType($propType);

if ($propType->hasPrimitiveParent() || $propType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) : ?>
if (isset($attributes[self::<?php echo $propConst; ?>])) {
<?php if ($property->isCollection()) : ?>
$type-><?php echo $setter; ?>((string)$attributes[self::<?php echo $propConst; ?>], <?php echo PHPFHIR_ENUM_XML_LOCATION; ?>::ATTRIBUTE);
<?php else : ?>
$pt = $type-><?php echo $property->getGetterName(); ?>();
if (null !== $pt) {
$pt->setValue((string)$attributes[self::<?php echo $propConst; ?>], <?php echo PHPFHIR_ENUM_XML_LOCATION; ?>::ATTRIBUTE);
} else {
$type-><?php echo $setter; ?>((string)$attributes[self::<?php echo $propConst; ?>], <?php echo PHPFHIR_ENUM_XML_LOCATION; ?>::ATTRIBUTE);
}
<?php endif; ?>
}
<?php endif;
else : ?>
if (isset($attributes[self::<?php echo $property->getFieldConstantName(); ?>])) {
$type->setValue((string)$attributes[self::<?php echo $property->getFieldConstantName(); ?>], <?php echo PHPFHIR_ENUM_XML_LOCATION; ?>::ATTRIBUTE);
}
<?php
endif;
endforeach;
return ob_get_clean();

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 25bbc95

Please sign in to comment.