Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Buttons.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Buttons 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->hasChildNodes()) {
44  return [
45  Converter::NAME_ATTRIBUTE_KEY => $this->converterUtils->getComponentName($node),
46  Dom::TYPE_ATTRIBUTE => 'array',
47  'item' => []
48  ];
49  }
50 
51  if ($node->nodeType !== XML_ELEMENT_NODE) {
52  return [];
53  }
54 
55  return $this->toArray($node);
56  }
57 
64  private function toArray(\DOMNode $node)
65  {
66  if ($node->localName == 'url') {
67  $urlResult = $this->converter->convert($node, ['type' => 'url']);
68  return $urlResult ?: [];
69  }
70 
71  $result = [];
72 
73  if ($this->hasChildElements($node)) {
74  $result = $this->processChildNodes($node);
75  } else {
76  if ($node->nodeType == XML_ELEMENT_NODE) {
77  $childResult = [];
78  $attributesResult = [];
79  $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
80  $childResult[Dom::TYPE_ATTRIBUTE] = 'string';
81  if ($node->hasAttributes()) {
82  $attributesResult = $this->processAttributes($node);
83  }
84 
85  $result = array_merge(['value' => trim($node->nodeValue)], $childResult, $attributesResult);
86  }
87  }
88 
89  return $result;
90  }
91 
98  private function hasChildElements(\DOMNode $node)
99  {
100  if ($node->hasChildNodes()) {
101  foreach ($node->childNodes as $childNode) {
102  if ($childNode->nodeType == XML_ELEMENT_NODE) {
103  return true;
104  }
105  }
106  }
107  return false;
108  }
109 
116  private function processAttributes(\DOMNode $node)
117  {
118  $attributes = [];
119  $childResult = [];
120  foreach ($node->attributes as $attribute) {
121  $attributes[$attribute->nodeName] = $attribute->value;
122  }
123 
124  if (isset($attributes['class'])) {
125  $childResult['value'] = $attributes['class'];
126  unset($attributes['class']);
127  }
128 
129  return array_merge($attributes, $childResult);
130  }
131 
138  private function processChildNodes(\DOMNode $node)
139  {
140  $result[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
141  $result[Dom::TYPE_ATTRIBUTE] = 'array';
143  foreach ($node->childNodes as $childNode) {
144  if ($childNode->nodeType === XML_ELEMENT_NODE) {
145  $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
146  }
147  }
148  if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'button') {
149  $result['item']['name'] = [
151  'value' => $node->getAttribute('name'),
152  Dom::TYPE_ATTRIBUTE => 'string'
153  ];
154  }
155  return $result;
156  }
157 }
convert(\DOMNode $node, array $data=[])
Definition: Buttons.php:41
__construct(ConverterInterface $converter, ConverterUtils $converterUtils)
Definition: Buttons.php:32
$attributes
Definition: matrix.phtml:13