Passed
Push — static-analysis ( 87b144...2730da )
by SignpostMarv
05:52
created

Schema::getLoadedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 135
    public function setElementsQualification($elementsQualification)
83
    {
84 135
        $this->elementsQualification = $elementsQualification;
85 135
    }
86
87
    /**
88
    * @return bool
89
    */
90
    public function getAttributesQualification()
91
    {
92
        return $this->attributesQualification;
93
    }
94
95
    /**
96
    * @param bool $attributesQualification
97
    */
98 135
    public function setAttributesQualification($attributesQualification)
99
    {
100 135
        $this->attributesQualification = $attributesQualification;
101 135
    }
102
103
    /**
104
    * @return string|null
105
    */
106 135
    public function getTargetNamespace()
107
    {
108 135
        return $this->targetNamespace;
109
    }
110
111
    /**
112
    * @param string|null $targetNamespace
113
    */
114 135
    public function setTargetNamespace($targetNamespace)
115
    {
116 135
        $this->targetNamespace = $targetNamespace;
117 135
    }
118
119
    /**
120
    * @return Type[]
121
    */
122 15
    public function getTypes()
123
    {
124 15
        return $this->types;
125
    }
126
127
    /**
128
    * @return ElementDef[]
129
    */
130 9
    public function getElements()
131
    {
132 9
        return $this->elements;
133
    }
134
135
    /**
136
    * @return Schema[]
137
    */
138 135
    public function getSchemas()
139
    {
140 135
        return $this->schemas;
141
    }
142
143
    /**
144
    * @return AttributeDef[]
145
    */
146 3
    public function getAttributes()
147
    {
148 3
        return $this->attributes;
149
    }
150
151
    /**
152
    * @return Group[]
153
    */
154 6
    public function getGroups()
