Passed
Push — static-analysis ( 08a476...ccf5dd )
by SignpostMarv
01:39
created

Schema::findSomethingNoThrowSchemas()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 6
dl 0
loc 14
rs 9.2
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
    * @param string $getter
296
    * @param string $name
297
    * @param string $namespace
298
    * @param bool[] $calling
299
    * @param bool $throw
300
    *
301
    * @return SchemaItem|null
302
    */
303
    protected function findSomethingNoThrow(
304
        $getter,
305
        $name,
306
        $namespace = null,
307
        array & $calling = array()
308
    ) {
309
        $calling[spl_object_hash($this)] = true;
310
        $cid = "$getter, $name, $namespace";
311
312
        if (isset($this->typeCache[$cid])) {
313
            return $this->typeCache[$cid];
314
        }
315
316
        if (null === $namespace || $this->getTargetNamespace() === $namespace) {
317
            /**
318
            * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem|null $item
319
            */
320
            $item = $this->$getter($name);
321
            if ($item instanceof SchemaItem) {
322
                return $this->typeCache[$cid] = $item;
323
            }
324
        }
325
326
        return $this->findSomethingNoThrowSchemas(
327
            $this->getSchemas(),
328
            $cid,
329
            $getter,
330
            $name,
331
            $namespace,
332
            $calling
333
        );
334
    }
335
336
337
    /**
338
    * @param Schema[] $schemas
339
    * @param string $cid
340
    * @param string $getter
341
    * @param string $name
342
    * @param string $namespace
343
    * @param bool[] $calling
344
    * @param bool $throw
345
    *
346
    * @return SchemaItem|null
347
    */
348
    protected function findSomethingNoThrowSchemas(
349
        array $schemas,
350
        $cid,
351
        $getter,
352
        $name,
353
        $namespace = null,
354
        array & $calling = array()
355
    ) {
356
        foreach ($this->getSchemas() as $childSchema) {
357
            if (!isset($calling[spl_object_hash($childSchema)])) {
358
                $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling);
359
360
                if ($in instanceof SchemaItem) {
361
                    return $this->typeCache[$cid] = $in;
362
                }
363
            }
364
        }
365
    }
366
367
    /**
368
     *
369
     * @param string $getter
370
     * @param string $name
371
     * @param string $namespace
372
     * @param bool[] $calling
373
     * @param bool $throw
374
     *
375
     * @throws TypeNotFoundException
376
     * @return SchemaItem
377
     */
378
    protected function findSomething($getter, $name, $namespace = null, &$calling = array())
379
    {
380
        $in = $this->findSomethingNoThrow(
381
            $getter,
382
            $name,
383
            $namespace,
384
            $calling
385
        );
386
387
        if ($in instanceof SchemaItem) {
388
            return $in;
389
        }
390
391
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
392
    }
393
394
    /**
395
     *
396
     * @param string $name
397
     * @param string $namespace
398
     * @return Type
399
     */
400
    public function findType($name, $namespace = null)
401
    {
402
        /**
403
        * @var Type $out
404
        */
405
        $out = $this->findSomething('getType', $name, $namespace);
406
407
        return $out;
408
    }
409
410
    /**
411
     *
412
     * @param string $name
413
     * @param string $namespace
414
     * @return Group
415
     */
416
    public function findGroup($name, $namespace = null)
417
    {
418
        /**
419
        * @var Group $out
420
        */
421
        $out = $this->findSomething('getGroup', $name, $namespace);
422
423
        return $out;
424
    }
425
426
    /**
427
     *
428
     * @param string $name
429
     * @param string $namespace
430
     * @return ElementDef
431
     */
432
    public function findElement($name, $namespace = null)
433
    {
434
        /**
435
        * @var ElementDef $out
436
        */
437
        $out = $this->findSomething('getElement', $name, $namespace);
438
439
        return $out;
440
    }
441
442
    /**
443
     *
444
     * @param string $name
445
     * @param string $namespace
446
     * @return AttributeItem
447
     */
448
    public function findAttribute($name, $namespace = null)
449
    {
450
        /**
451
        * @var AttributeItem $out
452
        */
453
        $out = $this->findSomething('getAttribute', $name, $namespace);
454
455
        return $out;
456
    }
457
458
    /**
459
     *
460
     * @param string $name
461
     * @param string $namespace
462
     * @return AttributeGroup
463
     */
464
    public function findAttributeGroup($name, $namespace = null)
465
    {
466
        /**
467
        * @var AttributeGroup
468
        */
469
        $out = $this->findSomething('getAttributeGroup', $name, $namespace);
470
471
        return $out;
472
    }
473
}
474