Passed
Push — static-analysis ( 41a0c2...9e3b11 )
by SignpostMarv
01:37
created

Schema::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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