Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StorageConfig.php
Go to the documentation of this file.
1 <?php
7 
12 
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->localName == 'path' || $node->getAttribute(Dom::TYPE_ATTRIBUTE) == 'url') {
59  $urlResult = $this->converter->convert($node, ['type' => 'url']);
60  return $urlResult ?: [];
61  }
62 
63  $result = [];
64  $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
65 
66  if ($this->hasChildElements($node)) {
67  $result = array_merge($result, $this->processChildNodes($node));
68  } else {
69  if ($node->nodeType == XML_ELEMENT_NODE) {
70  $childResult = [];
71  $attributes = [];
72  $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
73  $childResult[Dom::TYPE_ATTRIBUTE] = 'string';
74  if ($node->hasAttributes()) {
75  foreach ($node->attributes as $attribute) {
76  $attributes[$attribute->nodeName] = $attribute->value;
77  }
78  }
79 
80  $result = array_merge($childResult, ['value' => trim($node->nodeValue)], $attributes);
81  }
82  }
83 
84  return $result;
85  }
86 
93  private function hasChildElements(\DOMNode $node)
94  {
95  if ($node->hasChildNodes()) {
96  foreach ($node->childNodes as $childNode) {
97  if ($childNode->nodeType == XML_ELEMENT_NODE) {
98  return true;
99  }
100  }
101  }
102  return false;
103  }
104 
111  private function processChildNodes(\DOMNode $node)
112  {
113  $result[Dom::TYPE_ATTRIBUTE] = 'array';
115  foreach ($node->childNodes as $childNode) {
116  if ($childNode->nodeType === XML_ELEMENT_NODE) {
117  $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
118  }
119  }
120  return $result;
121  }
122 }
__construct(ConverterInterface $converter, ConverterUtils $converterUtils)
$attributes
Definition: matrix.phtml:13
convert(\DOMNode $node, array $data=[])