1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Xervice\DataProvider\Parser; |
6
|
|
|
|
7
|
|
|
use Xervice\DataProvider\DataProvider\AnyDataProvider; |
8
|
|
|
use Xervice\DataProvider\DataProvider\DataProviderInterface; |
9
|
|
|
|
10
|
|
|
class XmlMerger implements XmlMergerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $mergedXml = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $xmlContent |
19
|
|
|
*/ |
20
|
3 |
|
public function addXml(string $xmlContent): void |
21
|
|
|
{ |
22
|
3 |
|
$xml = \simplexml_load_string($xmlContent); |
23
|
|
|
|
24
|
3 |
|
foreach ($xml->DataProvider as $xmlDataProvider) { |
25
|
3 |
|
$dataProvider = $this->parseDataProvider($xmlDataProvider); |
26
|
|
|
|
27
|
3 |
|
foreach ($xmlDataProvider->DataElement as $element) { |
28
|
3 |
|
$fieldName = (string)$element->attributes()['name']; |
29
|
|
|
|
30
|
3 |
|
if (isset($this->mergedXml[$dataProvider][$fieldName])) { |
31
|
|
|
$this->mergedXml[$dataProvider][$fieldName] = array_merge( |
32
|
|
|
$this->mergedXml[$dataProvider][$fieldName], |
33
|
|
|
$this->getElementData($element) |
34
|
|
|
); |
35
|
|
|
} else { |
36
|
3 |
|
$this->mergedXml[$dataProvider][$fieldName] = $this->getElementData($element); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
3 |
|
public function getData(): array |
48
|
|
|
{ |
49
|
3 |
|
return $this->mergedXml; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \SimpleXMLElement $xmlDataProvider |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
private function parseDataProvider(\SimpleXMLElement $xmlDataProvider): string |
58
|
|
|
{ |
59
|
3 |
|
$dataProvider = (string)$xmlDataProvider->attributes()['name']; |
60
|
3 |
|
if (!isset($this->mergedXml[$dataProvider])) { |
61
|
3 |
|
$this->mergedXml[$dataProvider] = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $dataProvider; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $element |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
3 |
|
private function getElementData(\SimpleXMLElement $element): array |
73
|
|
|
{ |
74
|
3 |
|
$type = (string)$element->attributes()['type']; |
75
|
|
|
|
76
|
|
|
$data = [ |
77
|
3 |
|
'name' => (string)$element->attributes()['name'], |
78
|
3 |
|
'allownull' => (bool)$element->attributes()['allownull'], |
79
|
3 |
|
'default' => (string)$element->attributes()['default'], |
80
|
3 |
|
'type' => $this->getVariableType($type), |
81
|
3 |
|
'is_collection' => $this->isCollection($type), |
82
|
3 |
|
'is_dataprovider' => $this->isDataProvider($type) |
83
|
|
|
]; |
84
|
|
|
|
85
|
3 |
|
$singleton = (string)$element->attributes()['singleton']; |
86
|
3 |
|
if ($singleton !== '') { |
87
|
3 |
|
$data['singleton'] = (string)$element->attributes()['singleton']; |
88
|
3 |
|
$data['singleton_type'] = $this->getSingleVariableType($type); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
return $data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $type |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
3 |
|
private function isDataProvider(string $type) : bool |
100
|
|
|
{ |
101
|
3 |
|
return (!$this->isSimpleType($type) && !$this->isCollection($type)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $type |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
3 |
|
private function isCollection(string $type) : bool |
110
|
|
|
{ |
111
|
3 |
|
return strpos($type, '[]') !== false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $type |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
3 |
|
private function isSimpleType(string $type): bool |
120
|
|
|
{ |
121
|
|
|
$validTypes = [ |
122
|
3 |
|
'int', |
123
|
|
|
'string', |
124
|
|
|
'bool', |
125
|
|
|
'double', |
126
|
|
|
'array', |
127
|
|
|
'DataProviderInterface', |
128
|
|
|
'DataProviderInterface[]', |
129
|
|
|
DataProviderInterface::class, |
130
|
|
|
DataProviderInterface::class . '[]' |
131
|
|
|
]; |
132
|
|
|
|
133
|
3 |
|
return \in_array($type, $validTypes, true); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $type |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
3 |
|
private function getVariableType(string $type): string |
142
|
|
|
{ |
143
|
3 |
|
if (!$this->isSimpleType($type)) { |
144
|
3 |
|
$type = '\DataProvider\\' . $type . 'DataProvider'; |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
if ($type === 'DataProviderInterface') { |
148
|
3 |
|
$type = '\\' . DataProviderInterface::class; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
if ($type === 'DataProviderInterface[]') { |
152
|
3 |
|
$type = '\\' . DataProviderInterface::class . '[]'; |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
if (strpos($type, '[]') !== false) { |
156
|
3 |
|
$type = str_replace('[]', '', $type) . '[]'; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
return $type; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $type |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
3 |
|
private function getSingleVariableType(string $type): string |
168
|
|
|
{ |
169
|
3 |
|
if (!$this->isSimpleType($type)) { |
170
|
3 |
|
$type = '\DataProvider\\' . $type . 'DataProvider'; |
171
|
|
|
} |
172
|
|
|
|
173
|
3 |
|
if ($type === 'DataProviderInterface') { |
174
|
|
|
$type = '\\' . DataProviderInterface::class; |
175
|
|
|
} |
176
|
|
|
|
177
|
3 |
|
if ($type === 'DataProviderInterface[]') { |
178
|
3 |
|
$type = '\\' . DataProviderInterface::class . '[]'; |
179
|
|
|
} |
180
|
|
|
|
181
|
3 |
|
if (strpos($type, '[]') !== false) { |
182
|
3 |
|
$type = str_replace('[]', '', $type); |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
return $type; |
186
|
|
|
} |
187
|
|
|
} |