Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Actions.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Actions 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->localName == '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[Dom::TYPE_ATTRIBUTE] = 'array';
69  foreach ($node->childNodes as $childNode) {
70  if ($childNode->nodeType === XML_ELEMENT_NODE) {
71  $result['item'][$this->converterUtils->getComponentName($childNode)] = $this->toArray($childNode);
72  }
73  }
74  } else {
75  if ($node->nodeType == XML_ELEMENT_NODE) {
76  $childResult = [];
77  $attributesResult = [];
78  $childResult[Converter::NAME_ATTRIBUTE_KEY] = $this->converterUtils->getComponentName($node);
79  $childResult[Dom::TYPE_ATTRIBUTE] = 'string';
80  if ($node->hasAttributes()) {
81  $attributesResult = $this->processAttributes($node);
82  }
83 
84  $result = array_merge(['value' => trim($node->nodeValue)], $childResult, $attributesResult);
85  }
86  }
87 
88  return $result;
89  }
90 
97  private function hasChildElements(\DOMNode $node)
98  {
99  if ($node->hasChildNodes()) {
100  foreach ($node->childNodes as $childNode) {
101  if ($childNode->nodeType == XML_ELEMENT_NODE) {
102  return true;
103  }
104  }
105  }
106  return false;
107  }
108 
115  private function processAttributes(\DOMNode $node)
116  {
117  $attributes = [];
118  $childResult = [];
119  foreach ($node->attributes as $attribute) {
120  $attributes[$attribute->nodeName] = $attribute->value;
121  }
122 
123  if (isset($attributes['class'])) {
124  $childResult[Dom::TYPE_ATTRIBUTE] = 'object';
125  $childResult['value'] = $attributes['class'];
126  unset($attributes['class']);
127  }
128 
129  return array_merge($attributes, $childResult);
130  }
131 }
convert(\DOMNode $node, array $data=[])
Definition: Actions.php:41
$attributes
Definition: matrix.phtml:13
__construct(ConverterInterface $converter, ConverterUtils $converterUtils)
Definition: Actions.php:32