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