1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Element\UiComponent\Config;
use Magento\Framework\Config\ConverterInterface;
use Magento\Framework\View\Layout\Argument\Parser;
/**
* Class Converter
*/
class Converter implements ConverterInterface
{
/**
* The key attributes of a node
*/
const DATA_ATTRIBUTES_KEY = '@attributes';
/**
* The key for the data arguments
*/
const DATA_ARGUMENTS_KEY = '@arguments';
/**
* The key of the argument node
*/
const ARGUMENT_KEY = 'argument';
/**
* Key name attribute value
*/
const NAME_ATTRIBUTE_KEY = 'name';
/**
* @var Parser
*/
protected $argumentParser;
/**
* Constructor
*
* @param Parser $argumentParser
*/
public function __construct(Parser $argumentParser)
{
$this->argumentParser = $argumentParser;
}
/**
* Transform Xml to array
*
* @param \DOMNode $node
* @return array|string
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function toArray(\DOMNode $node)
{
$result = [];
$attributes = [];
// Collect data from attributes
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$attributes[$attribute->name] = $attribute->value;
}
}
switch ($node->nodeType) {
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
case XML_CDATA_SECTION_NODE:
break;
default:
if ($node->localName === static::ARGUMENT_KEY) {
if (!isset($attributes[static::NAME_ATTRIBUTE_KEY])) {
throw new \InvalidArgumentException(
'Attribute "' . static::NAME_ATTRIBUTE_KEY . '" is absent in the attributes node.'
);
}
$result[ $attributes[static::NAME_ATTRIBUTE_KEY] ] = $this->argumentParser->parse($node);
} else {
$arguments = [];
for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) {
$itemNode = $node->childNodes->item($i);
if (empty($itemNode->localName)) {
continue;
}
if ($itemNode->nodeName === static::ARGUMENT_KEY) {
$arguments += $this->toArray($itemNode);
} else {
$result[$itemNode->localName][] = $this->toArray($itemNode);
}
}
if (!empty($arguments)) {
$result[static::DATA_ARGUMENTS_KEY] = $arguments;
}
if (!empty($attributes)) {
$result[static::DATA_ATTRIBUTES_KEY] = $attributes;
}
}
break;
}
return $result;
}
/**
* Convert configuration
*
* @param \DOMDocument|null $source
* @return array
*/
public function convert($source)
{
if ($source === null) {
return [];
}
return $this->toArray($source);
}
}