Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/Xml/Reader/DocumentToLookupArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<DOMNode> $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);
Expand All @@ -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<DOMAttr> $attributes */
$attributes = $root->attributes;
foreach ($attributes as $attribute) {
$key = $attribute->localName ?? 'unkown';
$nodes[$key] = $attribute->value;
}
Expand Down