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

Public Member Functions

 setParent ($element)
 
 getParent ()
 
 hasChildren ()
 
 getAttribute ($name)
 
 descend ($path)
 
 setAttribute ($name, $value)
 
 asArray ()
 
 asCanonicalArray ()
 
 asNiceXml ($filename='', $level=0)
 
 innerXml ($level=0)
 
 xmlentities ($value=null)
 
 appendChild ($source)
 
 extend ($source, $overwrite=false)
 
 extendChild ($source, $overwrite=false)
 
 setNode ($path, $value, $overwrite=true)
 
 unsetSelf ()
 

Protected Member Functions

 _asArray ($isCanonical=false)
 

Protected Attributes

 $_parent = null
 

Detailed Description

Extends SimpleXML to add valuable functionality to \SimpleXMLElement class

@api

Since
100.0.2

Definition at line 15 of file Element.php.

Member Function Documentation

◆ _asArray()

_asArray (   $isCanonical = false)
protected

Returns the node and children as an array

Parameters
bool$isCanonical- whether to ignore attributes
Returns
array|string

Definition at line 197 of file Element.php.

198  {
199  $result = [];
200  if (!$isCanonical) {
201  // add attributes
202  foreach ($this->attributes() as $attributeName => $attribute) {
203  if ($attribute) {
204  $result['@'][$attributeName] = (string)$attribute;
205  }
206  }
207  }
208  // add children values
209  if ($this->hasChildren()) {
210  foreach ($this->children() as $childName => $child) {
211  $result[$childName] = $child->_asArray($isCanonical);
212  }
213  } else {
214  if (empty($result)) {
215  // return as string, if nothing was found
216  $result = (string)$this;
217  } else {
218  // value has zero key element
219  $result[0] = (string)$this;
220  }
221  }
222  return $result;
223  }

◆ appendChild()

appendChild (   $source)

Appends $source to current node

Parameters
\Magento\Framework\Simplexml\Element$source
Returns
$this

Definition at line 330 of file Element.php.

331  {
332  if ($source->count()) {
333  $child = $this->addChild($source->getName());
334  } else {
335  $child = $this->addChild($source->getName(), $this->xmlentities($source));
336  }
337  $child->setParent($this);
338 
339  $attributes = $source->attributes();
340  foreach ($attributes as $key => $value) {
341  $child->addAttribute($key, $this->xmlentities($value));
342  }
343 
344  foreach ($source->children() as $sourceChild) {
345  $child->appendChild($sourceChild);
346  }
347  return $this;
348  }
$source
Definition: source.php:23
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13

◆ asArray()

asArray ( )

Returns the node and children as an array

Returns
array|string

Definition at line 177 of file Element.php.

178  {
179  return $this->_asArray();
180  }
_asArray($isCanonical=false)
Definition: Element.php:197

◆ asCanonicalArray()

asCanonicalArray ( )

asArray() analog, but without attributes

Returns
array|string

Definition at line 186 of file Element.php.

187  {
188  return $this->_asArray(true);
189  }
_asArray($isCanonical=false)
Definition: Element.php:197

◆ asNiceXml()

asNiceXml (   $filename = '',
  $level = 0 
)

Makes nicely formatted XML from the node

Parameters
string$filename
int | boolean$levelif false
Returns
string @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 234 of file Element.php.

235  {
236  if (is_numeric($level)) {
237  $pad = str_pad('', $level * 3, ' ', STR_PAD_LEFT);
238  $nl = "\n";
239  } else {
240  $pad = '';
241  $nl = '';
242  }
243 
244  $out = $pad . '<' . $this->getName();
245 
246  $attributes = $this->attributes();
247  if ($attributes) {
248  foreach ($attributes as $key => $value) {
249  $out .= ' ' . $key . '="' . str_replace('"', '\"', (string)$value) . '"';
250  }
251  }
252 
253  $attributes = $this->attributes('xsi', true);
254  if ($attributes) {
255  foreach ($attributes as $key => $value) {
256  $out .= ' xsi:' . $key . '="' . str_replace('"', '\"', (string)$value) . '"';
257  }
258  }
259 
260  if ($this->hasChildren()) {
261  $out .= '>';
262  $value = trim((string)$this);
263  if (strlen($value)) {
264  $out .= $this->xmlentities($value);
265  }
266  $out .= $nl;
267  foreach ($this->children() as $child) {
268  $out .= $child->asNiceXml('', is_numeric($level) ? $level + 1 : true);
269  }
270  $out .= $pad . '</' . $this->getName() . '>' . $nl;
271  } else {
272  $value = (string)$this;
273  if (strlen($value)) {
274  $out .= '>' . $this->xmlentities($value) . '</' . $this->getName() . '>' . $nl;
275  } else {
276  $out .= '/>' . $nl;
277  }
278  }
279 
280  if ((0 === $level || false === $level) && !empty($filename)) {
281  file_put_contents($filename, $out);
282  }
283 
284  return $out;
285  }
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13

