Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Item.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Item implements ConverterInterface
17 {
21  private $converter;
22 
26  private $converterUtils;
27 
32  public function __construct(ConverterInterface $converter, ConverterUtils $converterUtils)
33  {
34  $this->converter = $converter;
35  $this->converterUtils = $converterUtils;
36  }
37 
41  public function convert(\DOMNode $node, array $data = [])
42  {
43  if ($node->nodeType !== XML_ELEMENT_NODE) {
44  return [];
45  }
46 
47  return $this->toArray($node);
48  }
49 
56  private function toArray(\DOMNode $node)
57  {
58  if ($node->nodeType == XML_ELEMENT_NODE && $node->getAttribute(Dom::TYPE_ATTRIBUTE) == 'url') {
59  $urlResult = $this->converter->convert($node, ['type' => 'url']);
60  return $urlResult ?: [];
61  }
62 
63  $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
64 
65  if ($this->hasChildNodes($node)) {
66  $result = array_merge($result, $this->processChildNodes($node));
67  } else {
68  $result[Dom::TYPE_ATTRIBUTE] = 'string';
69  if (trim($node->nodeValue) != '') {
70  $result['value'] = trim($node->nodeValue);
71  }
72  }
73  if ($node->hasAttributes() && $node->nodeType === XML_ELEMENT_NODE) {
74  $result = array_merge($result, $this->processAttributes($node));
75  }
76  return $result;
77  }
78 
85  private function hasChildNodes(\DOMNode $node)
86  {
87  if ($node->hasChildNodes()) {
88  foreach ($node->childNodes as $childNode) {
89  if ($childNode->nodeType == XML_ELEMENT_NODE) {
90  return true;
91  }
92  }
93  }
94  return false;
95  }
96 
103  private function processAttributes(\DOMNode $node)
104  {
105  $attributes = [];
106  foreach ($node->attributes as $attribute) {
108  continue;
109  }
110  $attributes[$attribute->nodeName] = $attribute->value;
111  }
112 
113  return $attributes;
114  }
115 
122  private function processChildNodes(\DOMNode $node)
123  {
124  $result[Dom::TYPE_ATTRIBUTE] = 'array';
126  foreach ($node->childNodes as $childNode) {
127  if ($childNode->nodeType === XML_ELEMENT_NODE) {
128  $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
129  }
130  }
131  return $result;
132  }
133 }
convert(\DOMNode $node, array $data=[])
Definition: Item.php:41
$attributes
Definition: matrix.phtml:13
__construct(ConverterInterface $converter, ConverterUtils $converterUtils)
Definition: Item.php:32