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