Passed
Pull Request — master (#18)
by SignpostMarv
05:20
created

Schema::findSomething()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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