Passed
Push — static-analysis ( d0f53e...7cdbe5 )
by SignpostMarv
02:41
created

Schema::setSchemaThingsFromNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

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