Skip to content

Commit

Permalink
xml is fun.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 30, 2025
1 parent 62c0457 commit 6c3f762
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
// Core class names
const PHPFHIR_CLASSNAME_AUTOLOADER = 'Autoloader';
const PHPFHIR_CLASSNAME_VERSION_CONFIG = 'VersionConfig';
const PHPFHIR_CLASSNAME_RESPONSE_PARSER = 'ResponseParser';
const PHPFHIR_CLASSNAME_CONSTANTS = 'Constants';
const PHPFHIR_CLASSNAME_VALIDATOR = 'Validator';

Expand Down Expand Up @@ -123,6 +122,7 @@
const PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION = 'ValueXMLLocationEnum';
const PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
const PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';
const PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER = 'ResourceParser';

// Core client entities
const PHPFHIR_CLIENT_INTERFACE_CLIENT = 'ClientInterface';
Expand Down
4 changes: 2 additions & 2 deletions src/Version/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ public function isSerializableAsXMLAttribute(): bool
}
return $propType->hasPrimitiveOrListParent()
|| $propType->isPrimitiveOrListType()
|| $propType->isPrimitiveContainer()
|| ($this->_memberOf->isValueContainer() && $this->isValueProperty());
|| $propType->hasValueContainerParent()
|| $propType->isValueContainer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
$coreFiles = $config->getCoreFiles();

$versionInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_INTERFACE_VERSION);
$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE);
$resourceTypeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE);
$constantsClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_CONSTANTS);

$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$versionInterface,
$typeInterface,
$resourceTypeInterface,
$constantsClass,
);

Expand All @@ -44,7 +44,7 @@

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

class <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>
class <?php echo $coreFile->getEntityName(); ?>

