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
|
|
|
$sameNames = []; |
50
|
|
|
$result = $this->convertAttributes($element->attributes); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// Creates an index which counts each key, starting at zero, e.g. ['foo' => 2, 'bar' => 0] |
53
|
|
|
$childNodeNames = []; |
|
|
|
|
54
|
|
|
foreach ($element->childNodes as $key => $node) { |
55
|
|
|
if (array_key_exists($node->nodeName, $sameNames)) { |
56
|
|
|
$sameNames[$node->nodeName] += 1; |
57
|
|
|
} else { |
58
|
|
|
$sameNames[$node->nodeName] = 0; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($element->childNodes as $key => $node) { |
63
|
|
|
if (is_null($result)) { |
64
|
|
|
$result = []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($node instanceof DOMCdataSection) { |
68
|
|
|
$result['_cdata'] = $node->data; |
69
|
|
|
|
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
if ($node instanceof DOMText) { |
73
|
|
|
if (empty($result)) { |
74
|
|
|
$result = $node->textContent; |
75
|
|
|
} else { |
76
|
|
|
$result['_value'] = $node->textContent; |
77
|
|
|
} |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
if ($node instanceof DOMElement) { |
81
|
|
|
if ($sameNames[$node->nodeName]) { // Truthy — When $sameNames['foo'] > 0 |
82
|
|
|
if (!array_key_exists($node->nodeName, $result)) { // Setup $result['foo'] |
83
|
|
|
$result[$node->nodeName] = []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$result[$node->nodeName][$key] = $this->convertDomElement($node); // Store as $result['foo'][*] |
87
|
|
|
} else { |
88
|
|
|
$result[$node->nodeName] = $this->convertDomElement($node); // Store as $result['foo'] |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function toArray(): array |
99
|
|
|
{ |
100
|
|
|
$result = []; |
101
|
|
|
|
102
|
|
|
if ($this->document->hasChildNodes()) { |
103
|
|
|
$children = $this->document->childNodes; |
104
|
|
|
|
105
|
|
|
foreach ($children as $child) { |
106
|
|
|
$result[$child->nodeName] = $this->convertDomElement($child); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|