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