From 6c3f7623828f91dd79e2022b71ddd5b53aaf96c5 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Thu, 30 Jan 2025 14:31:26 -0600 Subject: [PATCH] xml is fun. --- files/constants.php | 2 +- src/Version/Definition/Property.php | 4 +- .../class_resource_parser.php} | 71 +++++++++---------- .../versions/types/integration/class.php | 6 +- .../versions/core/class_version_client.php | 8 +-- .../serialization/json/unserialize/header.php | 2 +- 6 files changed, 43 insertions(+), 50 deletions(-) rename template/core/{class_response_parser.php => encoding/class_resource_parser.php} (65%) diff --git a/files/constants.php b/files/constants.php index abaf8cf0..0cd2dfbb 100644 --- a/files/constants.php +++ b/files/constants.php @@ -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'; @@ -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'; diff --git a/src/Version/Definition/Property.php b/src/Version/Definition/Property.php index a17eb3a8..8a914d9c 100644 --- a/src/Version/Definition/Property.php +++ b/src/Version/Definition/Property.php @@ -512,8 +512,8 @@ public function isSerializableAsXMLAttribute(): bool } return $propType->hasPrimitiveOrListParent() || $propType->isPrimitiveOrListType() - || $propType->isPrimitiveContainer() - || ($this->_memberOf->isValueContainer() && $this->isValueProperty()); + || $propType->hasValueContainerParent() + || $propType->isValueContainer(); } /** diff --git a/template/core/class_response_parser.php b/template/core/encoding/class_resource_parser.php similarity index 65% rename from template/core/class_response_parser.php rename to template/core/encoding/class_resource_parser.php index 706ed951..b3755466 100644 --- a/template/core/class_response_parser.php +++ b/template/core/encoding/class_resource_parser.php @@ -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, ); @@ -44,7 +44,7 @@ -class +class getEntityName(); ?> { private const XML_START = ['<']; @@ -55,12 +55,12 @@ class * * @param getFullyQualifiedName(true); ?> $version * @param null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> * @throws \Exception */ - public static function parse( $version, - null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null| + public static function parse(getEntityName(); ?> $version, + null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|getEntityName(); ?> { if (null === $input) { @@ -77,31 +77,31 @@ public static function parse( $version, /** * @param getFullyQualifiedName(true); ?> $version * @param array $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseArray( $version, - array $input): null| + public static function parseArray(getEntityName(); ?> $version, array $input): null|getEntityName(); ?> { if ([] === $input) { return null; } - if (isset($input[::JSON_FIELD_RESOURCE_TYPE])) { - $className = $version->getTypeMap()::getTypeClassName($input[::JSON_FIELD_RESOURCE_TYPE]); + if (isset($input[getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE])) { + /** @var getFullyQualifiedName(true); ?> $className */ + $className = $version->getTypeMap()::getTypeClassName($input[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"]', - ::JSON_FIELD_RESOURCE_TYPE, - $input[::JSON_FIELD_RESOURCE_TYPE], + getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE, + $input[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"]', - ::JSON_FIELD_RESOURCE_TYPE, + getEntityName(); ?>::JSON_FIELD_RESOURCE_TYPE, implode('","', array_keys($input)) )); } @@ -109,11 +109,10 @@ public static function parseArray( $vers /** * @param getFullyQualifiedName(true); ?> $version * @param \stdClass $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseStdClass( $version, - \stdClass $input): null| + public static function parseStdClass(getEntityName(); ?> $version, \stdClass $input): null|getEntityName(); ?> { return static::parseArray($version, (array)$input); @@ -122,15 +121,14 @@ public static function parseStdClass( $v /** * @param getFullyQualifiedName(true); ?> $version * @param \SimpleXMLElement $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseSimpleXMLElement( $version, - \SimpleXMLElement $input): null| + public static function parseSimpleXMLElement(getEntityName(); ?> $version, \SimpleXMLElement $input): null|getEntityName(); ?> { $elementName = $input->getName(); - /** @var getFullyQualifiedName(true, PHPFHIR_TYPES_INTERFACE_TYPE); ?> $fhirType */ + /** @var getFullyQualifiedName(true); ?> $fhirType */ $fhirType = $version->getTypeMap()::getTypeClassName($elementName); if (null === $fhirType) { throw new \UnexpectedValueException(sprintf( @@ -139,17 +137,16 @@ public static function parseSimpleXMLElement(saveXML()) )); } - return $fhirType::xmlUnserialize($input, null, $version->getConfig()->getUnserializeConfig()); + return $fhirType::xmlUnserialize($input, $version->getConfig()->getUnserializeConfig()); } /** * @param getFullyQualifiedName(true); ?> $version * @param \DOMDocument $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseDOMDocument( $version, - \DOMDocument $input): null| + public static function parseDOMDocument(getEntityName(); ?> $version, \DOMDocument $input): null|getEntityName(); ?> { return static::parseSimpleXMLElement($version, simplexml_import_dom($input)); @@ -158,11 +155,11 @@ public static function parseDOMDocument( /** * @param getFullyQualifiedName(true); ?> $version * @param \stdClass|\SimpleXMLElement|\DOMDocument $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseObject( $version, - \stdClass|\SimpleXMLElement|\DOMDocument $input): null| + public static function parseObject(getEntityName(); ?> $version, + \stdClass|\SimpleXMLElement|\DOMDocument $input): null|getEntityName(); ?> { if ($input instanceof \stdClass) { @@ -177,12 +174,11 @@ public static function parseObject( $ver /** * @param getFullyQualifiedName(true); ?> $version * @param string $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> * @throws \Exception */ - public static function parseXML( $version, - string $input): null| + public static function parseXML(getEntityName(); ?> $version, string $input): null|getEntityName(); ?> { return static::parseSimpleXMLElement( @@ -193,11 +189,10 @@ public static function parseXML( $versio /** * @param getFullyQualifiedName(true); ?> $version * @param string $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> */ - public static function parseJSON( $version, - string $input): null| + public static function parseJSON(getEntityName(); ?> $version, string $input): null|getEntityName(); ?> { $decoded = json_decode($input, true, $version->getConfig()->getUnserializeConfig()->getJSONDecodeMaxDepth()); @@ -216,15 +211,13 @@ public static function parseJSON( $versi /** * @param getFullyQualifiedName(true); ?> $version * @param string $input - * @return null|getFullyQualifiedName(true); ?> + * @return null|getFullyQualifiedName(true); ?> * @throws \Exception */ - public static function parseString( $version, - string $input): null| + public static function parseString(getEntityName(); ?> $version, string $input): null|getEntityName(); ?> { - $input = trim($input); if ('' === $input) { return null; } diff --git a/template/tests/versions/types/integration/class.php b/template/tests/versions/types/integration/class.php index 8902f476..6ee9c3e6 100644 --- a/template/tests/versions/types/integration/class.php +++ b/template/tests/versions/types/integration/class.php @@ -70,7 +70,7 @@ use getFullyQualifiedName(false, PHPFHIR_CLIENT_CLASSNAME_CLIENT); ?>; use getFullyQualifiedName(false, PHPFHIR_CLIENT_CLASSNAME_CONFIG); ?>; use getFullyQualifiedName(false, PHPFHIR_CLIENT_ENUM_RESPONSE_FORMAT); ?>; -use getFullyQualifiedName(false, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>; +use getFullyQualifiedName(false, PHPFHIR_ENCODING_CLASSNAME_RESOURCE_PARSER); ?>; use getFullyQualifiedClassName(false); ?>; use getFullyQualifiedClassName(false); ?>; use getFullyQualifiedName(false, PHPFHIR_VERSION_CLASSNAME_VERSION_CLIENT); ?>; @@ -375,7 +375,7 @@ public function testResponseParserXML(): void { $sourceXML = $this->fetchResourceBundle('xml'); try { - $bundle = ::parse($this->_version, $sourceXML); + $bundle = ::parse($this->_version, $sourceXML); } catch(\Exception $e) { throw new AssertionFailedError( sprintf( @@ -436,7 +436,7 @@ public function testResponseParserJSON(): void { $sourceJSON = $this->fetchResourceBundle('json'); try { - $bundle = ::parse($this->_version, $sourceJSON); + $bundle = ::parse($this->_version, $sourceJSON); } catch(\Exception $e) { throw new AssertionFailedError( sprintf( diff --git a/template/versions/core/class_version_client.php b/template/versions/core/class_version_client.php index 9edb94d2..be67e597 100644 --- a/template/versions/core/class_version_client.php +++ b/template/versions/core/class_version_client.php @@ -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( @@ -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(); @@ -195,7 +195,7 @@ public function read( $resource { $rc = $this->readRaw($resourceType, $resourceID, $count, $sort, $format, $queryParams, $parseResponseHeaders); $this->_requireOK($rc); - return ::parse($this->_version, $rc->resp); + return ::parse($this->_version, $rc->resp); } @@ -255,7 +255,7 @@ public function readOnegetFHIRName(); ?>(string|resp, config: $this->_version->getConfig()->getUnserializeConfig(), ), - default => ::parse($this->_version, $rc->resp), + default => ::parse($this->_version, $rc->resp), }; } diff --git a/template/versions/types/serialization/json/unserialize/header.php b/template/versions/types/serialization/json/unserialize/header.php index 6ee40986..4f21f7c9 100644 --- a/template/versions/types/serialization/json/unserialize/header.php +++ b/template/versions/types/serialization/json/unserialize/header.php @@ -70,7 +70,7 @@ public static function jsonUnserialize(isResourceType() || $typ $json = (array)$json; } hasConcreteParent()) : ?> - parent::jsonUnserialize($json, $type, $config);hasCommentContainerParent() && $type->isCommentContainer()) : ?> + parent::jsonUnserialize($json, $config, $type);hasCommentContainerParent() && $type->isCommentContainer()) : ?> if (isset($data[getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) { $type->_setFHIRComments((array)$data[getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]); }