forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDom.php
131 lines (121 loc) · 4.62 KB
/
Dom.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\ObjectManager\Config\Mapper;
use Magento\FunctionalTestingFramework\Data\Argument\InterpreterInterface;
use Magento\FunctionalTestingFramework\Stdlib\BooleanUtils;
// @codingStandardsIgnoreFile
class Dom implements \Magento\FunctionalTestingFramework\Config\ConverterInterface
{
/**
* @var BooleanUtils
*/
private $booleanUtils;
/**
* @var ArgumentParser
*/
private $argumentParser;
/**
* @var InterpreterInterface
*/
private $argumentInterpreter;
/**
* @param BooleanUtils $booleanUtils
* @param ArgumentParser $argumentParser
* @param InterpreterInterface $argumentInterpreter
*/
public function __construct(
InterpreterInterface $argumentInterpreter,
BooleanUtils $booleanUtils = null,
ArgumentParser $argumentParser = null
) {
$this->argumentInterpreter = $argumentInterpreter;
$this->booleanUtils = $booleanUtils ?: new BooleanUtils();
$this->argumentParser = $argumentParser ?: new ArgumentParser();
}
/**
* Convert configuration in DOM format to assoc array that can be used by object manager
*
* @param \DOMDocument $config
* @return array
* @throws \Exception
* @todo this method has high cyclomatic complexity in order to avoid performance issues
*/
public function convert($config)
{
$output = [];
/** @var \DOMNode $node */
foreach ($config->documentElement->childNodes as $node) {
if ($node->nodeType != XML_ELEMENT_NODE) {
continue;
}
switch ($node->nodeName) {
case 'preference':
$output['preferences'][$node->attributes->getNamedItem(
'for'
)->nodeValue] = $node->attributes->getNamedItem(
'type'
)->nodeValue;
break;
case 'type':
case 'virtualType':
$typeData = [];
$typeNodeAttributes = $node->attributes;
$typeNodeShared = $typeNodeAttributes->getNamedItem('shared');
if ($typeNodeShared) {
$typeData['shared'] = $this->booleanUtils->toBoolean($typeNodeShared->nodeValue);
}
if ($node->nodeName == 'virtualType') {
$attributeType = $typeNodeAttributes->getNamedItem('type');
// attribute type is required for virtual type only in merged configuration
if ($attributeType) {
$typeData['type'] = $attributeType->nodeValue;
}
}
$typeData['arguments'] = $this->setTypeArguments($node);
$output[$typeNodeAttributes->getNamedItem('name')->nodeValue] = $typeData;
break;
default:
throw new \Exception("Invalid application config. Unknown node: {$node->nodeName}.");
}
}
return $output;
}
/** Read typeChildNodes and set typeArguments
* @param DOMNode $node
* @return mixed
* @throws \Exception
*/
private function setTypeArguments($node)
{
$typeArguments = [];
foreach ($node->childNodes as $typeChildNode) {
/** @var \DOMNode $typeChildNode */
if ($typeChildNode->nodeType != XML_ELEMENT_NODE) {
continue;
}
switch ($typeChildNode->nodeName) {
case 'arguments':
/** @var \DOMNode $argumentNode */
foreach ($typeChildNode->childNodes as $argumentNode) {
if ($argumentNode->nodeType != XML_ELEMENT_NODE) {
continue;
}
$argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue;
$argumentData = $this->argumentParser->parse($argumentNode);
$typeArguments[$argumentName] = $this->argumentInterpreter->evaluate(
$argumentData
);
}
break;
default:
throw new \Exception(
"Invalid application config. Unknown node: {$typeChildNode->nodeName}."
);
}
}
return $typeArguments;
}
}