Skip to content

Commit

Permalink
DOMElement -> DOMNode, few cleanup items
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Apr 25, 2024
1 parent 0fa01f6 commit 0b72b94
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 42 deletions.
10 changes: 5 additions & 5 deletions template/core/interfaces/interface_xml_serializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $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, <?php echo PHPFHIR_INTERFACE_XML_SERIALIZABLE; ?> $type = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): null|self;
public static function xmlUnserialize(null|string|\DOMNode $element, <?php echo PHPFHIR_INTERFACE_XML_SERIALIZABLE; ?> $type = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): null|self;

/**
* @param null|\DOMElement $element
* @param null|\DOMNode $element
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $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|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMElement;
public function xmlSerialize(null|\DOMNode $element = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMNode;
}
<?php return ob_get_clean();
19 changes: 15 additions & 4 deletions template/types/class_xhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class <?php echo $type->getClassName(); ?> implements <?php echo PHPFHIR_INTERFA
use <?php echo PHPFHIR_TRAIT_CHANGE_TRACKING; ?>,
<?php echo PHPFHIR_TRAIT_XMLNS; ?>;

const _NOISE_NODES = ['html', 'head', 'body'];

/** @var null|\DOMNode */
private null|\DOMNode $_node = null;

Expand Down Expand Up @@ -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);
Expand All @@ -123,11 +132,11 @@ public function setNode(null|string|\DOMNode $node): self
}

/**
* @param \DOMElement|null $element
* @param \DOMNode|null $element
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $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|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMElement
public function xmlSerialize(\DOMNode $element = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMNode
{
if (is_int($config)) {
$libxmlOpts = $config;
Expand All @@ -139,6 +148,8 @@ public function xmlSerialize(\DOMElement $element = null, null|int|<?php echo PH
$dom = new \DOMDocument();
$dom->loadXML($this->_getFHIRXMLElementDefinition('<?php echo $xmlName; ?>'), $libxmlOpts);
$element = $dom->documentElement;
} else if ('' !== ($ns = $this->_getFHIRXMLNamespace())) {
$element->setAttribute('xmlns', $ns);
}
$node = $this->getNode();
if (null === $node) {
Expand Down
6 changes: 3 additions & 3 deletions template/types/serialization/xml/serialize/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

ob_start(); ?>
/**
* @param null|\DOMElement $element
* @param null|\DOMNode $element
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility.
* @return \DOMElement<?php if ($typeKind !== TypeKind::PRIMITIVE) : ?>
* @return \DOMNode<?php if ($typeKind !== TypeKind::PRIMITIVE) : ?>

* @throws \DOMException<?php endif; ?>

*/
public function xmlSerialize(\DOMElement $element = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMElement
public function xmlSerialize(\DOMNode $element = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMNode
{
if (is_int($config)) {
$libxmlOpts = $config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

ob_start(); ?>
/**
* @param null|\DOMElement $element
* @param null|\DOMNode $element
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $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|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMElement
public function xmlSerialize(null|\DOMNode $element = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): \DOMNode
{
if (is_int($config)) {
$libxmlOpts = $config;
Expand Down
10 changes: 6 additions & 4 deletions template/types/serialization/xml/unserialize/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

ob_start(); ?>
/**
* @param null|string|\DOMElement $element
* @param null|string|\DOMNode $element
* @param null|<?php echo $type->getFullyQualifiedClassName(true); ?> $type
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG; ?> $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility.
* @return null|<?php echo $type->getFullyQualifiedClassName(true); ?>

*/
public static function xmlUnserialize(null|string|\DOMElement $element, null|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZABLE; ?> $type = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): null|self
public static function xmlUnserialize(null|string|\DOMNode $element, null|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZABLE; ?> $type = null, null|int|<?php echo PHPFHIR_INTERFACE_XML_SERIALIZALE_CONFIG ?> $config = null): null|self
{
if (null === $element) {
return null;
Expand All @@ -58,7 +58,8 @@ public static function xmlUnserialize(null|string|\DOMElement $element, null|<?p
throw new \RuntimeException('<?php echo $typeClassName; ?>::xmlUnserialize: Cannot unserialize directly into root type');
} else if (!($type instanceof <?php echo $typeClassName; ?>)) {
throw new \RuntimeException(sprintf(
'<?php echo $typeClassName; ?>::xmlUnserialize - $type must be child instance of <?php echo $type->getFullyQualifiedClassName(true); ?> or null, %s seen.',
'<?php echo $typeClassName; ?>::xmlUnserialize - $type must be child instance of %s or null, %s seen.',
static::class,
get_class($type)
));
}
Expand All @@ -67,7 +68,8 @@ public static function xmlUnserialize(null|string|\DOMElement $element, null|<?p
$type = new <?php echo $typeClassName; ?>(null);
} else if (!($type instanceof <?php echo $typeClassName; ?>)) {
throw new \RuntimeException(sprintf(
'<?php echo $typeClassName; ?>::xmlUnserialize - $type must be instance of <?php echo $type->getFullyQualifiedClassName(true); ?> or null, %s seen.',
'<?php echo $typeClassName; ?>::xmlUnserialize - $type must be instance of %s or null, %s seen.',
static::class,
get_class($type)
));
}
Expand Down
4 changes: 2 additions & 2 deletions template/types/tests/integration/body_domain_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testXML(): void
$e
);
}
$this->assertInstanceOf('<?php echo $bundleType->getFullyQualifiedClassName(true); ?>', $bundle);
$this->assertInstanceOf(<?php echo $bundleType->getClassName(); ?>::class, $bundle);
$entry = $bundle->getEntry();
<?php if ($bundleEntryProperty->isCollection()) : ?>
if (0 === count($entry)) {
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testXML(): void
$e
);
}
$this->assertInstanceOf('<?php echo $type->getFullyQualifiedClassName(true); ?>', $type);
$this->assertInstanceOf(<?php echo $type->getClassName(); ?>::class, $type);
$typeElement = $type->xmlSerialize();
$this->assertEquals($resourceXML, $typeElement->ownerDocument->saveXML($typeElement));
$bundleElement = $bundle->xmlSerialize();
Expand Down
4 changes: 2 additions & 2 deletions template/types/tests/integration/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand All @@ -73,7 +73,7 @@
'bundleEntryProperty' => $bundleEntryProperty,
]
);
endif;
}

echo "}\n";
return ob_get_clean();
41 changes: 22 additions & 19 deletions template/types/validation/methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -54,34 +56,35 @@ public function _getValidationErrors(): array
$errs = [];
<?php endif; ?>
$validationRules = $this->_getValidationRules();
<?php foreach ($type->getLocalProperties()->localPropertiesIterator() as $property) :
<?php foreach ($type->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) :
Expand Down

0 comments on commit 0b72b94

Please sign in to comment.