Completed
Pull Request — master (#12)
by
unknown
03:01
created

Schema::findSomething()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 7.551
c 0
b 0
f 0
cc 7
eloc 11
nc 6
nop 3
crap 7
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
    protected $elementsQualification = false;
18
19
    protected $attributesQualification = false;
20
21
    protected $targetNamespace;
22
23
    protected $schemas = array();
24
25
    protected $types = array();
26
27
    protected $elements = array();
28
29
    protected $groups = array();
30
31
    protected $attributeGroups = array();
32
33
    protected $attributes = array();
34
35
    protected $doc;
36
37
    private $typeCache = array();
38
39
40
    public function getElementsQualification()
41
    {
42
        return $this->elementsQualification;
43
    }
44
45 44
    public function setElementsQualification($elementsQualification)
46
    {
47 44
        $this->elementsQualification = $elementsQualification;
48 44
    }
49
50
    public function getAttributesQualification()
51
    {
52
        return $this->attributesQualification;
53
    }
54
55 44
    public function setAttributesQualification($attributesQualification)
56
    {
57 44
        $this->attributesQualification = $attributesQualification;
58 44
    }
59
60 44
    public function getTargetNamespace()
61
    {
62 44
        return $this->targetNamespace;
63
    }
64
65 44
    public function setTargetNamespace($targetNamespace)
66
    {
67 44
        $this->targetNamespace = $targetNamespace;
68 44
    }
69
70 4
    public function getTypes()
71
    {
72 4
        return $this->types;
73
    }
74
75 1
    public function getElements()
76
    {
77 1
        return $this->elements;
78
    }
79
80 44
    public function getSchemas()
81
    {
82 44
        return $this->schemas;
83
    }
84
85 1
    public function getAttributes()
86
    {
87 1
        return $this->attributes;
88
    }
89
90 1
    public function getGroups()
91
    {
92 1
        return $this->groups;
93
    }
94
95
    public function getDoc()
96
    {
97
        return $this->doc;
98
    }
99
100 44
    public function setDoc($doc)
101
    {
102 44
        $this->doc = $doc;
103 44
    }
104
105 44
    public function addType(Type $type)
106
    {
107 44
        $this->types[$type->getName()] = $type;
108 44
    }
109
110 44
    public function addElement(ElementDef $element)
111
    {
112 44
        $this->elements[$element->getName()] = $element;
113 44
    }
114
115 44
    public function addSchema(Schema $schema, $namespace = null)
116
    {
117 44
        if ($namespace !== null && $schema->getTargetNamespace() !== $namespace) {
118
            throw new SchemaException(sprintf("The target namespace ('%s') for schema, does not match the declared namespace '%s'", $schema->getTargetNamespace(), $namespace));
119
        }
120
121 44
        if ($namespace !== null) {
122 44
            $this->schemas[$namespace] = $schema;
123 44
        } else {
124 44
            $this->schemas[] = $schema;
125
        }
126 44
    }
127
128 44
    public function addAttribute(AttributeDef $attribute)
129
    {
130 44
        $this->attributes[$attribute->getName()] = $attribute;
131 44
    }
132
133 44
    public function addGroup(Group $group)
134
    {
135 44
        $this->groups[$group->getName()] = $group;
136 44
    }
137
138 44
    public function addAttributeGroup(AttributeGroup $group)
139
    {
140 44
        $this->attributeGroups[$group->getName()] = $group;
141 44
    }
142
143 1
    public function getAttributeGroups()
144
    {
145 1
        return $this->attributeGroups;
146
    }
147
148
    /**
149
     *
150
     * @param string $name
151
     * @return Group|false
152
     */
153 44
    public function getGroup($name)
154
    {
155 44
        if (isset($this->groups[$name])) {
156 44
            return $this->groups[$name];
157
        }
158 1
        return false;
159
    }
160
161
    /**
162
     *
163
     * @param string $name
164
     * @return ElementItem|false
165
     */
166 44
    public function getElement($name)
167
    {
168 44
        if (isset($this->elements[$name])) {
169 44
            return $this->elements[$name];
170
        }
171 1
        return false;
172
    }
173
174
    /**
175
     *
176
     * @param string $name
177
     * @return Type|false
178
     */
179 44
    public function getType($name)
180
    {
181 44
        if (isset($this->types[$name])) {
182 44
            return $this->types[$name];
183
        }
184 1
        return false;
185
    }
186
187
    /**
188
     *
189
     * @param string $name
190
     * @return AttributeItem|false
191
     */
192 44
    public function getAttribute($name)
193
    {
194 44
        if (isset($this->attributes[$name])) {
195 44
            return $this->attributes[$name];
196
        }
197 1
        return false;
198
    }
199
200
    /**
201
     *
202
     * @param string $name
203
     * @return AttributeGroup|false
204
     */
205 44
    public function getAttributeGroup($name)
206
    {
207 44
        if (isset($this->attributeGroups[$name])) {
208 44
            return $this->attributeGroups[$name];
209
        }
210 1
        return false;
211
    }
212
213
    public function __toString()
214
    {
215
        return sprintf("Target namespaace %s", $this->getTargetNamespace());
216
    }
217
218
    /**
219
     *
220
     * @param string $getter
221
     * @param string $name
222
     * @param string $namespace
223
     * @throws TypeNotFoundException
224
     * @return \GoetasWebservices\XML\XSDReader\Schema\SchemaItem
225
     */
226 44
    protected function findSomething($getter, $name, $namespace = null)
227
    {
228 44
        $cid = "$getter, $name, $namespace";
229
230 44
        if (isset($this->typeCache[$cid])) {
231 44
            return $this->typeCache[$cid];
232
        }
233
234 44
        if (null === $namespace || $this->getTargetNamespace() === $namespace) {
235 44
            if ($item = $this->$getter($name)) {
236 44
                return $this->typeCache[$cid] = $item;
237
            }
238 5
        }
239
240 44
        $targetSchema = $this->findSchemaByNamespace($namespace);
241 44
        if ($targetSchema && $item = $targetSchema->$getter($name)) {
242 44
            return $this->typeCache[$cid] = $item;
243
        }
244
245 5
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
246
    }
247
248
    /**
249
     * @param string $namespace
250
     * @param array $calling
251
     * @return \GoetasWebservices\XML\XSDReader\Schema\Schema|null
252
     */
253 44
    protected function findSchemaByNamespace($namespace, &$calling = array())
254
    {
255 44
        $calling[spl_object_hash($this)] = true;
256 44
        if ($this->getTargetNamespace() === $namespace) {
257 44
            return $this;
258 44
        } elseif ($this->hasChildren()) {
259 44
            foreach ($this->getSchemas() as $schema) {
260 44
                if (isset($calling[spl_object_hash($schema)])) {
261 6
                    continue;
262
                }
263 44
                if ($found = $schema->findSchemaByNamespace($namespace, $calling)) {
264 44
                    return $found;
265
                }
266 6
            }
267 6
        }
268 6
        return null;
269
    }
270
271
    /**
272
     *
273
     * @param string $name
274
     * @param string $namespace
275
     * @return Type
276
     */
277 44
    public function findType($name, $namespace = null)
278
    {
279 44
        return $this->findSomething('getType', $name, $namespace);
280
    }
281
282
    /**
283
     *
284
     * @param string $name
285
     * @param string $namespace
286
     * @return Group
287
     */
288 44
    public function findGroup($name, $namespace = null)
289
    {
290 44
        return $this->findSomething('getGroup', $name, $namespace);
291
    }
292
293
    /**
294
     *
295
     * @param string $name
296
     * @param string $namespace
297
     * @return ElementDef
298
     */
299 44
    public function findElement($name, $namespace = null)
300
    {
301 44
        return $this->findSomething('getElement', $name, $namespace);
302
    }
303
304
    /**
305
     *
306
     * @param string $name
307
     * @param string $namespace
308
     * @return AttributeReal
309
     */
310 44
    public function findAttribute($name, $namespace = null)
311
    {
312 44
        return $this->findSomething('getAttribute', $name, $namespace);
313
    }
314
315
    /**
316
     *
317
     * @param string $name
318
     * @param string $namespace
319
     * @return AttributeGroup
320
     */
321 44
    public function findAttributeGroup($name, $namespace = null)
322
    {
323 44
        return $this->findSomething('getAttributeGroup', $name, $namespace);
324
    }
325
326
    /**
327
     * @return bool
328
     */
329 44
    public function hasChildren()
330
    {
331 44
        return count($this->getSchemas()) > 0;
332
    }
333
}
334