-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathConverter.php
112 lines (106 loc) · 3.53 KB
/
Converter.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Config;
use Magento\Framework\View\Xsd\Media\TypeDataExtractorPool;
/**
* Class Converter convert xml to appropriate array
*
* @package Magento\Framework\Config
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**
* @var \Magento\Framework\View\Xsd\Media\TypeDataExtractorPool
*/
protected $extractorPool;
/**
* @param TypeDataExtractorPool $extractorPool
*/
public function __construct(TypeDataExtractorPool $extractorPool)
{
$this->extractorPool = $extractorPool;
}
/**
* Convert dom node tree to array
*
* @param \DOMDocument $source
* @return array
* @throws \InvalidArgumentException
*/
public function convert($source)
{
$xpath = new \DOMXPath($source);
$output = [];
foreach ($xpath->evaluate('/view') as $typeNode) {
foreach ($typeNode->childNodes as $childNode) {
if ($childNode->nodeType != XML_ELEMENT_NODE) {
continue;
}
$result = $this->parseNodes($childNode);
$output = array_merge_recursive($output, $result);
}
}
return $output;
}
/**
* Parse node values from xml nodes
*
* @param \DOMElement $childNode
* @return array
*/
protected function parseNodes($childNode)
{
$output = [];
switch ($childNode->nodeName) {
case 'vars':
$moduleName = $childNode->getAttribute('module');
$output[$childNode->tagName][$moduleName] = $this->parseVarElement($childNode);
break;
case 'exclude':
/** @var $itemNode \DOMElement */
foreach ($childNode->getElementsByTagName('item') as $itemNode) {
$itemType = $itemNode->getAttribute('type');
$output[$childNode->tagName][$itemType][] = $itemNode->nodeValue;
}
break;
case 'media':
foreach ($childNode->childNodes as $mediaNode) {
if ($mediaNode instanceof \DOMElement) {
$mediaNodesArray =
$this->extractorPool->nodeProcessor($mediaNode->tagName)->process(
$mediaNode,
$childNode->tagName
);
$output = array_merge_recursive($output, $mediaNodesArray);
}
}
break;
}
return $output;
}
/**
* Recursive parser for <var> nodes
*
* @param \DOMElement $node
* @return string|boolean|number|null|[]
*/
protected function parseVarElement(\DOMElement $node)
{
$result = [];
for ($varNode = $node->firstChild; $varNode !== null; $varNode = $varNode->nextSibling) {
if ($varNode instanceof \DOMElement && $varNode->tagName == "var") {
$varName = $varNode->getAttribute('name');
$result[$varName] = $this->parseVarElement($varNode);
}
}
if (!count($result)) {
$result = (strtolower($node->nodeValue) !== 'true' && strtolower($node->nodeValue) !== 'false')
? $node->nodeValue
: filter_var($node->nodeValue, FILTER_VALIDATE_BOOLEAN);
}
return $result;
}
}