Passed
Push — static-analysis ( a4a47f...f76858 )
by SignpostMarv
01:24
created

Schema::findSomething()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 10
nop 5
dl 0
loc 33
rs 4.909
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
     *
296
     * @param string $getter
297
     * @param string $name
298
     * @param string $namespace
299
     * @param bool[] $calling
300
     * @param bool $throw
301
     *
302
     * @throws TypeNotFoundException
303
     * @return SchemaItem|null
304
     */
305
    protected function findSomething($getter, $name, $namespace = null, &$calling = array(), $throw = true)
306
    {
307
        $calling[spl_object_hash($this)] = true;
308
        $cid = "$getter, $name, $namespace";
309
310
        if (isset($this->typeCache[$cid])) {
311
            return $this->typeCache[$cid];
312
        }
313
314
        if (null === $namespace || $this->getTargetNamespace() === $namespace) {
315
            /**
316
            * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem|null $item
317
            */
318
            $item = $this->$getter($name);
319
            if ($item instanceof SchemaItem) {
320
                return $this->typeCache[$cid] = $item;
321
            }
322
        }
323
        foreach ($this->getSchemas() as $childSchema) {
324
            if (!isset($calling[spl_object_hash($childSchema)])) {
325
                    /**
326
                    * @var \GoetasWebservices\XML\XSDReader\Schema\SchemaItem $in
327
                    */
328
                    $in = $childSchema->findSomething($getter, $name, $namespace, $calling, false);
329
330
                if ($in instanceof SchemaItem) {
331
                    return $this->typeCache[$cid] = $in;
332
                }
333
            }
334
        }
335
336
        if ($throw) {
337
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
338
        }
339
    }
340
341
    /**
342
     *
343
     * @param string $name
344
     * @param string $namespace
345
     * @return Type
346
     */
347
    public function findType($name, $namespace = null)
348
    {
349
        /**
350
        * @var Type $out
351
        */
352
        $out = $this->findSomething('getType', $name, $namespace);
353
354
        return $out;
355
    }
356
357
    /**
358
     *
359
     * @param string $name
360
     * @param string $namespace
361
     * @return Group
362
     */
363
    public function findGroup($name, $namespace = null)
364
    {
365
        /**
366
        * @var Group $out
367
        */
368
        $out = $this->findSomething('getGroup', $name, $namespace);
369
370
        return $out;
371
    }
372
373
    /**
374
     *
375
     * @param string $name
376
     * @param string $namespace
377
     * @return ElementDef
378
     */
379
    public function findElement($name, $namespace = null)
380
    {
381
        /**
382
        * @var ElementDef $out
383
        */
384
        $out = $this->findSomething('getElement', $name, $namespace);
385
386
        return $out;
387
    }
388
389
    /**
390
     *
391
     * @param string $name
392
     * @param string $namespace
393
     * @return AttributeItem
394
     */
395
    public function findAttribute($name, $namespace = null)
396
    {
397
        /**
398
        * @var AttributeItem $out
399
        */
400
        $out = $this->findSomething('getAttribute', $name, $namespace);
401
402
        return $out;
403
    }
404
405
    /**
406
     *
407
     * @param string $name
408
     * @param string $namespace
409
     * @return AttributeGroup
410
     */
411
    public function findAttributeGroup($name, $namespace = null)
412
    {
413
        /**
414
        * @var AttributeGroup
415
        */
416
        $out = $this->findSomething('getAttributeGroup', $name, $namespace);
417
418
        return $out;
419
    }
420
}
421