Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Parser.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Xml;
7 
8 class Parser
9 {
13  protected $_dom = null;
14 
18  protected $_currentDom;
19 
23  protected $_content = [];
24 
28  protected $errorHandlerIsActive = false;
29 
33  public function __construct()
34  {
35  $this->_dom = new \DOMDocument();
36  $this->_currentDom = $this->_dom;
37  return $this;
38  }
39 
45  public function initErrorHandler()
46  {
47  $this->errorHandlerIsActive = true;
48  }
49 
53  public function getDom()
54  {
55  return $this->_dom;
56  }
57 
61  protected function _getCurrentDom()
62  {
63  return $this->_currentDom;
64  }
65 
70  protected function _setCurrentDom($node)
71  {
72  $this->_currentDom = $node;
73  return $this;
74  }
75 
79  public function xmlToArray()
80  {
81  $this->_content = $this->_xmlToArray();
82  return $this->_content;
83  }
84 
91  protected function _xmlToArray($currentNode = false)
92  {
93  if (!$currentNode) {
94  $currentNode = $this->getDom();
95  }
96  $content = '';
97  foreach ($currentNode->childNodes as $node) {
98  switch ($node->nodeType) {
99  case XML_ELEMENT_NODE:
100  $content = $content ?: [];
101 
102  $value = null;
103  if ($node->hasChildNodes()) {
104  $value = $this->_xmlToArray($node);
105  }
106  $attributes = [];
107  if ($node->hasAttributes()) {
108  foreach ($node->attributes as $attribute) {
109  $attributes += [$attribute->name => $attribute->value];
110  }
111  $value = ['_value' => $value, '_attribute' => $attributes];
112  }
113  if (isset($content[$node->nodeName])) {
114  if ((is_string($content[$node->nodeName]) || !isset($content[$node->nodeName][0]))
115  || (is_array($value) && !is_array($content[$node->nodeName][0]))
116  ) {
117  $oldValue = $content[$node->nodeName];
118  $content[$node->nodeName] = [];
119  $content[$node->nodeName][] = $oldValue;
120  }
121  $content[$node->nodeName][] = $value;
122  } else {
123  $content[$node->nodeName] = $value;
124  }
125  break;
126  case XML_CDATA_SECTION_NODE:
127  $content = $node->nodeValue;
128  break;
129  case XML_TEXT_NODE:
130  if (trim($node->nodeValue) !== '') {
131  $content = $node->nodeValue;
132  }
133  break;
134  }
135  }
136  return $content;
137  }
138 
143  public function load($file)
144  {
145  $this->getDom()->load($file);
146  return $this;
147  }
148 
154  public function loadXML($string)
155  {
156  if ($this->errorHandlerIsActive) {
157  set_error_handler([$this, 'errorHandler']);
158  }
159 
160  try {
161  $this->getDom()->loadXML($string);
162  } catch (\Magento\Framework\Exception\LocalizedException $e) {
163  restore_error_handler();
164  throw new \Magento\Framework\Exception\LocalizedException(
165  new \Magento\Framework\Phrase($e->getMessage()),
166  $e
167  );
168  }
169 
170  if ($this->errorHandlerIsActive) {
171  restore_error_handler();
172  }
173 
174  return $this;
175  }
176 
187  public function errorHandler($errorNo, $errorStr, $errorFile, $errorLine)
188  {
189  if ($errorNo != 0) {
190  $message = "{$errorStr} in {$errorFile} on line {$errorLine}";
191  throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase($message));
192  }
193  }
194 }
_xmlToArray($currentNode=false)
Definition: Parser.php:91
$message
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
errorHandler($errorNo, $errorStr, $errorFile, $errorLine)
Definition: Parser.php:187