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
6 namespace Magento\Ui\Config;
7 
8 use Magento\Framework\Config\ConverterInterface as ConfigConverterInterface;
12 
16 class Converter implements ConfigConverterInterface
17 {
21  const DATA_ATTRIBUTES_KEY = 'attributes';
22 
26  const DATA_ARGUMENTS_KEY = 'arguments';
27 
31  const DATA_COMPONENTS_KEY = 'children';
32 
36  const ARGUMENT_KEY = 'argument';
37 
41  const SETTINGS_KEY = 'settings';
42 
46  const NAME_ATTRIBUTE_KEY = 'name';
47 
51  const CLASS_ATTRIBUTE_KEY = 'class';
52 
56  private $argumentParser;
57 
61  private $schemaMap = [];
62 
66  private $reader;
67 
71  private $parser;
72 
76  private $converterUtils;
77 
84  public function __construct(
86  ParserInterface $parser,
87  ReaderInterface $reader,
88  ConverterUtils $converterUtils
89  ) {
90  $this->argumentParser = $argumentParser;
91  $this->reader = $reader;
92  $this->parser = $parser;
93  $this->converterUtils = $converterUtils;
94  }
95 
102  private function toArray(\DOMNode $node)
103  {
104  $result = [];
105  $attributes = [];
106  // Collect data from attributes
107  if ($node->hasAttributes()) {
108  foreach ($node->attributes as $attribute) {
109  if ($attribute->name == 'noNamespaceSchemaLocation') {
110  continue;
111  }
112  $attributes[$attribute->name] = $attribute->value;
113  }
114  }
115 
116  switch ($node->nodeType) {
117  case XML_TEXT_NODE:
118  case XML_COMMENT_NODE:
119  case XML_CDATA_SECTION_NODE:
120  break;
121  default:
122  if ($node->localName === static::ARGUMENT_KEY) {
123  if (!isset($attributes[static::NAME_ATTRIBUTE_KEY])) {
124  throw new \InvalidArgumentException(
125  'Attribute "' . static::NAME_ATTRIBUTE_KEY . '" is absent in the attributes node.'
126  );
127  }
128  $result[$attributes[static::NAME_ATTRIBUTE_KEY]] = $this->argumentParser->parse($node);
129  } else {
130  $resultComponent = $this->convertNode($node);
131  list($arguments, $childResult) = $this->convertChildNodes($node);
132 
133  $result = array_merge(
134  $this->processArguments($arguments, $resultComponent),
135  $this->processAttributes($attributes),
136  $this->processChildResult($node, $childResult)
137  );
138  }
139 
140  break;
141  }
142 
143  return $result;
144  }
145 
152  public function convert($source)
153  {
154  if ($source === null) {
155  return [];
156  }
157 
158  if (!$this->schemaMap) {
159  $this->schemaMap = $this->reader->read();
160  }
161  $result = $this->toArray($source);
162  return empty($result) ? $result : reset($result);
163  }
164 
171  private function convertNode(\DOMNode $node)
172  {
173  $resultComponent = [];
174  if (empty($node->localName)
175  || !$this->converterUtils->isUiComponent($node)
176  || !isset($this->schemaMap[$node->localName])
177  ) {
178  return $resultComponent;
179  }
180 
181  foreach ($this->schemaMap[$node->localName] as $componentData) {
182  $result = [];
183  foreach ($componentData as $dataKey => $dataValue) {
184  $resultParser = $this->parser->parse($dataValue, $node);
185  if ($resultParser) {
186  $result[$dataKey] = $resultParser;
187  }
188  }
189  $resultComponent = array_replace_recursive($resultComponent, $result);
190  }
191 
192  return $resultComponent;
193  }
194 
202  private function processArguments(array $arguments, array $resultComponent)
203  {
204  $result = [];
205  if (!empty($arguments) || !empty($resultComponent)) {
206  $result[static::DATA_ARGUMENTS_KEY] = array_replace_recursive($resultComponent, $arguments);
207  }
208  return $result;
209  }
210 
217  private function processAttributes(array $attributes)
218  {
219  $result = [];
220  if (!empty($attributes)) {
221  $result[static::DATA_ATTRIBUTES_KEY] = $attributes;
222  }
223  return $result;
224  }
225 
231  private function processChildResult(\DOMNode $node, array $childResult)
232  {
233  $result = [];
234  if ($node->parentNode !== null) {
235  $result[static::DATA_COMPONENTS_KEY] = $childResult;
236  } else {
237  $result = $childResult;
238  }
239 
240  return $result;
241  }
242 
249  private function convertChildNodes(\DOMNode $node)
250  {
251  $arguments = [];
252  $childResult = [];
253  for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) {
254  $itemNode = $node->childNodes->item($i);
255  if ($itemNode->localName == null) {
256  continue;
257  }
258  if ($itemNode->localName === static::ARGUMENT_KEY) {
259  $arguments += $this->toArray($itemNode);
260  } elseif ($this->converterUtils->isUiComponent($itemNode)
261  && isset($this->schemaMap[$itemNode->localName])
262  ) {
263  $itemNodeName = $this->converterUtils->getComponentName($itemNode);
264  $childResult[$itemNodeName] = $this->toArray($itemNode);
265  // 'uiComponentType' is needed this for Reader to merge default values from definition
266  $childResult[$itemNodeName]['uiComponentType'] = $itemNode->localName;
267  } else {
268  continue;
269  }
270  }
271 
272  return [$arguments, $childResult];
273  }
274 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$attributes
Definition: matrix.phtml:13
$arguments
$i
Definition: gallery.phtml:31
__construct(Parser $argumentParser, ParserInterface $parser, ReaderInterface $reader, ConverterUtils $converterUtils)
Definition: Converter.php:84