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[][] $methods |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
45 |
|
protected function maybeCallCallableWithArgs( |
46
|
|
|
DOMElement $childNode, |
47
|
|
|
array $commonMethods = [], |
48
|
|
|
array $methods = [], |
49
|
|
|
array $commonArguments = [] |
50
|
|
|
) { |
51
|
45 |
|
foreach ($commonMethods as $commonMethodsSpec) { |
52
|
45 |
|
list ($localNames, $callable, $args) = $commonMethodsSpec; |
53
|
|
|
|
54
|
45 |
|
if (in_array($childNode->localName, $localNames)) { |
55
|
45 |
|
return call_user_func_array($callable, $args); |
56
|
|
|
} |
57
|
|
|
} |
58
|
45 |
|
foreach ($commonArguments as $commonArgumentSpec) { |
59
|
45 |
|
list ($callables, $args) = $commonArgumentSpec; |
60
|
|
|
|
61
|
45 |
|
if (isset($callables[$childNode->localName])) { |
62
|
45 |
|
return call_user_func_array( |
63
|
45 |
|
$callables[$childNode->localName], |
64
|
45 |
|
$args |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
45 |
|
if (isset($methods[$childNode->localName])) { |
69
|
45 |
|
list ($callable, $args) = $methods[$childNode->localName]; |
70
|
|
|
|
71
|
45 |
|
return call_user_func_array($callable, $args); |
72
|
|
|
} |
73
|
45 |
|
} |
74
|
|
|
|
75
|
45 |
View Code Duplication |
protected function maybeLoadSequenceFromElementContainer( |
|
|
|
|
76
|
|
|
BaseComplexType $type, |
77
|
|
|
DOMElement $childNode |
78
|
|
|
) : void { |
79
|
45 |
|
if (! ($type instanceof ElementContainer)) { |
80
|
|
|
throw new RuntimeException( |
81
|
|
|
'$type passed to ' . |
82
|
|
|
__FUNCTION__ . |
83
|
|
|
'expected to be an instance of ' . |
84
|
|
|
ElementContainer::class . |
85
|
|
|
' when child node localName is "group", ' . |
86
|
|
|
get_class($type) . |
87
|
|
|
' given.' |
88
|
|
|
); |
89
|
|
|
} |
90
|
45 |
|
$this->loadSequence($type, $childNode); |
91
|
45 |
|
} |
92
|
|
|
|
93
|
|
|
protected function makeCallbackCallback( |
94
|
|
|
Type $type, |
95
|
|
|
DOMElement $node, |
96
|
|
|
Closure $callbackCallback, |
97
|
|
|
Closure $callback = null |
98
|
|
|
) : Closure { |
99
|
45 |
|
return function ( |
100
|
|
|
) use ( |
101
|
45 |
|
$type, |
102
|
45 |
|
$node, |
103
|
45 |
|
$callbackCallback, |
104
|
45 |
|
$callback |
105
|
|
|
) : void { |
106
|
45 |
|
$this->runCallbackAgainstDOMNodeList( |
107
|
45 |
|
$type, |
108
|
45 |
|
$node, |
109
|
45 |
|
$callbackCallback, |
110
|
45 |
|
$callback |
111
|
|
|
); |
112
|
45 |
|
}; |
113
|
|
|
} |
114
|
|
|
|
115
|
45 |
|
protected function runCallbackAgainstDOMNodeList( |
116
|
|
|
Type $type, |
117
|
|
|
DOMElement $node, |
118
|
|
|
Closure $againstNodeList, |
119
|
|
|
Closure $callback = null |
120
|
|
|
) : void { |
121
|
45 |
|
$this->fillTypeNode($type, $node, true); |
122
|
|
|
|
123
|
45 |
|
foreach ($node->childNodes as $childNode) { |
124
|
45 |
|
if ($childNode instanceof DOMElement) { |
125
|
45 |
|
$againstNodeList( |
126
|
45 |
|
$node, |
127
|
45 |
|
$childNode |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
45 |
|
if ($callback) { |
133
|
45 |
|
call_user_func($callback, $type); |
134
|
|
|
} |
135
|
45 |
|
} |
136
|
|
|
|
137
|
45 |
View Code Duplication |
protected function maybeLoadExtensionFromBaseComplexType( |
|
|
|
|
138
|
|
|
Type $type, |
139
|
|
|
DOMElement $childNode |
140
|
|
|
) : void { |
141
|
45 |
|
if (! ($type instanceof BaseComplexType)) { |
142
|
|
|
throw new RuntimeException( |
143
|
|
|
'Argument 1 passed to ' . |
144
|
|
|
__METHOD__ . |
145
|
|
|
' needs to be an instance of ' . |
146
|
|
|
BaseComplexType::class . |
147
|
|
|
' when passed onto ' . |
148
|
|
|
static::class . |
149
|
|
|
'::loadExtension(), ' . |
150
|
|
|
get_class($type) . |
151
|
|
|
' given.' |
152
|
|
|
); |
153
|
|
|
} |
154
|
45 |
|
$this->loadExtension($type, $childNode); |
155
|
45 |
|
} |
156
|
|
|
} |
157
|
|
|
|
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.