Skip to content

Commit 5e4a88b

Browse files
committed
Faster document to array reader
1 parent 7d94550 commit 5e4a88b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/Xml/Reader/DocumentToLookupArrayReader.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
use Soap\Encoding\Xml\Node\Element;
77
use Soap\Encoding\Xml\Node\ElementList;
8-
use function VeeWee\Xml\Dom\Locator\Attribute\attributes_list;
9-
use function VeeWee\Xml\Dom\Locator\Element\children as readChildElements;
8+
use function VeeWee\Xml\Dom\Predicate\is_element;
109

1110
final class DocumentToLookupArrayReader
1211
{
@@ -22,8 +21,12 @@ public function __invoke(Element $xml): array
2221
// Read all child elements.
2322
// The key is the name of the elements
2423
// The value is the raw XML for those element(s)
25-
$elements = readChildElements($root);
24+
$elements = $root->childNodes;
2625
foreach ($elements as $element) {
26+
if (!is_element($element)) {
27+
continue;
28+
}
29+
2730
$key = $element->localName ?? 'unknown';
2831
$previousValue = $nodes[$key] ?? null;
2932
$currentElement = Element::fromDOMElement($element);
@@ -41,13 +44,12 @@ public function __invoke(Element $xml): array
4144

4245
// It might be possible that the child is a regular textNode.
4346
// In that case, we use '_' as the key and the value of the textNode as value.
44-
$content = trim($root->textContent);
45-
if (!$elements->count() && $content) {
47+
if (!$nodes && $content = trim($root->textContent)) {
4648
$nodes['_'] = $content;
4749
}
4850

4951
// All attributes also need to be added as key => value pairs.
50-
foreach (attributes_list($root) as $attribute) {
52+
foreach ($root->attributes as $attribute) {
5153
$key = $attribute->localName ?? 'unkown';
5254
$nodes[$key] = $attribute->value;
5355
}

0 commit comments

Comments
 (0)