1
|
|
|
<?php |
2
|
|
|
namespace Goetas\XML\XSDReader\Schema; |
3
|
|
|
|
4
|
|
|
use Goetas\XML\XSDReader\Schema\Type\Type; |
5
|
|
|
use Goetas\XML\XSDReader\Schema\Attribute\Group as AttributeGroup; |
6
|
|
|
use Goetas\XML\XSDReader\Schema\Element\Group; |
7
|
|
|
use Goetas\XML\XSDReader\Schema\Element\ElementDef; |
8
|
|
|
use Goetas\XML\XSDReader\Schema\Element\ElementItem; |
9
|
|
|
use Goetas\XML\XSDReader\Schema\Exception\TypeNotFoundException; |
10
|
|
|
use Goetas\XML\XSDReader\Schema\Exception\SchemaException; |
11
|
|
|
use Goetas\XML\XSDReader\Schema\Attribute\AttributeItem; |
12
|
|
|
use Goetas\XML\XSDReader\Schema\Attribute\AttributeDef; |
13
|
|
|
|
14
|
|
|
class Schema |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $elementsQualification = true; |
18
|
|
|
|
19
|
|
|
protected $attributesQualification = false; |
20
|
|
|
|
21
|
|
|
protected $targetNamespace; |
22
|
|
|
|
23
|
|
|
protected $schemas = array(); |
24
|
|
|
|
25
|
|
|
protected $types = array(); |
26
|
|
|
|
27
|
|
|
protected $elements = array(); |
28
|
|
|
|
29
|
|
|
protected $groups = array(); |
30
|
|
|
|
31
|
|
|
protected $attributeGroups = array(); |
32
|
|
|
|
33
|
|
|
protected $attributes = array(); |
34
|
|
|
|
35
|
|
|
protected $doc; |
36
|
|
|
|
37
|
|
|
private $typeCache = array(); |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function getElementsQualification() |
41
|
|
|
{ |
42
|
|
|
return $this->elementsQualification; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setElementsQualification($elementsQualification) |
46
|
|
|
{ |
47
|
|
|
$this->elementsQualification = $elementsQualification; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getAttributesQualification() |
51
|
|
|
{ |
52
|
|
|
return $this->attributesQualification; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setAttributesQualification($attributesQualification) |
56
|
|
|
{ |
57
|
|
|
$this->attributesQualification = $attributesQualification; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getTargetNamespace() |
61
|
|
|
{ |
62
|
|
|
return $this->targetNamespace; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setTargetNamespace($targetNamespace) |
66
|
|
|
{ |
67
|
|
|
$this->targetNamespace = $targetNamespace; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getTypes() |
71
|
|
|
{ |
72
|
|
|
return $this->types; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getElements() |
76
|
|
|
{ |
77
|
|
|
return $this->elements; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getSchemas() |
81
|
|
|
{ |
82
|
|
|
return $this->schemas; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getAttributes() |
86
|
|
|
{ |
87
|
|
|
return $this->attributes; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getGroups() |
91
|
|
|
{ |
92
|
|
|
return $this->groups; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getDoc() |
96
|
|
|
{ |
97
|
|
|
return $this->doc; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setDoc($doc) |
101
|
|
|
{ |
102
|
|
|
$this->doc = $doc; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function addType(Type $type) |
106
|
|
|
{ |
107
|
|
|
$this->types[$type->getName()] = $type; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function addElement(ElementDef $element) |
111
|
|
|
{ |
112
|
|
|
$this->elements[$element->getName()] = $element; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function addSchema(Schema $schema, $namespace = null) |
116
|
|
|
{ |
117
|
|
|
if ($namespace !== null && $schema->getTargetNamespace() !== $namespace) { |
118
|
|
|
throw new SchemaException(sprintf("The target namespace ('%s') for schema, does not match the declared namespace '%s'", $schema->getTargetNamespace(), $namespace)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($namespace !== null) { |
122
|
|
|
$this->schemas[$namespace] = $schema; |
123
|
|
|
} else { |
124
|
|
|
$this->schemas[] = $schema; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function addAttribute(AttributeDef $attribute) |
129
|
|
|
{ |
130
|
|
|
$this->attributes[$attribute->getName()] = $attribute; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function addGroup(Group $group) |
134
|
|
|
{ |
135
|
|
|
$this->groups[$group->getName()] = $group; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function addAttributeGroup(AttributeGroup $group) |
139
|
|
|
{ |
140
|
|
|
$this->attributeGroups[$group->getName()] = $group; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getAttributeGroups() |
144
|
|
|
{ |
145
|
|
|
return $this->attributeGroups; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
* @param string $name |
151
|
|
|
* @return Group|false |
152
|
|
|
*/ |
153
|
|
|
public function getGroup($name) |
154
|
|
|
{ |
155
|
|
|
if (isset($this->groups[$name])) { |
156
|
|
|
return $this->groups[$name]; |
157
|
|
|
} |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* |
163
|
|
|
* @param string $name |
164
|
|
|
* @return ElementItem|false |
165
|
|
|
*/ |
166
|
|
|
public function getElement($name) |
167
|
|
|
{ |
168
|
|
|
if (isset($this->elements[$name])) { |
169
|
|
|
return $this->elements[$name]; |
170
|
|
|
} |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* @param string $name |
177
|
|
|
* @return Type|false |
178
|
|
|
*/ |
179
|
|
|
public function getType($name) |
180
|
|
|
{ |
181
|
|
|
if (isset($this->types[$name])) { |
182
|
|
|
return $this->types[$name]; |
183
|
|
|
} |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
* @param string $name |
190
|
|
|
* @return AttributeItem|false |
191
|
|
|
*/ |
192
|
|
|
public function getAttribute($name) |
193
|
|
|
{ |
194
|
|
|
if (isset($this->attributes[$name])) { |
195
|
|
|
return $this->attributes[$name]; |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
* @param string $name |
203
|
|
|
* @return AttributeGroup|false |
204
|
|
|
*/ |
205
|
|
|
public function getAttributeGroup($name) |
206
|
|
|
{ |
207
|
|
|
if (isset($this->attributeGroups[$name])) { |
208
|
|
|
return $this->attributeGroups[$name]; |
209
|
|
|
} |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function __toString() |
214
|
|
|
{ |
215
|
|
|
return sprintf("Target namespaace %s", $this->getTargetNamespace()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* |
220
|
|
|
* @param string $getter |
221
|
|
|
* @param string $name |
222
|
|
|
* @param string $namespace |
223
|
|
|
* @throws TypeNotFoundException |
224
|
|
|
* @return \Goetas\XML\XSDReader\Schema\SchemaItem |
225
|
|
|
*/ |
226
|
|
|
protected function findSomething($getter, $name, $namespace = null, &$calling = array()) |
227
|
|
|
{ |
228
|
|
|
$calling[spl_object_hash($this)] = true; |
229
|
|
|
$cid = "$getter, $name, $namespace"; |
230
|
|
|
|
231
|
|
|
if (isset($this->typeCache[$cid])) { |
232
|
|
|
return $this->typeCache[$cid]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (null === $namespace || $this->getTargetNamespace() === $namespace) { |
236
|
|
|
if ($item = $this->$getter($name)) { |
237
|
|
|
return $this->typeCache[$cid] = $item; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
foreach ($this->getSchemas() as $childSchema) { |
241
|
|
|
if ($childSchema->getTargetNamespace() === $namespace && !isset($calling[spl_object_hash($childSchema)])) { |
242
|
|
|
try { |
243
|
|
|
return $this->typeCache[$cid] = $childSchema->findSomething($getter, $name, $namespace, $calling); |
244
|
|
|
} catch (TypeNotFoundException $e) { |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name)); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* |
253
|
|
|
* @param string $name |
254
|
|
|
* @param string $namespace |
255
|
|
|
* @return Type |
256
|
|
|
*/ |
257
|
|
|
public function findType($name, $namespace = null) |
258
|
|
|
{ |
259
|
|
|
return $this->findSomething('getType', $name, $namespace); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* |
264
|
|
|
* @param string $name |
265
|
|
|
* @param string $namespace |
266
|
|
|
* @return Group |
267
|
|
|
*/ |
268
|
|
|
public function findGroup($name, $namespace = null) |
269
|
|
|
{ |
270
|
|
|
return $this->findSomething('getGroup', $name, $namespace); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* |
275
|
|
|
* @param string $name |
276
|
|
|
* @param string $namespace |
277
|
|
|
* @return ElementDef |
278
|
|
|
*/ |
279
|
|
|
public function findElement($name, $namespace = null) |
280
|
|
|
{ |
281
|
|
|
return $this->findSomething('getElement', $name, $namespace); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* |
286
|
|
|
* @param string $name |
287
|
|
|
* @param string $namespace |
288
|
|
|
* @return AttributeReal |
289
|
|
|
*/ |
290
|
|
|
public function findAttribute($name, $namespace = null) |
291
|
|
|
{ |
292
|
|
|
return $this->findSomething('getAttribute', $name, $namespace); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* |
297
|
|
|
* @param string $name |
298
|
|
|
* @param string $namespace |
299
|
|
|
* @return AttributeGroup |
300
|
|
|
*/ |
301
|
|
|
public function findAttributeGroup($name, $namespace = null) |
302
|
|
|
{ |
303
|
|
|
return $this->findSomething('getAttributeGroup', $name, $namespace); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|