◆ descend()

descend (   $path)

Find a descendant of a node by path

Todo:

Do we need to make it xpath look-a-like?

Check if we still need all this and revert to plain XPath if this makes any sense

param string $path Subset of xpath. Example: "child/grand[@attrName='attrValue']/subGrand"

Parameters
string$pathExample: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex)
Returns
\Magento\Framework\Simplexml\Element @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 103 of file Element.php.

104  {
105  # $node = $this->xpath($path);
106  # return $node[0];
107  if (is_array($path)) {
108  $pathArr = $path;
109  } else {
110  // Simple exploding by / does not suffice,
111  // as an attribute value may contain a / inside
112  // Note that there are three matches for different kinds of attribute values specification
113  if (strpos($path, "@") === false) {
114  $pathArr = explode('/', $path);
115  } else {
116  $regex = "#([^@/\\\"]+(?:@[^=/]+=(?:\\\"[^\\\"]*\\\"|[^/]*))?)/?#";
117  $pathArr = $pathMatches = [];
118  if (preg_match_all($regex, $path, $pathMatches)) {
119  $pathArr = $pathMatches[1];
120  }
121  }
122  }
123  $desc = $this;
124  foreach ($pathArr as $nodeName) {
125  if (strpos($nodeName, '@') !== false) {
126  $a = explode('@', $nodeName);
127  $b = explode('=', $a[1]);
128  $nodeName = $a[0];
129  $attributeName = $b[0];
130  $attributeValue = $b[1];
131  //
132  // Does a very simplistic trimming of attribute value.
133  //
134  $attributeValue = trim($attributeValue, '"');
135  $found = false;
136  foreach ($desc->{$nodeName} as $subdesc) {
137  if ((string)$subdesc[$attributeName] === $attributeValue) {
138  $found = true;
139  $desc = $subdesc;
140  break;
141  }
142  }
143  if (!$found) {
144  $desc = false;
145  }
146  } else {
147  $desc = $desc->{$nodeName};
148  }
149  if (!$desc) {
150  return false;
151  }
152  }
153  return $desc;
154  }

◆ extend()

extend (   $source,
  $overwrite = false 
)

Extends current node with xml from $source

If $overwrite is false will merge only missing nodes Otherwise will overwrite existing nodes

Parameters
\Magento\Framework\Simplexml\Element$source
boolean$overwrite
Returns
$this

Definition at line 360 of file Element.php.

361  {
362  if (!$source instanceof \Magento\Framework\Simplexml\Element) {
363  return $this;
364  }
365 
366  foreach ($source->children() as $child) {
367  $this->extendChild($child, $overwrite);
368  }
369 
370  return $this;
371  }
$source
Definition: source.php:23
extendChild($source, $overwrite=false)
Definition: Element.php:382

◆ extendChild()

extendChild (   $source,
  $overwrite = false 
)

Extends one node

Parameters
\Magento\Framework\Simplexml\Element$source
boolean$overwrite
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 382 of file Element.php.

383  {
384  // this will be our new target node
385  $targetChild = null;
386 
387  // name of the source node
388  $sourceName = $source->getName();
389 
390  // here we have children of our source node
391  $sourceChildren = $source->children();
392 
393  if (!$source->hasChildren()) {
394  // handle string node
395  if (isset($this->{$sourceName})) {
396  // if target already has children return without regard
397  if ($this->{$sourceName}->hasChildren()) {
398  return $this;
399  }
400  if ($overwrite) {
401  unset($this->{$sourceName});
402  } else {
403  return $this;
404  }
405  }
406 
407  $targetChild = $this->addChild($sourceName, $source->xmlentities());
408  $targetChild->setParent($this);
409  foreach ($source->attributes() as $key => $value) {
410  $targetChild->addAttribute($key, $this->xmlentities($value));
411  }
412  return $this;
413  }
414 
415  if (isset($this->{$sourceName})) {
416  $targetChild = $this->{$sourceName};
417  }
418 
419  if ($targetChild === null) {
420  // if child target is not found create new and descend
421  $targetChild = $this->addChild($sourceName);
422  $targetChild->setParent($this);
423  foreach ($source->attributes() as $key => $value) {
424  $targetChild->addAttribute($key, $this->xmlentities($value));
425  }
426  }
427 
428  // finally add our source node children to resulting new target node
429  foreach ($sourceChildren as $childKey => $childNode) {
430  $targetChild->extendChild($childNode, $overwrite);
431  }
432 
433  return $this;
434  }
$source
Definition: source.php:23
$value
Definition: gender.phtml:16

