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); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$sameNamesOccurrences = []; |
52
|
|
|
$sameNodeNameIndexes = []; |
53
|
|
|
|
54
|
|
|
if ($element->childNodes->length > 1) { |
55
|
|
|
$childNodeNames = []; |
56
|
|
|
|
57
|
|
|
foreach ($element->childNodes as $node) { |
58
|
|
|
$childNodeNames[] = $node->nodeName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$sameNamesOccurrences = array_count_values($childNodeNames); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($element->childNodes as $node) { |
65
|
|
|
if ($node instanceof DOMCdataSection) { |
66
|
|
|
$result['_cdata'] = $node->data; |
67
|
|
|
|
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
if ($node instanceof DOMText) { |
71
|
|
|
$result = $node->textContent; |
72
|
|
|
|
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
if ($node instanceof DOMElement) { |
76
|
|
|
$nodeName = $node->nodeName; |
77
|
|
|
$hasSameName = array_key_exists($nodeName, $sameNamesOccurrences) && $sameNamesOccurrences[$nodeName] > 1; |
78
|
|
|
|
79
|
|
|
if ($hasSameName === false) { |
80
|
|
|
$result[$nodeName] = $this->convertDomElement($node); |
81
|
|
|
continue; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// If we already have a child node with the same name, we need to increment |
85
|
|
|
// and keep track of their index. |
86
|
|
|
|
87
|
|
|
if (isset($sameNodeNameIndexes[$nodeName])) { |
88
|
|
|
$key = $sameNodeNameIndexes[$nodeName] + 1; |
89
|
|
|
} else { |
90
|
|
|
$key = 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result[$nodeName][$key] = $this->convertDomElement($node); |
94
|
|
|
$sameNodeNameIndexes[$nodeName] = $key; |
95
|
|
|
|
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function toArray(): array |
104
|
|
|
{ |
105
|
|
|
$result = []; |
106
|
|
|
|
107
|
|
|
if ($this->document->hasChildNodes()) { |
108
|
|
|
$children = $this->document->childNodes; |
109
|
|
|
|
110
|
|
|
foreach ($children as $child) { |
111
|
|
|
$result[$child->nodeName] = $this->convertDomElement($child); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|