From 17382428cf433b87f7aa75ffec0c24a5307582d3 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 14 Jun 2024 08:37:14 +0200 Subject: [PATCH] Faster document to array reader --- .../Reader/DocumentToLookupArrayReader.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Xml/Reader/DocumentToLookupArrayReader.php b/src/Xml/Reader/DocumentToLookupArrayReader.php index b2948e7..c60d482 100644 --- a/src/Xml/Reader/DocumentToLookupArrayReader.php +++ b/src/Xml/Reader/DocumentToLookupArrayReader.php @@ -3,10 +3,11 @@ namespace Soap\Encoding\Xml\Reader; +use DOMAttr; +use DOMNode; use Soap\Encoding\Xml\Node\Element; use Soap\Encoding\Xml\Node\ElementList; -use function VeeWee\Xml\Dom\Locator\Attribute\attributes_list; -use function VeeWee\Xml\Dom\Locator\Element\children as readChildElements; +use function VeeWee\Xml\Dom\Predicate\is_element; final class DocumentToLookupArrayReader { @@ -22,8 +23,13 @@ public function __invoke(Element $xml): array // Read all child elements. // The key is the name of the elements // The value is the raw XML for those element(s) - $elements = readChildElements($root); - foreach ($elements as $element) { + /** @var iterable $children */ + $children = $root->childNodes; + foreach ($children as $element) { + if (!is_element($element)) { + continue; + } + $key = $element->localName ?? 'unknown'; $previousValue = $nodes[$key] ?? null; $currentElement = Element::fromDOMElement($element); @@ -41,13 +47,14 @@ public function __invoke(Element $xml): array // It might be possible that the child is a regular textNode. // In that case, we use '_' as the key and the value of the textNode as value. - $content = trim($root->textContent); - if (!$elements->count() && $content) { + if (!$nodes && $content = trim($root->textContent)) { $nodes['_'] = $content; } // All attributes also need to be added as key => value pairs. - foreach (attributes_list($root) as $attribute) { + /** @var \iterable $attributes */ + $attributes = $root->attributes; + foreach ($attributes as $attribute) { $key = $attribute->localName ?? 'unkown'; $nodes[$key] = $attribute->value; }