Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class Converter implements ConverterInterface
15 {
19  const ARGUMENT_KEY = 'argument';
20 
24  const INCLUDE_KEY = 'include';
25 
29  const CURRENT_SCHEMA_KEY = 'current';
30 
34  const NAME_ATTRIBUTE_KEY = 'name';
35 
39  private $argumentParser;
40 
46  public function __construct(Parser $argumentParser)
47  {
48  $this->argumentParser = $argumentParser;
49  }
50 
57  private function toArray(\DOMNode $node)
58  {
59  $result = [];
60  $attributes = [];
61  // Collect data from attributes
62  if ($node->hasAttributes()) {
63  foreach ($node->attributes as $attribute) {
64  $attributes[$attribute->name] = $attribute->value;
65  }
66  }
67 
68  if (isset($attributes[static::INCLUDE_KEY])) {
69  $result[static::INCLUDE_KEY] = $attributes[static::INCLUDE_KEY];
70  unset($attributes[static::INCLUDE_KEY]);
71  }
72 
73  switch ($node->nodeType) {
74  case XML_TEXT_NODE:
75  case XML_COMMENT_NODE:
76  case XML_CDATA_SECTION_NODE:
77  break;
78  default:
79  if ($node->localName === static::ARGUMENT_KEY) {
80  if (!isset($attributes[static::NAME_ATTRIBUTE_KEY])) {
81  throw new \InvalidArgumentException(
82  'Attribute "' . static::NAME_ATTRIBUTE_KEY . '" is absent in the attributes node.'
83  );
84  }
85  $result[ $attributes[static::NAME_ATTRIBUTE_KEY] ] = $this->argumentParser->parse($node);
86  } else {
87  list($arguments, $childResult) = $this->convertChildNodes($node);
88 
89  $result += $this->processArguments($arguments);
90  $result += $childResult;
91  }
92  break;
93  }
94 
95  return $result;
96  }
97 
104  private function getComponentName(\DOMNode $node)
105  {
106  $result = $node->localName;
107  if (!$node->hasAttributes()) {
108  return $result;
109  }
110  foreach ($node->attributes as $attribute) {
111  if ($attribute->name == static::NAME_ATTRIBUTE_KEY) {
112  $result = $attribute->value;
113  break;
114  }
115  }
116 
117  return $result;
118  }
119 
126  public function convert($source)
127  {
128  if ($source === null) {
129  return [];
130  }
131 
132  $result = $this->toArray($source);
133  $result = empty($result) ? $result : reset($result);
134  $schemaMap = [];
135  foreach ($result as $componentName => $componentData) {
136  $schemaMap[$componentName] = $this->processMap($componentData, $result);
137  }
138  return $schemaMap;
139  }
140 
148  private function processMap($componentData, $sourceMap)
149  {
150  $result = [];
151  if (isset($componentData[static::INCLUDE_KEY])) {
152  if (isset($sourceMap[$componentData[static::INCLUDE_KEY]])) {
153  $result = array_replace_recursive(
154  $this->processMap($sourceMap[$componentData[static::INCLUDE_KEY]], $sourceMap),
155  $result
156  );
157  }
158  }
159 
160  if (isset($componentData[static::CURRENT_SCHEMA_KEY])) {
161  $result = array_replace_recursive($componentData[static::CURRENT_SCHEMA_KEY], $result);
162  }
163 
164  return $result;
165  }
166 
173  private function convertChildNodes(\DOMNode $node)
174  {
175  $arguments = [];
176  $childResult = [];
177  foreach ($node->childNodes as $itemNode) {
178  if (empty($itemNode->localName)) {
179  continue;
180  }
181  if ($itemNode->localName === static::ARGUMENT_KEY) {
182  $arguments += $this->toArray($itemNode);
183  } else {
184  $childResult[$this->getComponentName($itemNode)] = $this->toArray($itemNode);
185  }
186  }
187 
188  return [$arguments, $childResult];
189  }
190 
197  private function processArguments(array $arguments)
198  {
199  $result = [];
200  if (!empty($arguments)) {
201  $result[static::ARGUMENT_KEY] = $arguments;
202  }
203 
204  return $result;
205  }
206 }
$source
Definition: source.php:23
$attributes
Definition: matrix.phtml:13
$arguments