155
    {
156 6
        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 135
    public function setDoc($doc)
171
    {
172 135
        $this->doc = $doc;
173 135
    }
174
175 135
    public function addType(Type $type)
176
    {
177 135
        $this->types[$type->getName()] = $type;
178 135
    }
179
180 135
    public function addElement(ElementDef $element)
181
    {
182 135
        $this->elements[$element->getName()] = $element;
183 135
    }
184
185
    /**
186
    * @param string|null $namespace
187
    */
188 135
    public function addSchema(Schema $schema, $namespace = null)
189
    {
190 135
        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 135
        if ($namespace !== null) {
195 135
            $this->schemas[$namespace] = $schema;
196 45
        } else {
197 135
            $this->schemas[] = $schema;
198
        }
199 135
    }
200
201 135
    public function addAttribute(AttributeDef $attribute)
202
    {
203 135
        $this->attributes[$attribute->getName()] = $attribute;
204 135
    }
205
206 135
    public function addGroup(Group $group)
207
    {
208 135
        $this->groups[$group->getName()] = $group;
209 135
    }
210
211 135
    public function addAttributeGroup(AttributeGroup $group)
212
    {
213 135
        $this->attributeGroups[$group->getName()] = $group;
214 135
    }
215
216
    /**
217
    * @return AttributeGroup[]
218
    */
219 3
    public function getAttributeGroups()
220
    {
221 3
        return $this->attributeGroups;
222
    }
223
224
    /**
225
     *
226
     * @param string $name
227
     * @return Group|false
228
     */
229 135
    public function getGroup($name)
230
    {
231 135
        if (isset($this->groups[$name])) {
232 135
            return $this->groups[$name];
233
        }
234 3
        return false;
235
    }
236
237
    /**
238
     *
239
     * @param string $name
240
     * @return ElementItem|false
241
     */
242 135
    public function getElement($name)
243
    {
244 135
        if (isset($this->elements[$name])) {
245 135
            return $this->elements[$name];
246
        }
247 3
        return false;
248
    }
249
250
    /**
251
     *
252
     * @param string $name
253
     * @return Type|false
254
     */
255 135
    public function getType($name)
256
    {
257 135
        if (isset($this->types[$name])) {
258 135
            return $this->types[$name];
259
        }
260 3
        return false;
261
    }
262
263
    /**
264
     *
265
     * @param string $name
266
     * @return AttributeItem|false
267
     */
268 135
    public function getAttribute($name)
269
    {
270 135
        if (isset($this->attributes[$name])) {
271 135
            return $this->attributes[$name];
272
        }
273 3
        return false;
274
    }
275
276
    /**
277
     *
278
     * @param string $name
279
     * @return AttributeGroup|false
280
     */
281 135
    public function getAttributeGroup($name)
282
    {
283 135
        if (isset($this->attributeGroups[$name])) {
284 135
            return $this->attributeGroups[$name];
285
        }
286 3
        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 135
    protected function findSomethingNoThrow(
304
        $getter,
305
        $name,
306
        $namespace = null,
307
        array & $calling = array()
308
    ) {
309 135
        $calling[spl_object_hash($this)] = true;
310 135
        $cid = "$getter, $name, $namespace";
311
312 135
        if (isset($this->typeCache[$cid])) {
313 135
            return $this->typeCache[$cid];
314
        }
315
316 135
        if (null === $namespace || $this->getTargetNamespace() === $namespace) {
317
            /**
318
            * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem|null $item
319
            */
320 135
            $item = $this->$getter($name);
321 135
            if ($item instanceof SchemaItem) {
322 135
                return $this->typeCache[$cid] = $item;
323
            }
324 5
        }
325
326 135
        return $this->findSomethingNoThrowSchemas(
327 135
            $this->getSchemas(),
328 135
            $cid,
329 135
            $getter,
330 135
            $name,
331 135
            $namespace,
332 90
            $calling
333 45
        );
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 135
    protected function findSomethingNoThrowSchemas(
349
        array $schemas,
350
        $cid,
351
        $getter,
352
        $name,
353
        $namespace = null,
354
        array & $calling = array()
355
    ) {
356 135
        foreach ($this->getSchemas() as $childSchema) {
357 135
            if (!isset($calling[spl_object_hash($childSchema)])) {
358 135
                $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling);
359
360 135
                if ($in instanceof SchemaItem) {
361 135
                    return $this->typeCache[$cid] = $in;
362
                }
363 7
            }
364 7
        }
365 21
    }
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 135
    protected function findSomething($getter, $name, $namespace = null, &$calling = array())
379
    {
380 135
        $in = $this->findSomethingNoThrow(
381 135
            $getter,
382 135
            $name,
383 135
            $namespace,
384 90
            $calling
385 45
        );
386
387 135
        if ($in instanceof SchemaItem) {
388 135
            return $in;
389
        }
390
391 15
        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 135
    public function findType($name, $namespace = null)
401
    {
402
        /**
403
        * @var Type $out
404
        */
405 135
        $out = $this->findSomething('getType', $name, $namespace);
406
407 135
        return $out;
408
    }
409
410
    /**
411
     *
412
     * @param string $name
413
     * @param string $namespace
414
     * @return Group
415
     */
416 135
    public function findGroup($name, $namespace = null)
417
    {
418
        /**
419
        * @var Group $out
420
        */
421 135
        $out = $this->findSomething('getGroup', $name, $namespace);
422
423 135
        return $out;
424
    }
425
426
    /**
427
     *
428
     * @param string $name
429
     * @param string $namespace
430
     * @return ElementDef
431
     */
432 135
    public function findElement($name, $namespace = null)
433
    {
434
        /**
435
        * @var ElementDef $out
436
        */
437 135
        $out = $this->findSomething('getElement', $name, $namespace);
438
439 135
        return $out;
440
    }
441
442
    /**
443
     *
444
     * @param string $name
445
     * @param string $namespace
446
     * @return AttributeItem
447
     */
448 135
    public function findAttribute($name, $namespace = null)
449
    {
450
        /**
451
        * @var AttributeItem $out
452
        */
453 135
        $out = $this->findSomething('getAttribute', $name, $namespace);
454
455 135
        return $out;
456
    }
457
458
    /**
459
     *
460
     * @param string $name
461
     * @param string $namespace
462
     * @return AttributeGroup
463
     */
464 135
    public function findAttributeGroup($name, $namespace = null)
465
    {
466
        /**
467
        * @var AttributeGroup
468
        */
469 135
        $out = $this->findSomething('getAttributeGroup', $name, $namespace);
470
471 135
        return $out;
472
    }
473
474
    /**
475
    * @var Schema[]
476
    */
477
    private static $loadedFiles = array();
478
479
    /**
480
    * @param string $key
481
    *
482
    * @return bool
483
    */
484 135
    public static function hasLoadedFile($key)
485
    {
486 135
        return isset(static::$loadedFiles[$key]);
0 ignored issues
show
Bug introduced by
Since $loadedFiles is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $loadedFiles to at least protected.
Loading history...
487
    }
488
489
    /**
490
    * @param string $key
491
    *
492
    * @return Schema
493
    */
494 135
    public static function getLoadedFile($key)
495
    {
496 135
        return static::$loadedFiles[$key];
0 ignored issues
show
Bug introduced by
Since $loadedFiles is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $loadedFiles to at least protected.
Loading history...
497
    }
498
499
    /**
500
    * @param string $key
501
    *
502
    * @return Schema
503
    */
504 135
    public static function setLoadedFile($key, Schema $schema)
505
    {
506 135
        static::$loadedFiles[$key] = $schema;
0 ignored issues
show
Bug introduced by
Since $loadedFiles is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $loadedFiles to at least protected.
Loading history...
507
508 135
        return $schema;
509
    }
510
}
511