Passed
Push — static-analysis ( d8a1cf...2cdeb5 )
by SignpostMarv
07:38 queued 04:26
created

Schema::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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