Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Parser Class Reference

Public Member Functions

 __construct ()
 
 initErrorHandler ()
 
 getDom ()
 
 xmlToArray ()
 
 load ($file)
 
 loadXML ($string)
 
 errorHandler ($errorNo, $errorStr, $errorFile, $errorLine)
 

Protected Member Functions

 _getCurrentDom ()
 
 _setCurrentDom ($node)
 
 _xmlToArray ($currentNode=false)
 

Protected Attributes

 $_dom = null
 
 $_currentDom
 
 $_content = []
 
 $errorHandlerIsActive = false
 

Detailed Description

Definition at line 8 of file Parser.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Definition at line 33 of file Parser.php.

34  {
35  $this->_dom = new \DOMDocument();
36  $this->_currentDom = $this->_dom;
37  return $this;
38  }

Member Function Documentation

◆ _getCurrentDom()

_getCurrentDom ( )
protected
Returns
\DOMDocument

Definition at line 61 of file Parser.php.

62  {
63  return $this->_currentDom;
64  }

◆ _setCurrentDom()

_setCurrentDom (   $node)
protected
Parameters
\DOMDocument$node
Returns
$this

Definition at line 70 of file Parser.php.

71  {
72  $this->_currentDom = $node;
73  return $this;
74  }

◆ _xmlToArray()

_xmlToArray (   $currentNode = false)
protected
Parameters
bool$currentNode
Returns
array @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 91 of file Parser.php.

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  }
_xmlToArray($currentNode=false)
Definition: Parser.php:91
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13

◆ errorHandler()

errorHandler (   $errorNo,
  $errorStr,
  $errorFile,
  $errorLine 
)

Custom XML lib error handler

Parameters
int$errorNo
string$errorStr
string$errorFile
int$errorLine
Exceptions

Definition at line 187 of file Parser.php.

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  }
$message

◆ getDom()

getDom ( )
Returns
\DOMDocument|null

Definition at line 53 of file Parser.php.

54  {
55  return $this->_dom;
56  }

◆ initErrorHandler()

initErrorHandler ( )

Initializes error handler

Returns
void

Definition at line 45 of file Parser.php.

46  {
47  $this->errorHandlerIsActive = true;
48  }

◆ load()

load (   $file)
Parameters
string$file
Returns
$this

Definition at line 143 of file Parser.php.

144  {
145  $this->getDom()->load($file);
146  return $this;
147  }

◆ loadXML()

loadXML (   $string)
Parameters
string$string
Returns
$this
Exceptions

Definition at line 154 of file Parser.php.

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  }

◆ xmlToArray()

xmlToArray ( )
Returns
array

Definition at line 79 of file Parser.php.

80  {
81  $this->_content = $this->_xmlToArray();
82  return $this->_content;
83  }
_xmlToArray($currentNode=false)
Definition: Parser.php:91

Field Documentation

◆ $_content

$_content = []
protected

Definition at line 23 of file Parser.php.

◆ $_currentDom

$_currentDom
protected

Definition at line 18 of file Parser.php.

◆ $_dom

$_dom = null
protected

Definition at line 13 of file Parser.php.

◆ $errorHandlerIsActive

$errorHandlerIsActive = false
protected

Definition at line 28 of file Parser.php.


The documentation for this class was generated from the following file: