|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GoetasWebservices\XML\XSDReader\Schema; |
|
6
|
|
|
|
|
7
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef; |
|
8
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem; |
|
9
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup; |
|
10
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef; |
|
11
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem; |
|
12
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\Group; |
|
13
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Exception\SchemaException; |
|
14
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException; |
|
15
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\Type; |
|
16
|
|
|
|
|
17
|
|
|
class Schema |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param bool[] $calling |
|
21
|
|
|
*/ |
|
22
|
61 |
|
protected function findSomethingNoThrow( |
|
23
|
|
|
string $getter, |
|
24
|
|
|
string $name, |
|
25
|
|
|
string $namespace = null, |
|
26
|
|
|
array &$calling = array() |
|
27
|
|
|
): ? SchemaItem { |
|
28
|
61 |
|
$calling[spl_object_hash($this)] = true; |
|
29
|
61 |
|
$cid = "$getter, $name, $namespace"; |
|
30
|
|
|
|
|
31
|
61 |
|
if (isset($this->typeCache[$cid])) { |
|
32
|
61 |
|
return $this->typeCache[$cid]; |
|
33
|
|
|
} elseif ( |
|
34
|
61 |
|
$this->getTargetNamespace() === $namespace |
|
35
|
|
|
) { |
|
36
|
|
|
/** |
|
37
|
|
|
* @var SchemaItem|null |
|
38
|
|
|
*/ |
|
39
|
61 |
|
$item = $this->$getter($name); |
|
40
|
|
|
|
|
41
|
61 |
|
if ($item instanceof SchemaItem) { |
|
42
|
61 |
|
return $this->typeCache[$cid] = $item; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
61 |
|
return $this->findSomethingNoThrowSchemas( |
|
47
|
61 |
|
$this->getSchemas(), |
|
48
|
61 |
|
$cid, |
|
49
|
61 |
|
$getter, |
|
50
|
61 |
|
$name, |
|
51
|
61 |
|
$namespace, |
|
52
|
61 |
|
$calling |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Schema[] $schemas |
|
58
|
|
|
* @param bool[] $calling |
|
59
|
|
|
*/ |
|
60
|
61 |
|
protected function findSomethingNoThrowSchemas( |
|
61
|
|
|
array $schemas, |
|
62
|
|
|
string $cid, |
|
63
|
|
|
string $getter, |
|
64
|
|
|
string $name, |
|
65
|
|
|
string $namespace = null, |
|
66
|
|
|
array &$calling = array() |
|
67
|
|
|
): ? SchemaItem { |
|
68
|
61 |
|
foreach ($schemas as $childSchema) { |
|
69
|
61 |
|
if (!isset($calling[spl_object_hash($childSchema)])) { |
|
70
|
|
|
/** |
|
71
|
|
|
* @var SchemaItem|null |
|
72
|
|
|
*/ |
|
73
|
61 |
|
$in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling); |
|
74
|
|
|
|
|
75
|
61 |
|
if ($in instanceof SchemaItem) { |
|
76
|
61 |
|
return $this->typeCache[$cid] = $in; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @throws TypeNotFoundException |
|
86
|
|
|
*/ |
|
87
|
61 |
|
protected function findSomething(string $getter, string $name, string $namespace = null, array &$calling = array()): SchemaItem |
|
88
|
|
|
{ |
|
89
|
61 |
|
$in = $this->findSomethingNoThrow( |
|
90
|
61 |
|
$getter, |
|
91
|
61 |
|
$name, |
|
92
|
61 |
|
$namespace, |
|
93
|
61 |
|
$calling |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
61 |
|
if ($in instanceof SchemaItem) { |
|
97
|
61 |
|
return $in; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
throw new TypeNotFoundException( |
|
101
|
5 |
|
sprintf( |
|
102
|
5 |
|
"Can't find the %s named {%s}#%s.", |
|
103
|
5 |
|
(string) substr($getter, 3), |
|
104
|
5 |
|
$namespace, |
|
105
|
5 |
|
$name |
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @var bool |
|
112
|
|
|
*/ |
|
113
|
|
|
protected $elementsQualification = false; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @var bool |
|
117
|
|
|
*/ |
|
118
|
|
|
protected $attributesQualification = false; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var string|null |
|
122
|
|
|
*/ |
|
123
|
|
|
protected $targetNamespace; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @var Schema[] |
|
127
|
|
|
*/ |
|
128
|
|
|
protected $schemas = array(); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var Type[] |
|
132
|
|
|
*/ |
|
133
|
|
|
protected $types = array(); |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @var ElementDef[] |
|
137
|
|
|
*/ |
|
138
|
|
|
protected $elements = array(); |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @var Group[] |
|
142
|
|
|
*/ |
|
143
|
|
|
protected $groups = array(); |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @var AttributeGroup[] |
|
147
|
|
|
*/ |
|
148
|
|
|
protected $attributeGroups = array(); |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @var AttributeDef[] |
|
152
|
|
|
*/ |
|
153
|
|
|
protected $attributes = array(); |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @var string|null |
|
157
|
|
|
*/ |
|
158
|
|
|
protected $doc; |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem[] |
|
162
|
|
|
*/ |
|
163
|
|
|
protected $typeCache = array(); |
|
164
|
|
|
|
|
165
|
1 |
|
public function getElementsQualification(): bool |
|
166
|
|
|
{ |
|
167
|
1 |
|
return $this->elementsQualification; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
61 |
|
public function setElementsQualification(bool $elementsQualification): void |
|
171
|
|
|
{ |
|
172
|
61 |
|
$this->elementsQualification = $elementsQualification; |
|
173
|
61 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getAttributesQualification(): bool |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->attributesQualification; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
61 |
|
public function setAttributesQualification(bool $attributesQualification): void |
|
181
|
|
|
{ |
|
182
|
61 |
|
$this->attributesQualification = $attributesQualification; |
|
183
|
61 |
|
} |
|
184
|
|
|
|
|
185
|
61 |
|
public function getTargetNamespace(): ?string |
|
186
|
|
|
{ |
|
187
|
61 |
|
return $this->targetNamespace; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
61 |
|
public function setTargetNamespace(? string $targetNamespace): void |
|
191
|
|
|
{ |
|
192
|
61 |
|
$this->targetNamespace = $targetNamespace; |
|
193
|
61 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return Type[] |
|
197
|
|
|
*/ |
|
198
|
5 |
|
public function getTypes(): array |
|
199
|
|
|
{ |
|
200
|
5 |
|
return $this->types; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return ElementDef[] |
|
205
|
|
|
*/ |
|
206
|
3 |
|
public function getElements(): array |
|
207
|
|
|
{ |
|
208
|
3 |
|
return $this->elements; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return Schema[] |
|
213
|
|
|
*/ |
|
214
|
61 |
|
public function getSchemas(): array |
|
215
|
|
|
{ |
|
216
|
61 |
|
return $this->schemas; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return AttributeDef[] |
|
221
|
|
|
*/ |
|
222
|
1 |
|
public function getAttributes(): array |
|
223
|
|
|
{ |
|
224
|
1 |
|
return $this->attributes; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return Group[] |
|
229
|
|
|
*/ |
|
230
|
2 |
|
public function getGroups(): array |
|
231
|
|
|
{ |
|
232
|
2 |
|
return $this->groups; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function getDoc(): ?string |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->doc; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
61 |
|
public function setDoc(string $doc): void |
|
241
|
|
|
{ |
|
242
|
61 |
|
$this->doc = $doc; |
|
243
|
61 |
|
} |
|
244
|
|
|
|
|
245
|
61 |
|
public function addType(Type $type): void |
|
246
|
|
|
{ |
|
247
|
61 |
|
$this->types[(string) $type->getName()] = $type; |
|
248
|
61 |
|
} |
|
249
|
|
|
|
|
250
|
61 |
|
public function addElement(ElementDef $element): void |
|
251
|
|
|
{ |
|
252
|
61 |
|
$this->elements[$element->getName()] = $element; |
|
253
|
61 |
|
} |
|
254
|
|
|
|
|
255
|
61 |
|
public function addSchema(self $schema, string $namespace = null): void |
|
256
|
|
|
{ |
|
257
|
61 |
|
if ($namespace !== null) { |
|
258
|
61 |
|
if ($schema->getTargetNamespace() !== $namespace) { |
|
259
|
|
|
throw new SchemaException( |
|
260
|
|
|
sprintf( |
|
261
|
|
|
"The target namespace ('%s') for schema, does not match the declared namespace '%s'", |
|
262
|
|
|
$schema->getTargetNamespace(), |
|
263
|
|
|
$namespace |
|
264
|
|
|
) |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
61 |
|
$this->schemas[$namespace] = $schema; |
|
268
|
|
|
} else { |
|
269
|
61 |
|
$this->schemas[] = $schema; |
|
270
|
|
|
} |
|
271
|
61 |
|
} |
|
272
|
|
|
|
|
273
|
61 |
|
public function addAttribute(AttributeDef $attribute): void |
|
274
|
|
|
{ |
|
275
|
61 |
|
$this->attributes[$attribute->getName()] = $attribute; |
|
276
|
61 |
|
} |
|
277
|
|
|
|
|
278
|
61 |
|
public function addGroup(Group $group): void |
|
279
|
|
|
{ |
|
280
|
61 |
|
$this->groups[$group->getName()] = $group; |
|
281
|
61 |
|
} |
|
282
|
|
|
|
|
283
|
61 |
|
public function addAttributeGroup(AttributeGroup $group): void |
|
284
|
|
|
{ |
|
285
|
61 |
|
$this->attributeGroups[$group->getName()] = $group; |
|
286
|
61 |
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return AttributeGroup[] |
|
290
|
|
|
*/ |
|
291
|
1 |
|
public function getAttributeGroups(): array |
|
292
|
|
|
{ |
|
293
|
1 |
|
return $this->attributeGroups; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
61 |
|
public function getGroup(string $name): ?Group |
|
297
|
|
|
{ |
|
298
|
61 |
|
if (isset($this->groups[$name])) { |
|
299
|
61 |
|
return $this->groups[$name]; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return null; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
61 |
|
public function getElement(string $name): ?ElementItem |
|
306
|
|
|
{ |
|
307
|
61 |
|
if (isset($this->elements[$name])) { |
|
308
|
61 |
|
return $this->elements[$name]; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return null; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
61 |
|
public function getType(string $name): ?Type |
|
315
|
|
|
{ |
|
316
|
61 |
|
if (isset($this->types[$name])) { |
|
317
|
61 |
|
return $this->types[$name]; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return null; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
61 |
|
public function getAttribute(string $name): ? AttributeItem |
|
324
|
|
|
{ |
|
325
|
61 |
|
if (isset($this->attributes[$name])) { |
|
326
|
61 |
|
return $this->attributes[$name]; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return null; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
61 |
|
public function getAttributeGroup(string $name): ?AttributeGroup |
|
333
|
|
|
{ |
|
334
|
61 |
|
if (isset($this->attributeGroups[$name])) { |
|
335
|
61 |
|
return $this->attributeGroups[$name]; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
return null; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
public function __toString(): string |
|
342
|
|
|
{ |
|
343
|
|
|
return sprintf('Target namespace %s', $this->getTargetNamespace()); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
61 |
|
public function findType(string $name, string $namespace = null): Type |
|
347
|
|
|
{ |
|
348
|
61 |
|
$out = $this->findSomething('getType', $name, $namespace); |
|
349
|
|
|
|
|
350
|
61 |
|
if (!($out instanceof Type)) { |
|
351
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", 'Type', $namespace, $name)); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
61 |
|
return $out; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
61 |
|
public function findGroup(string $name, string $namespace = null): Group |
|
358
|
|
|
{ |
|
359
|
61 |
|
$out = $this->findSomething('getGroup', $name, $namespace); |
|
360
|
|
|
|
|
361
|
61 |
|
if (!($out instanceof Group)) { |
|
362
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", 'Group', $namespace, $name)); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
61 |
|
return $out; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
61 |
|
public function findElement(string $name, string $namespace = null): ElementDef |
|
369
|
|
|
{ |
|
370
|
61 |
|
$out = $this->findSomething('getElement', $name, $namespace); |
|
371
|
|
|
|
|
372
|
61 |
|
if (!($out instanceof ElementDef)) { |
|
373
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", 'Element', $namespace, $name)); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
61 |
|
return $out; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
61 |
|
public function findAttribute(string $name, string $namespace = null): AttributeItem |
|
380
|
|
|
{ |
|
381
|
61 |
|
$out = $this->findSomething('getAttribute', $name, $namespace); |
|
382
|
|
|
|
|
383
|
61 |
|
if (!($out instanceof AttributeItem)) { |
|
384
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", 'Attribute', $namespace, $name)); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
61 |
|
return $out; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
61 |
|
public function findAttributeGroup(string $name, string $namespace = null): AttributeGroup |
|
391
|
|
|
{ |
|
392
|
61 |
|
$out = $this->findSomething('getAttributeGroup', $name, $namespace); |
|
393
|
|
|
|
|
394
|
61 |
|
if (!($out instanceof AttributeGroup)) { |
|
395
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", 'AttributeGroup', $namespace, $name)); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
61 |
|
return $out; |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|