{
private const XML_START = ['<'];
Expand All @@ -55,12 +55,12 @@ class <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>
*
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

* @throws \Exception
*/
public static function parse(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parse(<?php echo $versionInterface->getEntityName(); ?> $version,
null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
if (null === $input) {
Expand All @@ -77,43 +77,42 @@ public static function parse(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param array $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseArray(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
array $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseArray(<?php echo $versionInterface->getEntityName(); ?> $version, array $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
if ([] === $input) {
return null;
}
if (isset($input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE])) {
$className = $version->getTypeMap()::getTypeClassName($input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
if (isset($input[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE])) {
/** @var <?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?> $className */
$className = $version->getTypeMap()::getTypeClassName($input[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE]);
if (null === $className) {
throw new \UnexpectedValueException(sprintf(
'Provided input has "%s" value of "%s", but it does not map to any known type. Other keys: ["%s"]',
<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE,
$input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE],
<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE,
$input[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE],
implode('","', array_keys($input))
));
}
return $className::jsonUnserialize($input, null, $version->getConfig()->getUnserializeConfig());
return $className::jsonUnserialize($input, $version->getConfig()->getUnserializeConfig());
}
throw new \DomainException(sprintf(
'Unable to determine FHIR Type from provided array: missing "%s" key. Available keys: ["%s"]',
<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE,
<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE,
implode('","', array_keys($input))
));
}

/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param \stdClass $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseStdClass(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\stdClass $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseStdClass(<?php echo $versionInterface->getEntityName(); ?> $version, \stdClass $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
return static::parseArray($version, (array)$input);
Expand All @@ -122,15 +121,14 @@ public static function parseStdClass(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $v
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param \SimpleXMLElement $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseSimpleXMLElement(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\SimpleXMLElement $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseSimpleXMLElement(<?php echo $versionInterface->getEntityName(); ?> $version, \SimpleXMLElement $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
$elementName = $input->getName();
/** @var <?php echo $config->getFullyQualifiedName(true, PHPFHIR_TYPES_INTERFACE_TYPE); ?> $fhirType */
/** @var <?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?> $fhirType */
$fhirType = $version->getTypeMap()::getTypeClassName($elementName);
if (null === $fhirType) {
throw new \UnexpectedValueException(sprintf(
Expand All @@ -139,17 +137,16 @@ public static function parseSimpleXMLElement(<?php echo PHPFHIR_INTERFACE_VERSIO
static::getPrintableStringInput($input->saveXML())
));
}
return $fhirType::xmlUnserialize($input, null, $version->getConfig()->getUnserializeConfig());
return $fhirType::xmlUnserialize($input, $version->getConfig()->getUnserializeConfig());
}

/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param \DOMDocument $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseDOMDocument(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseDOMDocument(<?php echo $versionInterface->getEntityName(); ?> $version, \DOMDocument $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
return static::parseSimpleXMLElement($version, simplexml_import_dom($input));
Expand All @@ -158,11 +155,11 @@ public static function parseDOMDocument(<?php echo PHPFHIR_INTERFACE_VERSION; ?>
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param \stdClass|\SimpleXMLElement|\DOMDocument $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseObject(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseObject(<?php echo $versionInterface->getEntityName(); ?> $version,
\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
if ($input instanceof \stdClass) {
Expand All @@ -177,12 +174,11 @@ public static function parseObject(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $ver
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param string $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

* @throws \Exception
*/
public static function parseXML(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseXML(<?php echo $versionInterface->getEntityName(); ?> $version, string $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
return static::parseSimpleXMLElement(
Expand All @@ -193,11 +189,10 @@ public static function parseXML(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $versio
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param string $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

*/
public static function parseJSON(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseJSON(<?php echo $versionInterface->getEntityName(); ?> $version, string $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
$decoded = json_decode($input, true, $version->getConfig()->getUnserializeConfig()->getJSONDecodeMaxDepth());
Expand All @@ -216,15 +211,13 @@ public static function parseJSON(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $versi
/**
* @param <?php echo $versionInterface->getFullyQualifiedName(true); ?> $version
* @param string $input
* @return null|<?php echo $typeInterface->getFullyQualifiedName(true); ?>
* @return null|<?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?>

* @throws \Exception
*/
public static function parseString(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>
public static function parseString(<?php echo $versionInterface->getEntityName(); ?> $version, string $input): null|<?php echo $resourceTypeInterface->getEntityName(); ?>

{
$input = trim($input);
if ('' === $input) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions template/tests/versions/types/integration/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLIENT_CLASSNAME_CLIENT); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLIENT_CLASSNAME_CONFIG); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLIENT_ENUM_RESPONSE_FORMAT); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER); ?>;
use <?php echo $bundleType->getFullyQualifiedClassName(false); ?>;
use <?php echo $type->getFullyQualifiedClassName(false); ?>;
use <?php echo $version->getFullyQualifiedName(false, PHPFHIR_VERSION_CLASSNAME_VERSION_CLIENT); ?>;
Expand Down Expand Up @@ -375,7 +375,7 @@ public function testResponseParserXML(): void
{
$sourceXML = $this->fetchResourceBundle('xml');
try {
$bundle = <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>::parse($this->_version, $sourceXML);
$bundle = <?php echo PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER; ?>::parse($this->_version, $sourceXML);
} catch(\Exception $e) {
throw new AssertionFailedError(
sprintf(
Expand Down Expand Up @@ -436,7 +436,7 @@ public function testResponseParserJSON(): void
{
$sourceJSON = $this->fetchResourceBundle('json');
try {
$bundle = <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>::parse($this->_version, $sourceJSON);
$bundle = <?php echo PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER; ?>::parse($this->_version, $sourceJSON);
} catch(\Exception $e) {
throw new AssertionFailedError(
sprintf(
Expand Down
8 changes: 4 additions & 4 deletions template/versions/core/class_version_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
PHPFHIR_CLIENT_CLASSNAME_RESPONSE,
PHPFHIR_EXCEPTION_CLIENT_ERROR,
PHPFHIR_EXCEPTION_CLIENT_UNEXPECTED_RESPONSE_CODE,
PHPFHIR_CLASSNAME_RESPONSE_PARSER,
PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER,
PHPFHIR_TYPES_INTERFACE_TYPE,
)
->addVersionTypeImports(
Expand Down Expand Up @@ -67,7 +67,7 @@
$clientResponseClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLIENT_CLASSNAME_RESPONSE);
$clientErrorException = $coreFiles->getCoreFileByEntityName(PHPFHIR_EXCEPTION_CLIENT_ERROR);
$clientUnexpectedResponseCodeException = $coreFiles->getCoreFileByEntityName(PHPFHIR_EXCEPTION_CLIENT_UNEXPECTED_RESPONSE_CODE);
$responseParserClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_RESPONSE_PARSER);
$responseParserClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER);
$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE);

$versionCoreFiles = $version->getCoreFiles();
Expand Down Expand Up @@ -195,7 +195,7 @@ public function read(<?php echo PHPFHIR_VERSION_ENUM_VERSION_TYPES; ?> $resource
{
$rc = $this->readRaw($resourceType, $resourceID, $count, $sort, $format, $queryParams, $parseResponseHeaders);
$this->_requireOK($rc);
return <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>::parse($this->_version, $rc->resp);
return <?php echo PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER; ?>::parse($this->_version, $rc->resp);
}

<?php endif; ?>
Expand Down Expand Up @@ -255,7 +255,7 @@ public function readOne<?php echo $rsc->getFHIRName(); ?>(string|<?php echo $idT
element: $rc->resp,
config: $this->_version->getConfig()->getUnserializeConfig(),
),
default => <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>::parse($this->_version, $rc->resp),
default => <?php echo PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER; ?>::parse($this->_version, $rc->resp),
};
}
<?php endforeach; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
$json = (array)$json;
}
<?php if ($type->hasConcreteParent()) : ?>
parent::jsonUnserialize($json, $type, $config);<?php elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
parent::jsonUnserialize($json, $config, $type);<?php elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
if (isset($data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) {
$type->_setFHIRComments((array)$data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]);
}
Expand Down

0 comments on commit 6c3f762

Please sign in to comment.