Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Options.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Options implements ConverterInterface
17 {
21  private $converterUtils;
22 
26  public function __construct(ConverterUtils $converterUtils)
27  {
28  $this->converterUtils = $converterUtils;
29  }
30 
34  public function convert(\DOMNode $node, array $data = [])
35  {
36  if ($node->nodeType !== XML_ELEMENT_NODE) {
37  return [];
38  }
39 
40  return $this->toArray($node);
41  }
42 
49  private function toArray(\DOMNode $node)
50  {
51  $result = [];
52  $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
53 
54  if ($this->hasChildElements($node)) {
55  $result[Dom::TYPE_ATTRIBUTE] = 'array';
57  foreach ($node->childNodes as $childNode) {
58  if ($childNode->nodeType === XML_ELEMENT_NODE) {
59  $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
60  }
61  }
62  } else {
63  if ($node->nodeType == XML_ELEMENT_NODE) {
64  $childResult = [];
65  $attributes = [];
66  $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
67  $childResult[Dom::TYPE_ATTRIBUTE] = $node->getAttribute(Dom::TYPE_ATTRIBUTE) ?: 'string';
68  if ($node->hasAttributes()) {
69  foreach ($node->attributes as $attribute) {
70  $attributes[$attribute->nodeName] = $attribute->value;
71  }
72  }
73 
74  if (isset($attributes['class'])) {
75  $childResult[Dom::TYPE_ATTRIBUTE] = 'object';
76  $childResult['value'] = $attributes['class'];
77  unset($attributes['class']);
78  }
79 
80  $result = array_merge($attributes, ['value' => trim($node->nodeValue)], $childResult);
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 }
convert(\DOMNode $node, array $data=[])
Definition: Options.php:34
__construct(ConverterUtils $converterUtils)
Definition: Options.php:26
$attributes
Definition: matrix.phtml:13