Passed
Pull Request — master (#13)
by
unknown
02:19
created

XmlToArray::convertDomElement()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 3
b 0
f 0
nc 16
nop 1
dl 0
loc 42
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vyuldashev\XmlToArray;
6
7
use DOMAttr;
8
use DOMCdataSection;
9
use DOMDocument;
10
use DOMElement;
11
use DOMNamedNodeMap;
12
use DOMText;
13
14
class XmlToArray
15
{
16
    protected $document;
17
18
    public function __construct(string $xml)
19
    {
20
        $this->document = new DOMDocument();
21
        $this->document->loadXML($xml);
22
    }
23
24
    public static function convert(string $xml): array
25
    {
26
        $converter = new static($xml);
27
28
        return $converter->toArray();
29
    }
30
31
    protected function convertAttributes(DOMNamedNodeMap $nodeMap): ?array
32
    {
33
        if ($nodeMap->length === 0) {
34
            return null;
35
        }
36
37
        $result = [];
38
39
        /** @var DOMAttr $item */
40
        foreach ($nodeMap as $item) {
41
            $result[$item->name] = $item->value;
42
        }
43
44
        return ['_attributes' => $result];
45
    }
46
47
    protected function convertDomElement(DOMElement $element)
48
    {
49
        $result = $this->convertAttributes($element->attributes);
0 ignored issues
show
Bug introduced by
It seems like $element->attributes can also be of type null; however, parameter $nodeMap of Vyuldashev\XmlToArray\Xm...ay::convertAttributes() does only seem to accept DOMNamedNodeMap, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $result = $this->convertAttributes(/** @scrutinizer ignore-type */ $element->attributes);
Loading history...
50
51
        $sameNamesOccurrences = [];
52
53
        if ($element->childNodes->length > 1) {
54
            $childNodeNames = [];
55
56
            foreach ($element->childNodes as $node) {
57
                $childNodeNames[] = $node->nodeName;
58
            }
59
60
            $sameNamesOccurrences = array_count_values($childNodeNames);
61
        }
62
63
        foreach ($element->childNodes as $key => $node) {
64
            if ($node instanceof DOMCdataSection) {
65
                $result['_cdata'] = $node->data;
66
67
                continue;
68
            }
69
            if ($node instanceof DOMText) {
70
                $result = $node->textContent;
71
72
                continue;
73
            }
74
            if ($node instanceof DOMElement) {
75
                $nodeName = $node->nodeName;
76
                $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1;
77
78
                if ($hasSameName) {
79
                    $result[$nodeName][$key] = $this->convertDomElement($node);
80
                } else {
81
                    $result[$nodeName] = $this->convertDomElement($node);
82
                }
83
84
                continue;
85
            }
86
        }
87
88
        return $result;
89
    }
90
91
    public function toArray(): array
92
    {
93
        $result = [];
94
95
        if ($this->document->hasChildNodes()) {
96
            $children = $this->document->childNodes;
97
98
            foreach ($children as $child) {
99
                $result[$child->nodeName] = $this->convertDomElement($child);
100
            }
101
        }
102
103
        return $result;
104
    }
105
}
106