1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\XML\XSDReader; |
3
|
|
|
|
4
|
|
|
use Closure; |
5
|
|
|
use DOMDocument; |
6
|
|
|
use DOMElement; |
7
|
|
|
use DOMNode; |
8
|
|
|
use DOMNodeList; |
9
|
|
|
use GoetasWebservices\XML\XSDReader\Exception\IOException; |
10
|
|
|
use GoetasWebservices\XML\XSDReader\Exception\TypeException; |
11
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute; |
12
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef; |
13
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem; |
14
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup; |
15
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\Element; |
16
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer; |
17
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef; |
18
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem; |
19
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef; |
20
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\Group; |
21
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef; |
22
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax; |
23
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException; |
24
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Base; |
25
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Extension; |
26
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction; |
27
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Item; |
28
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Schema; |
29
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem; |
30
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\BaseComplexType; |
31
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; |
32
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexTypeSimpleContent; |
33
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType; |
34
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\Type; |
35
|
|
|
use GoetasWebservices\XML\XSDReader\Utils\UrlUtils; |
36
|
|
|
use RuntimeException; |
37
|
|
|
|
38
|
|
|
abstract class SchemaReaderCallbackAbstraction extends AbstractSchemaReader |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @param mixed[][] $commonMethods |
42
|
|
|
* @param mixed[][] $methods |
43
|
|
|
* @param mixed[][] $commonArguments |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
135 |
|
protected function maybeCallCallableWithArgs( |
48
|
|
|
DOMElement $childNode, |
49
|
|
|
array $commonMethods = [], |
50
|
|
|
array $methods = [], |
51
|
|
|
array $commonArguments = [] |
52
|
|
|
) { |
53
|
135 |
|
foreach ($commonMethods as $commonMethodsSpec) { |
54
|
135 |
|
list ($localNames, $callable, $args) = $commonMethodsSpec; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string[] $localNames |
58
|
|
|
*/ |
59
|
135 |
|
$localNames = $localNames; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var callable $callable |
63
|
|
|
*/ |
64
|
135 |
|
$callable = $callable; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var mixed[] $args |
68
|
|
|
*/ |
69
|
135 |
|
$args = $args; |
70
|
|
|
|
71
|
135 |
|
if (in_array($childNode->localName, $localNames)) { |
72
|
135 |
|
return call_user_func_array($callable, $args); |
73
|
|
|
} |
74
|
45 |
|
} |
75
|
135 |
|
foreach ($commonArguments as $commonArgumentSpec) { |
76
|
|
|
/** |
77
|
|
|
* @var mixed[] $commonArgumentSpec |
78
|
|
|
*/ |
79
|
135 |
|
list ($callables, $args) = $commonArgumentSpec; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var callable[] $callables |
83
|
|
|
*/ |
84
|
135 |
|
$callables = $callables; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var mixed[] |
88
|
|
|
*/ |
89
|
135 |
|
$args = $args; |
90
|
|
|
|
91
|
135 |
|
if (isset($callables[$childNode->localName])) { |
92
|
135 |
|
return call_user_func_array( |
93
|
135 |
|
$callables[$childNode->localName], |
94
|
90 |
|
$args |
95
|
45 |
|
); |
96
|
|
|
} |
97
|
45 |
|
} |
98
|
135 |
|
if (isset($methods[$childNode->localName])) { |
99
|
135 |
|
list ($callable, $args) = $methods[$childNode->localName]; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var callable $callable |
103
|
|
|
*/ |
104
|
135 |
|
$callable = $callable; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var mixed[] $args |
108
|
|
|
*/ |
109
|
135 |
|
$args = $args; |
110
|
|
|
|
111
|
135 |
|
return call_user_func_array($callable, $args); |
112
|
|
|
} |
113
|
135 |
|
} |
114
|
|
|
|
115
|
135 |
|
protected function maybeLoadSequenceFromElementContainer( |
116
|
|
|
BaseComplexType $type, |
117
|
|
|
DOMElement $childNode |
118
|
|
|
) { |
119
|
135 |
|
$this->maybeLoadThingFromThing( |
120
|
135 |
|
$type, |
121
|
135 |
|
$childNode, |
122
|
135 |
|
ElementContainer::class, |
123
|
90 |
|
'loadSequence' |
124
|
45 |
|
); |
125
|
135 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $instanceof |
129
|
|
|
* @param string $passTo |
130
|
|
|
*/ |
131
|
135 |
|
protected function maybeLoadThingFromThing( |
132
|
|
|
Type $type, |
133
|
|
|
DOMElement $childNode, |
134
|
|
|
$instanceof, |
135
|
|
|
$passTo |
136
|
|
|
) { |
137
|
135 |
|
if (! is_a($type, $instanceof, true)) { |
138
|
|
|
/** |
139
|
|
|
* @var string $class |
140
|
|
|
*/ |
141
|
|
|
$class = static::class; |
142
|
|
|
throw new RuntimeException( |
143
|
|
|
'Argument 1 passed to ' . |
144
|
|
|
__METHOD__ . |
145
|
|
|
' needs to be an instance of ' . |
146
|
|
|
$instanceof . |
147
|
|
|
' when passed onto ' . |
148
|
|
|
$class . |
149
|
|
|
'::' . |
150
|
|
|
$passTo . |
151
|
|
|
'(), ' . |
152
|
|
|
(string) get_class($type) . |
153
|
|
|
' given.' |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
135 |
|
$this->$passTo($type, $childNode); |
158
|
135 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Closure|null $callback |
162
|
|
|
* |
163
|
|
|
* @return Closure |
164
|
|
|
*/ |
165
|
|
|
protected function makeCallbackCallback( |
166
|
|
|
Type $type, |
167
|
|
|
DOMElement $node, |
168
|
|
|
Closure $callbackCallback, |
169
|
|
|
$callback = null |
170
|
|
|
) { |
171
|
135 |
|
return function ( |
172
|
|
|
) use ( |
173
|
135 |
|
$type, |
174
|
135 |
|
$node, |
175
|
135 |
|
$callbackCallback, |
176
|
135 |
|
$callback |
177
|
|
|
) { |
178
|
135 |
|
$this->runCallbackAgainstDOMNodeList( |
179
|
135 |
|
$type, |
180
|
135 |
|
$node, |
181
|
135 |
|
$callbackCallback, |
182
|
90 |
|
$callback |
183
|
45 |
|
); |
184
|
135 |
|
}; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param Closure|null $callback |
189
|
|
|
*/ |
190
|
135 |
|
protected function runCallbackAgainstDOMNodeList( |
191
|
|
|
Type $type, |
192
|
|
|
DOMElement $node, |
193
|
|
|
Closure $againstNodeList, |
194
|
|
|
$callback = null |
195
|
1 |
|
) { |
196
|
135 |
|
$this->fillTypeNode($type, $node, true); |
197
|
|
|
|
198
|
135 |
|
$limit = $node->childNodes->length; |
199
|
135 |
View Code Duplication |
for ($i = 0; $i < $limit; $i += 1) { |
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @var DOMNode $childNode |
202
|
|
|
*/ |
203
|
135 |
|
$childNode = $node->childNodes->item($i); |
204
|
|
|
|
205
|
135 |
|
if ($childNode instanceof DOMElement) { |
206
|
135 |
|
$againstNodeList( |
207
|
135 |
|
$node, |
208
|
90 |
|
$childNode |
209
|
45 |
|
); |
210
|
45 |
|
} |
211
|
45 |
|
} |
212
|
|
|
|
213
|
135 |
|
if ($callback) { |
214
|
135 |
|
call_user_func($callback, $type); |
215
|
45 |
|
} |
216
|
135 |
|
} |
217
|
|
|
|
218
|
135 |
|
protected function maybeLoadExtensionFromBaseComplexType( |
219
|
|
|
Type $type, |
220
|
|
|
DOMElement $childNode |
221
|
|
|
) { |
222
|
135 |
|
$this->maybeLoadThingFromThing( |
223
|
135 |
|
$type, |
224
|
135 |
|
$childNode, |
225
|
135 |
|
BaseComplexType::class, |
226
|
90 |
|
'loadExtension' |
227
|
45 |
|
); |
228
|
135 |
|
} |
229
|
|
|
} |
230
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.