From 0b72b943b73534267d393fee60640e129a2bb179 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Thu, 25 Apr 2024 08:37:45 -0500 Subject: [PATCH] DOMElement -> DOMNode, few cleanup items --- .../interfaces/interface_xml_serializable.php | 10 ++--- template/types/class_xhtml.php | 19 +++++++-- .../serialization/xml/serialize/header.php | 6 +-- .../xml/serialize/resource_container.php | 6 +-- .../serialization/xml/unserialize/header.php | 10 +++-- .../integration/body_domain_resource.php | 4 +- .../types/tests/integration/class_default.php | 4 +- template/types/validation/methods.php | 41 ++++++++++--------- 8 files changed, 58 insertions(+), 42 deletions(-) diff --git a/template/core/interfaces/interface_xml_serializable.php b/template/core/interfaces/interface_xml_serializable.php index 8c68e693..bcc203b6 100644 --- a/template/core/interfaces/interface_xml_serializable.php +++ b/template/core/interfaces/interface_xml_serializable.php @@ -66,18 +66,18 @@ public function _setFHIRXMLNamespace(string $xmlNamespace): self; public function _getFHIRXMLElementDefinition(string $elementName): string; /** - * @param null|string|\DOMElement $element + * @param null|string|\DOMNode $element * @param null|static $type * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. * @return null|static */ - public static function xmlUnserialize(null|string|\DOMElement $element, $type = null, null|int| $config = null): null|self; + public static function xmlUnserialize(null|string|\DOMNode $element, $type = null, null|int| $config = null): null|self; /** - * @param null|\DOMElement $element + * @param null|\DOMNode $element * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. - * @return \DOMElement + * @return \DOMNode */ - public function xmlSerialize(null|\DOMElement $element = null, null|int| $config = null): \DOMElement; + public function xmlSerialize(null|\DOMNode $element = null, null|int| $config = null): \DOMNode; } getClassName(); ?> implements , ; + const _NOISE_NODES = ['html', 'head', 'body']; + /** @var null|\DOMNode */ private null|\DOMNode $_node = null; @@ -97,7 +99,14 @@ public function setNode(null|string|\DOMNode $node): self $dom->appendChild($dom->importNode($node, true)); } $newNode = $dom->documentElement; - if ('' !== ($ens = (string)$newNode->namespaceURI)) { + while (null !== $newNode) { + if (in_array(strtolower($newNode->nodeName), self::_NOISE_NODES, true)) { + $newNode = $newNode->firstChild; + } else { + break; + } + } + if ('' !== ($ens = (string)$newNode?->namespaceURI)) { $this->_setFHIRXMLNamespace($ens); } $this->_trackValueSet($this->_node, $newNode); @@ -123,11 +132,11 @@ public function setNode(null|string|\DOMNode $node): self } /** - * @param \DOMElement|null $element + * @param \DOMNode|null $element * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. - * @return \DOMElement + * @return \DOMNode */ - public function xmlSerialize(\DOMElement $element = null, null|int| $config = null): \DOMElement + public function xmlSerialize(\DOMNode $element = null, null|int| $config = null): \DOMNode { if (is_int($config)) { $libxmlOpts = $config; @@ -139,6 +148,8 @@ public function xmlSerialize(\DOMElement $element = null, null|int|loadXML($this->_getFHIRXMLElementDefinition(''), $libxmlOpts); $element = $dom->documentElement; + } else if ('' !== ($ns = $this->_getFHIRXMLNamespace())) { + $element->setAttribute('xmlns', $ns); } $node = $this->getNode(); if (null === $node) { diff --git a/template/types/serialization/xml/serialize/header.php b/template/types/serialization/xml/serialize/header.php index 847535f9..c95381df 100644 --- a/template/types/serialization/xml/serialize/header.php +++ b/template/types/serialization/xml/serialize/header.php @@ -29,14 +29,14 @@ ob_start(); ?> /** - * @param null|\DOMElement $element + * @param null|\DOMNode $element * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. - * @return \DOMElement + * @return \DOMNode * @throws \DOMException */ - public function xmlSerialize(\DOMElement $element = null, null|int| $config = null): \DOMElement + public function xmlSerialize(\DOMNode $element = null, null|int| $config = null): \DOMNode { if (is_int($config)) { $libxmlOpts = $config; diff --git a/template/types/serialization/xml/serialize/resource_container.php b/template/types/serialization/xml/serialize/resource_container.php index 7e23ef25..3720a150 100644 --- a/template/types/serialization/xml/serialize/resource_container.php +++ b/template/types/serialization/xml/serialize/resource_container.php @@ -26,11 +26,11 @@ ob_start(); ?> /** - * @param null|\DOMElement $element + * @param null|\DOMNode $element * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. - * @return \DOMElement + * @return \DOMNode */ - public function xmlSerialize(null|\DOMElement $element = null, null|int| $config = null): \DOMElement + public function xmlSerialize(null|\DOMNode $element = null, null|int| $config = null): \DOMNode { if (is_int($config)) { $libxmlOpts = $config; diff --git a/template/types/serialization/xml/unserialize/header.php b/template/types/serialization/xml/unserialize/header.php index a60c93df..6ca1b265 100644 --- a/template/types/serialization/xml/unserialize/header.php +++ b/template/types/serialization/xml/unserialize/header.php @@ -27,13 +27,13 @@ ob_start(); ?> /** - * @param null|string|\DOMElement $element + * @param null|string|\DOMNode $element * @param null|getFullyQualifiedClassName(true); ?> $type * @param null|int|\ $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility. * @return null|getFullyQualifiedClassName(true); ?> */ - public static function xmlUnserialize(null|string|\DOMElement $element, null| $type = null, null|int| $config = null): null|self + public static function xmlUnserialize(null|string|\DOMNode $element, null| $type = null, null|int| $config = null): null|self { if (null === $element) { return null; @@ -58,7 +58,8 @@ public static function xmlUnserialize(null|string|\DOMElement $element, null|::xmlUnserialize: Cannot unserialize directly into root type'); } else if (!($type instanceof )) { throw new \RuntimeException(sprintf( - '::xmlUnserialize - $type must be child instance of getFullyQualifiedClassName(true); ?> or null, %s seen.', + '::xmlUnserialize - $type must be child instance of %s or null, %s seen.', + static::class, get_class($type) )); } @@ -67,7 +68,8 @@ public static function xmlUnserialize(null|string|\DOMElement $element, null|(null); } else if (!($type instanceof )) { throw new \RuntimeException(sprintf( - '::xmlUnserialize - $type must be instance of getFullyQualifiedClassName(true); ?> or null, %s seen.', + '::xmlUnserialize - $type must be instance of %s or null, %s seen.', + static::class, get_class($type) )); } diff --git a/template/types/tests/integration/body_domain_resource.php b/template/types/tests/integration/body_domain_resource.php index bfff072a..a3c945a9 100644 --- a/template/types/tests/integration/body_domain_resource.php +++ b/template/types/tests/integration/body_domain_resource.php @@ -139,7 +139,7 @@ public function testXML(): void $e ); } - $this->assertInstanceOf('getFullyQualifiedClassName(true); ?>', $bundle); + $this->assertInstanceOf(getClassName(); ?>::class, $bundle); $entry = $bundle->getEntry(); isCollection()) : ?> if (0 === count($entry)) { @@ -172,7 +172,7 @@ public function testXML(): void $e ); } - $this->assertInstanceOf('getFullyQualifiedClassName(true); ?>', $type); + $this->assertInstanceOf(getClassName(); ?>::class, $type); $typeElement = $type->xmlSerialize(); $this->assertEquals($resourceXML, $typeElement->ownerDocument->saveXML($typeElement)); $bundleElement = $bundle->xmlSerialize(); diff --git a/template/types/tests/integration/class_default.php b/template/types/tests/integration/class_default.php index ddd27957..d42c1c2c 100644 --- a/template/types/tests/integration/class_default.php +++ b/template/types/tests/integration/class_default.php @@ -54,7 +54,7 @@ ob_start(); -if ($type->isDomainResource()) : +if ($type->isDomainResource()) { echo require_with( PHPFHIR_TEMPLATE_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR . 'header_domain_resource.php', [ @@ -73,7 +73,7 @@ 'bundleEntryProperty' => $bundleEntryProperty, ] ); -endif; +} echo "}\n"; return ob_get_clean(); diff --git a/template/types/validation/methods.php b/template/types/validation/methods.php index 942ce5cd..0082211f 100644 --- a/template/types/validation/methods.php +++ b/template/types/validation/methods.php @@ -19,6 +19,8 @@ /** @var \DCarbone\PHPFHIR\Config\VersionConfig $config */ /** @var \DCarbone\PHPFHIR\Definition\Type $type */ +use DCarbone\PHPFHIR\Enum\TypeKind; + $typeNameConst = $type->getTypeNameConst(true); $typeKind = $type->getKind(); @@ -54,34 +56,35 @@ public function _getValidationErrors(): array $errs = []; $validationRules = $this->_getValidationRules(); -getLocalProperties()->localPropertiesIterator() as $property) : +getLocalProperties()->localPropertiesIterator() as $property) { $propertyType = $property->getValueFHIRType(); - if (null === $propertyType) : - if ($property->isCollection()) : + if (null === $propertyType) { + if ($property->isCollection()) { echo require_with( PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'collection_typed.php', $requireArgs + ['property' => $property] ); - else : + } else { echo require_with( PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'primitive.php', $requireArgs + ['property' => $property] ); - endif; - else : - if ($property->isCollection()) : - echo require_with( - PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'collection_typed.php', - $requireArgs + ['property' => $property] - ); - else : - echo require_with( - PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'typed.php', - $requireArgs + ['property' => $property] - ); - endif; - endif; -endforeach; + } + } else if ($propertyType->getKind() === TypeKind::PHPFHIR_XHTML) { + // TODO(@dcarbone): better way to omit validation + continue; + } else if ($property->isCollection()) { + echo require_with( + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'collection_typed.php', + $requireArgs + ['property' => $property] + ); + } else { + echo require_with( + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'typed.php', + $requireArgs + ['property' => $property] + ); + } +} if (null !== $type->getParentType()) : $ptype = $type; while (null !== $ptype) :