Passed
Pull Request — master (#13)
by
unknown
01:28
created

XmlToArray::isHomogenous()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
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 $node) {
64
            if ($node instanceof DOMCdataSection) {
65
                $result['_cdata'] = $node->data;
66
67
                continue;
68
            }
69
70
            if ($node instanceof DOMText) {
71
                $result = $node->textContent;
72
73
                continue;
74
            }
75
76
            if ($node instanceof DOMElement) {
77
                $nodeName = $node->nodeName;
78
                $hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1;
79
80
                if ($hasSameName === false) {
81
                    $result[$nodeName] = $this->convertDomElement($node);
82
                    continue;
83
                }
84
85
                $result[$nodeName][] = $this->convertDomElement($node);
86
            }
87
        }
88
89
        return $result;
90
    }
91
92
    public function toArray(): array
93
    {
94
        $result = [];
95
96
        if ($this->document->hasChildNodes()) {
97
            $children = $this->document->childNodes;
98
99
            foreach ($children as $child) {
100
                $result[$child->nodeName] = $this->convertDomElement($child);
101
            }
102
        }
103
104
        return $result;
105
    }
106
}
107