-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathParser.php
208 lines (190 loc) · 5.12 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Xml;
class Parser
{
/**
* @var \DOMDocument|null
*/
protected $_dom = null;
/**
* @var \DOMDocument
*/
protected $_currentDom;
/**
* @var array
*/
protected $_content = [];
/**
* @var boolean
*/
protected $errorHandlerIsActive = false;
/**
*
*/
public function __construct()
{
$this->_dom = new \DOMDocument();
$this->_currentDom = $this->_dom;
return $this;
}
/**
* Initializes error handler
*
* @return void
*/
public function initErrorHandler()
{
$this->errorHandlerIsActive = true;
}
/**
* Get DOM
*
* @return \DOMDocument|null
*/
public function getDom()
{
return $this->_dom;
}
/**
* Get current DOM
*
* @return \DOMDocument
*/
protected function _getCurrentDom()
{
return $this->_currentDom;
}
/**
* Set current DOM
*
* @param \DOMDocument $node
* @return $this
*/
protected function _setCurrentDom($node)
{
$this->_currentDom = $node;
return $this;
}
/**
* XML to array
*
* @return array
*/
public function xmlToArray()
{
$this->_content = $this->_xmlToArray();
return $this->_content;
}
/**
* XML to array
*
* @param bool $currentNode
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _xmlToArray($currentNode = false)
{
if (!$currentNode) {
$currentNode = $this->getDom();
}
$content = '';
foreach ($currentNode->childNodes as $node) {
switch ($node->nodeType) {
case XML_ELEMENT_NODE:
$content = $content ?: [];
$value = null;
if ($node->hasChildNodes()) {
$value = $this->_xmlToArray($node);
}
$attributes = [];
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$attributes += [$attribute->name => $attribute->value];
}
$value = ['_value' => $value, '_attribute' => $attributes];
}
if (isset($content[$node->nodeName])) {
if ((is_string($content[$node->nodeName]) || !isset($content[$node->nodeName][0]))
|| (is_array($value) && !is_array($content[$node->nodeName][0]))
) {
$oldValue = $content[$node->nodeName];
$content[$node->nodeName] = [];
$content[$node->nodeName][] = $oldValue;
}
$content[$node->nodeName][] = $value;
} else {
$content[$node->nodeName] = $value;
}
break;
case XML_CDATA_SECTION_NODE:
$content = $node->nodeValue;
break;
case XML_TEXT_NODE:
if ($node->nodeValue !== null && trim($node->nodeValue) !== '') {
$content = $node->nodeValue;
}
break;
}
}
return $content;
}
/**
* Load
*
* @param string $file
* @return $this
*/
public function load($file)
{
$this->getDom()->load($file);
return $this;
}
/**
* Load XML
*
* @param string $string
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadXML($string)
{
if ($this->errorHandlerIsActive) {
set_error_handler([$this, 'errorHandler']);
}
try {
$this->getDom()->loadXML($string);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
restore_error_handler();
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase($e->getMessage()),
$e
);
}
if ($this->errorHandlerIsActive) {
restore_error_handler();
}
return $this;
}
/**
* Custom XML lib error handler
*
* @param int $errorNo
* @param string $errorStr
* @param string $errorFile
* @param int $errorLine
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
public function errorHandler($errorNo, $errorStr, $errorFile, $errorLine)
{
if ($errorNo != 0) {
$message = "{$errorStr} in {$errorFile} on line {$errorLine}";
throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase($message));
}
}
}