◆ getAttribute()

getAttribute (   $name)

Returns attribute value by attribute name

Parameters
string$name
Returns
string|null

Definition at line 87 of file Element.php.

88  {
89  $attrs = $this->attributes();
90  return isset($attrs[$name]) ? (string)$attrs[$name] : null;
91  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getParent()

getParent ( )

Returns parent node for the element

Currently using xpath

Exceptions

Definition at line 48 of file Element.php.

49  {
50  if (!empty($this->_parent)) {
51  $parent = $this->_parent;
52  } else {
53  $arr = $this->xpath('..');
54  if (!isset($arr[0])) {
55  throw new \InvalidArgumentException('Root node could not be unset.');
56  }
57  $parent = $arr[0];
58  }
59  return $parent;
60  }

◆ hasChildren()

hasChildren ( )

Enter description here...

Returns
boolean @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 68 of file Element.php.

69  {
70  if (!$this->children()) {
71  return false;
72  }
73 
74  // simplexml bug: @attributes is in children() but invisible in foreach
75  foreach ($this->children() as $k => $child) {
76  return true;
77  }
78  return false;
79  }

◆ innerXml()

innerXml (   $level = 0)

Enter description here...

Parameters
int$level
Returns
string

Definition at line 293 of file Element.php.

294  {
295  $out = '';
296  foreach ($this->children() as $child) {
297  $out .= $child->asNiceXml($level);
298  }
299  return $out;
300  }

◆ setAttribute()

setAttribute (   $name,
  $value 
)

Create attribute if it does not exists and set value to it

Parameters
string$name
string$value
Returns
void

Definition at line 163 of file Element.php.

164  {
165  if (!isset($this->attributes()[$name])) {
166  $this->addAttribute($name, $value);
167  }
168 
169  $this->attributes()[$name] = $value;
170  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setNode()

setNode (   $path,
  $value,
  $overwrite = true 
)

Set node

Parameters
string$path
string$value
bool$overwrite
Returns
$this

Definition at line 444 of file Element.php.

445  {
446  $arr1 = explode('/', $path);
447  $arr = [];
448  foreach ($arr1 as $v) {
449  if (!empty($v)) {
450  $arr[] = $v;
451  }
452  }
453  $last = sizeof($arr) - 1;
454  $node = $this;
455  foreach ($arr as $i => $nodeName) {
456  if ($last === $i) {
457  if (!isset($node->{$nodeName}) || $overwrite) {
458  $node->{$nodeName} = $value;
459  }
460  } else {
461  if (!isset($node->{$nodeName})) {
462  $node = $node->addChild($nodeName);
463  } else {
464  $node = $node->{$nodeName};
465  }
466  }
467  }
468  return $this;
469  }
$value
Definition: gender.phtml:16
$i
Definition: gallery.phtml:31

◆ setParent()

setParent (   $element)

For future use

Parameters
\Magento\Framework\Simplexml\Element$element
Returns
void @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 35 of file Element.php.

36  {
37  //$this->_parent = $element;
38  }

◆ unsetSelf()

unsetSelf ( )

Unset self from the XML-node tree

Note: trying to refer this object as a variable after "unsetting" like this will result in E_WARNING

Returns
void

Definition at line 477 of file Element.php.

478  {
479  $uniqueId = uniqid();
480  $this['_unique_id'] = $uniqueId;
481  $children = $this->getParent()->xpath('*');
482  for ($i = count($children); $i > 0; $i--) {
483  if ($children[$i - 1][0]['_unique_id'] == $uniqueId) {
484  unset($children[$i - 1][0]);
485  return;
486  }
487  }
488  }
$children
Definition: actions.phtml:11
$i
Definition: gallery.phtml:31

◆ xmlentities()

xmlentities (   $value = null)

Converts meaningful xml characters to xml entities

Parameters
string$value
Returns
string

Definition at line 308 of file Element.php.

309  {
310  if ($value === null) {
311  $value = $this;
312  }
313  $value = (string)$value;
314 
315  $value = str_replace(
316  ['&', '"', "'", '<', '>'],
317  ['&amp;', '&quot;', '&apos;', '&lt;', '&gt;'],
318  $value
319  );
320 
321  return $value;
322  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_parent

$_parent = null
protected

Definition at line 26 of file Element.php.


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