Passed
Branch php-7.1 (52dd08)
by SignpostMarv
03:16
created

SchemaReader::loadImportFresh()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.3906

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 4
dl 0
loc 25
ccs 9
cts 12
cp 0.75
crap 5.3906
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace GoetasWebservices\XML\XSDReader;
3
4
use Closure;
5
use DOMDocument;
6
use DOMElement;
7
use DOMNode;
8
use GoetasWebservices\XML\XSDReader\Exception\IOException;
9
use GoetasWebservices\XML\XSDReader\Exception\TypeException;
10
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute;
11
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
12
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
13
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
14
use GoetasWebservices\XML\XSDReader\Schema\Element\Element;
15
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer;
16
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
17
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
18
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef;
19
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
20
use GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef;
21
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax;
22
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
23
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Base;
24
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Extension;
25
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction;
26
use GoetasWebservices\XML\XSDReader\Schema\Item;
27
use GoetasWebservices\XML\XSDReader\Schema\Schema;
28
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem;
29
use GoetasWebservices\XML\XSDReader\Schema\Type\BaseComplexType;
30
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
31
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexTypeSimpleContent;
32
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
33
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
34
use GoetasWebservices\XML\XSDReader\Utils\UrlUtils;
35
use RuntimeException;
36
37
class SchemaReader
38
{
39
40
    const XSD_NS = "http://www.w3.org/2001/XMLSchema";
41
42
    const XML_NS = "http://www.w3.org/XML/1998/namespace";
43
44
    /**
45
    * @var string[]
46
    */
47
    private $knownLocationSchemas = [
48
        'http://www.w3.org/2001/xml.xsd' => (
49
            __DIR__ . '/Resources/xml.xsd'
50
        ),
51
        'http://www.w3.org/2001/XMLSchema.xsd' => (
52
            __DIR__ . '/Resources/XMLSchema.xsd'
53
        ),
54
        'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' => (
55
            __DIR__ . '/Resources/oasis-200401-wss-wssecurity-secext-1.0.xsd'
56
        ),
57
        'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' => (
58
            __DIR__ . '/Resources/oasis-200401-wss-wssecurity-utility-1.0.xsd'
59
        ),
60
        'https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => (
61
            __DIR__ . '/Resources/xmldsig-core-schema.xsd'
62
        ),
63
        'http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => (
64
            __DIR__ . '/Resources/xmldsig-core-schema.xsd'
65
        ),
66
    ];
67
68
    /**
69
    * @var string[]
70
    */
71
    private static $globalSchemaInfo = array(
72
        self::XML_NS => 'http://www.w3.org/2001/xml.xsd',
73
        self::XSD_NS => 'http://www.w3.org/2001/XMLSchema.xsd'
74
    );
75
76 54
    public function __construct()
77
    {
78 54
    }
79
80
    /**
81
    * @param string $remote
82
    * @param string $local
83
    */
84
    public function addKnownSchemaLocation($remote, $local)
85
    {
86
        $this->knownLocationSchemas[$remote] = $local;
87
    }
88
89
    /**
90
    * @return Closure
91
    */
92 45
    private function loadAttributeGroup(Schema $schema, DOMElement $node)
93
    {
94 45
        return AttributeGroup::loadAttributeGroup($this, $schema, $node);
95
    }
96
97
    /**
98
    * @param bool $attributeDef
99
    *
100
    * @return Closure
101
    */
102 45
    private function loadAttributeOrElementDef(
103
        Schema $schema,
104
        DOMElement $node,
105
        $attributeDef
106
    ) {
107 45
        $name = $node->getAttribute('name');
108 45
        if ($attributeDef) {
109 45
            $attribute = new AttributeDef($schema, $name);
110 45
            $schema->addAttribute($attribute);
111
        } else {
112 45
            $attribute = new ElementDef($schema, $name);
113 45
            $schema->addElement($attribute);
114
        }
115
116
117 45
        return function () use ($attribute, $node) {
118 45
            $this->fillItem($attribute, $node);
119 45
        };
120
    }
121
122
    /**
123
    * @return Closure
124
    */
125 45
    private function loadAttributeDef(Schema $schema, DOMElement $node)
126
    {
127 45
        return $this->loadAttributeOrElementDef($schema, $node, true);
128
    }
129
130
    /**
131
     * @param DOMElement $node
132
     * @return string
133
     */
134 45
    public static function getDocumentation(DOMElement $node)
135
    {
136 45
        $doc = '';
137 45
        foreach ($node->childNodes as $childNode) {
138 45
            if ($childNode->localName == "annotation") {
139 45
                $doc .= static::getDocumentation($childNode);
140 45
            } elseif ($childNode->localName == 'documentation') {
141 45
                $doc .= ($childNode->nodeValue);
142
            }
143
        }
144 45
        $doc = preg_replace('/[\t ]+/', ' ', $doc);
145 45
        return trim($doc);
146
    }
147
148 45
    private function setSchemaThingsFromNode(
149
        Schema $schema,
150
        DOMElement $node,
151
        Schema $parent = null
152
    ) {
153 45
        $schema->setDoc(static::getDocumentation($node));
154
155 45
        if ($node->hasAttribute("targetNamespace")) {
156 45
            $schema->setTargetNamespace($node->getAttribute("targetNamespace"));
157
        } elseif ($parent) {
158
            $schema->setTargetNamespace($parent->getTargetNamespace());
159
        }
160 45
        $schema->setElementsQualification($node->getAttribute("elementFormDefault") == "qualified");
161 45
        $schema->setAttributesQualification($node->getAttribute("attributeFormDefault") == "qualified");
162 45
        $schema->setDoc(static::getDocumentation($node));
163 45
    }
164
165
    /**
166
    * @param string $key
167
    *
168
    * @return Closure|null
169
    */
170 45
    public function maybeCallMethod(
171
        array $methods,
172
        $key,
173
        DOMNode $childNode,
174
        ...$args
175
    ) {
176 45
        if ($childNode instanceof DOMElement && isset($methods[$key])) {
177 45
            $method = $methods[$key];
178
179 45
            $append = $this->$method(...$args);
180
181 45
            if ($append instanceof Closure) {
182 45
                return $append;
183
            }
184
        }
185 45
    }
186
187
    /**
188
     *
189
     * @param Schema $schema
190
     * @param DOMElement $node
191
     * @param Schema $parent
192
     * @return Closure[]
193
     */
194 45
    private function schemaNode(Schema $schema, DOMElement $node, Schema $parent = null)
195
    {
196 45
        $this->setSchemaThingsFromNode($schema, $node, $parent);
197 45
        $functions = array();
198
199 45
        static $methods = [
200
            'include' => 'loadImport',
201
            'import' => 'loadImport',
202
            'element' => 'loadElementDef',
203
            'attribute' => 'loadAttributeDef',
204
            'attributeGroup' => 'loadAttributeGroup',
205
            'group' => 'loadGroup',
206
            'complexType' => 'loadComplexType',
207
            'simpleType' => 'loadSimpleType',
208
        ];
209
210 45
        foreach ($node->childNodes as $childNode) {
211 45
            $callback = $this->maybeCallMethod(
212 45
                $methods,
213 45
                (string) $childNode->localName,
214 45
                $childNode,
215 45
                $schema,
216 45
                $childNode
217
            );
218
219 45
            if ($callback instanceof Closure) {
220 45
                $functions[] = $callback;
221
            }
222
        }
223
224 45
        return $functions;
225
    }
226
227
    /**
228
    * @return InterfaceSetMinMax
229
    */
230 45
    public static function maybeSetMax(InterfaceSetMinMax $ref, DOMElement $node)
231
    {
232
        if (
233 45
            $node->hasAttribute("maxOccurs")
234
        ) {
235 45
            $ref->setMax($node->getAttribute("maxOccurs") == "unbounded" ? -1 : (int)$node->getAttribute("maxOccurs"));
236
        }
237
238 45
        return $ref;
239
    }
240
241
    /**
242
    * @return InterfaceSetMinMax
243
    */
244 45
    public static function maybeSetMin(InterfaceSetMinMax $ref, DOMElement $node)
245
    {
246 45
        if ($node->hasAttribute("minOccurs")) {
247 45
            $ref->setMin((int) $node->getAttribute("minOccurs"));
248
        }
249
250 45
        return $ref;
251
    }
252
253
    /**
254
    * @param int|null $max
255
    *
256
    * @return int|null
257
    */
258 45
    private static function loadSequenceNormaliseMax(DOMElement $node, $max)
259
    {
260
        return
261
        (
262 45
            (is_int($max) && (bool) $max) ||
263 45
            $node->getAttribute("maxOccurs") == "unbounded" ||
264 45
            $node->getAttribute("maxOccurs") > 1
265
        )
266
            ? 2
267 45
            : null;
268
    }
269
270
    /**
271
    * @param int|null $max
272
    */
273 45
    private function loadSequence(ElementContainer $elementContainer, DOMElement $node, $max = null)
274
    {
275 45
        $max = static::loadSequenceNormaliseMax($node, $max);
276
277 45
        foreach ($node->childNodes as $childNode) {
278 45
            if ($childNode instanceof DOMElement) {
279 45
                $this->loadSequenceChildNode(
280 45
                    $elementContainer,
281 45
                    $node,
282 45
                    $childNode,
283 45
                    $max
284
                );
285
            }
286
        }
287 45
    }
288
289
    /**
290
    * @param int|null $max
291
    */
292
    private function loadSequenceChildNode(
293
        ElementContainer $elementContainer,
294
        DOMElement $node,
295
        DOMElement $childNode,
296
        $max
297
    ) {
298 45
        $loadSeq = function () use ($elementContainer, $childNode, $max) {
299 45
            $this->loadSequence($elementContainer, $childNode, $max);
300 45
        };
301
        $methods = [
302 45
            'choice' => $loadSeq,
303 45
            'sequence' => $loadSeq,
304 45
            'all' => $loadSeq,
305 45
            'element' => function () use (
306 45
                $elementContainer,
307 45
                $node,
308 45
                $childNode,
309 45
                $max
310
            ) {
311 45
                if ($childNode->hasAttribute("ref")) {
312
                    /**
313
                    * @var ElementDef $referencedElement
314
                    */
315 45
                    $referencedElement = $this->findSomething('findElement', $elementContainer->getSchema(), $node, $childNode->getAttribute("ref"));
316 45
                    $element = ElementRef::loadElementRef(
317 45
                        $referencedElement,
318 45
                        $childNode
319
                    );
320
                } else {
321 45
                    $element = Element::loadElement(
322 45
                        $this,
323 45
                        $elementContainer->getSchema(),
324 45
                        $childNode
325
                    );
326
                }
327 45
                if (is_int($max) && (bool) $max) {
328 45
                    $element->setMax($max);
329
                }
330 45
                $elementContainer->addElement($element);
331 45
            },
332 45
            'group' => function () use (
333 45
                $elementContainer,
334 45
                $node,
335 45
                $childNode
336
            ) {
337 45
                $this->addGroupAsElement(
338 45
                    $elementContainer->getSchema(),
339 45
                    $node,
340 45
                    $childNode,
341 45
                    $elementContainer
342
                );
343 45
            },
344
        ];
345
346 45
        if (isset($methods[$childNode->localName])) {
347 45
            $method = $methods[$childNode->localName];
348 45
            $method();
349
        }
350 45
    }
351
352 45
    private function addGroupAsElement(
353
        Schema $schema,
354
        DOMElement $node,
355
        DOMElement $childNode,
356
        ElementContainer $elementContainer
357
    ) {
358
        /**
359
        * @var Group $referencedGroup
360
        */
361 45
        $referencedGroup = $this->findSomething(
362 45
            'findGroup',
363 45
            $schema,
364 45
            $node,
365 45
            $childNode->getAttribute("ref")
366
        );
367
368 45
        $group = GroupRef::loadGroupRef($referencedGroup, $childNode);
369 45
        $elementContainer->addElement($group);
370 45
    }
371
372 45
    private function maybeLoadSequenceFromElementContainer(
373
        BaseComplexType $type,
374
        DOMElement $childNode
375
    ) {
376 45
        if (! ($type instanceof ElementContainer)) {
377
            throw new RuntimeException(
378
                '$type passed to ' .
379
                __FUNCTION__ .
380
                'expected to be an instance of ' .
381
                ElementContainer::class .
382
                ' when child node localName is "group", ' .
383
                get_class($type) .
384
                ' given.'
385
            );
386
        }
387 45
        $this->loadSequence($type, $childNode);
388 45
    }
389
390
    /**
391
    * @return Closure
392
    */
393 45
    private function loadGroup(Schema $schema, DOMElement $node)
394
    {
395 45
        return Group::loadGroup($this, $schema, $node);
396
    }
397
398
    /**
399
    * @param Closure|null $callback
400
    *
401
    * @return Closure
402
    */
403 45
    private function loadComplexType(Schema $schema, DOMElement $node, $callback = null)
404
    {
405 45
        $isSimple = false;
406
407 45
        foreach ($node->childNodes as $childNode) {
408 45
            if ($childNode->localName === "simpleContent") {
409 2
                $isSimple = true;
410 2
                break;
411
            }
412
        }
413
414 45
        $type = $isSimple ? new ComplexTypeSimpleContent($schema, $node->getAttribute("name")) : new ComplexType($schema, $node->getAttribute("name"));
415
416 45
        $type->setDoc(static::getDocumentation($node));
417 45
        if ($node->getAttribute("name")) {
418 45
            $schema->addType($type);
419
        }
420
421 45
        return $this->makeCallbackCallback(
422 45
            $type,
423 45
            $node,
424 45
                function (
425
                    DOMElement $node,
426
                    DOMElement $childNode
427
                ) use(
428 45
                    $schema,
429 45
                    $type
430
                ) {
431 45
                    $this->loadComplexTypeFromChildNode(
432 45
                        $type,
433 45
                        $node,
434 45
                        $childNode,
435 45
                        $schema
436
                    );
437 45
                },
438 45
            $callback
439
        );
440
    }
441
442
    /**
443
    * @param Closure|null $callback
444
    *
445
    * @return Closure
446
    */
447
    private function makeCallbackCallback(
448
        Type $type,
449
        DOMElement $node,
450
        Closure $callbackCallback,
451
        $callback = null
452
    ) {
453 45
        return function (
454
        ) use (
455 45
            $type,
456 45
            $node,
457 45
            $callbackCallback,
458 45
            $callback
459
        ) {
460 45
            $this->runCallbackAgainstDOMNodeList(
461 45
                $type,
462 45
                $node,
463 45
                $callbackCallback,
464 45
                $callback
465
            );
466 45
        };
467
    }
468
469
    /**
470
    * @param Closure|null $callback
471
    */
472 45
    private function runCallbackAgainstDOMNodeList(
473
        Type $type,
474
        DOMElement $node,
475
        Closure $againstNodeList,
476
        $callback = null
477
    ) {
478 45
        $this->fillTypeNode($type, $node, true);
479
480 45
        foreach ($node->childNodes as $childNode) {
481 45
            if ($childNode instanceof DOMElement) {
482 45
                $againstNodeList(
483 45
                    $node,
484 45
                    $childNode
485
                );
486
            }
487
        }
488
489 45
        if ($callback) {
490 45
            call_user_func($callback, $type);
491
        }
492 45
    }
493
494
    private function loadComplexTypeFromChildNode(
495
        BaseComplexType $type,
496
        DOMElement $node,
497
        DOMElement $childNode,
498
        Schema $schema
499
    ) {
500 45
        $maybeLoadSeq = function () use ($type, $childNode) {
501 45
            $this->maybeLoadSequenceFromElementContainer(
502 45
                $type,
503 45
                $childNode
504
            );
505 45
        };
506
        $methods = [
507 45
            'sequence' => $maybeLoadSeq,
508 45
            'choice' => $maybeLoadSeq,
509 45
            'all' => $maybeLoadSeq,
510 45
            'attribute' => function () use (
511 45
                $childNode,
512 45
                $schema,
513 45
                $node,
514 45
                $type
515
            ) {
516 45
                $attribute = Attribute::getAttributeFromAttributeOrRef(
517 45
                    $this,
518 45
                    $childNode,
519 45
                    $schema,
520 45
                    $node
521
                );
522
523 45
                $type->addAttribute($attribute);
524 45
            },
525 45
            'attributeGroup' => function() use (
526 2
                $schema,
527 2
                $node,
528 2
                $childNode,
529 2
                $type
530
            ) {
531 2
                AttributeGroup::findSomethingLikeThis(
532 2
                    $this,
533 2
                    $schema,
534 2
                    $node,
535 2
                    $childNode,
536 2
                    $type
537
                );
538 45
            },
539
        ];
540
        if (
541 45
            $type instanceof ComplexType
542
        ) {
543 1
            $methods['group'] = function() use (
544 1
                $schema,
545 1
                $node,
546 1
                $childNode,
547 1
                $type
548
            ) {
549 1
                $this->addGroupAsElement(
550 1
                    $schema,
551 1
                    $node,
552 1
                    $childNode,
553 1
                    $type
554
                );
555 1
            };
556
        }
557
558 45
        if (isset($methods[$childNode->localName])) {
559 45
            $method = $methods[$childNode->localName];
560 45
            $method();
561
        }
562 45
    }
563
564
    /**
565
    * @param Closure|null $callback
566
    *
567
    * @return Closure
568
    */
569 45
    private function loadSimpleType(Schema $schema, DOMElement $node, $callback = null)
570
    {
571 45
        $type = new SimpleType($schema, $node->getAttribute("name"));
572 45
        $type->setDoc(static::getDocumentation($node));
573 45
        if ($node->getAttribute("name")) {
574 45
            $schema->addType($type);
575
        }
576
577 45
        static $methods = [
578
            'union' => 'loadUnion',
579
            'list' => 'loadList',
580
        ];
581
582 45
        return $this->makeCallbackCallback(
583 45
            $type,
584 45
            $node,
585 45
            function (
586
                DOMElement $node,
587
                DOMElement $childNode
588
            ) use (
589 45
                $methods,
590 45
                $type
591
            ) {
592 45
                $this->maybeCallMethod(
593 45
                    $methods,
594 45
                    $childNode->localName,
595 45
                    $childNode,
596 45
                    $type,
597 45
                    $childNode
598
                );
599 45
            },
600 45
            $callback
601
        );
602
    }
603
604 45
    private function loadList(SimpleType $type, DOMElement $node)
605
    {
606 45
        if ($node->hasAttribute("itemType")) {
607
            /**
608
            * @var SimpleType $listType
609
            */
610 45
            $listType = $this->findSomeType($type, $node, 'itemType');
611 45
            $type->setList($listType);
612
        } else {
613 45
            $addCallback = function (SimpleType $list) use ($type) {
614 45
                $type->setList($list);
615 45
            };
616
617 45
            Type::loadTypeWithCallbackOnChildNodes(
618 45
                $this,
619 45
                $type->getSchema(),
620 45
                $node,
621 45
                $addCallback
622
            );
623
        }
624 45
    }
625
626
    /**
627
    * @param string $attributeName
628
    *
629
    * @return SchemaItem
630
    */
631 45
    private function findSomeType(
632
        SchemaItem $fromThis,
633
        DOMElement $node,
634
        $attributeName
635
    ) {
636 45
        return $this->findSomeTypeFromAttribute(
637 45
            $fromThis,
638 45
            $node,
639 45
            $node->getAttribute($attributeName)
640
        );
641
    }
642
643
    /**
644
    * @param string $attributeName
645
    *
646
    * @return SchemaItem
647
    */
648 45
    private function findSomeTypeFromAttribute(
649
        SchemaItem $fromThis,
650
        DOMElement $node,
651
        $attributeName
652
    ) {
653
        /**
654
        * @var SchemaItem $out
655
        */
656 45
        $out = $this->findSomething(
657 45
            'findType',
658 45
            $fromThis->getSchema(),
659 45
            $node,
660 45
            $attributeName
661
        );
662
663 45
        return $out;
664
    }
665
666 45
    private function loadUnion(SimpleType $type, DOMElement $node)
667
    {
668 45
        if ($node->hasAttribute("memberTypes")) {
669 45
            $types = preg_split('/\s+/', $node->getAttribute("memberTypes"));
670 45
            foreach ($types as $typeName) {
671
                /**
672
                * @var SimpleType $unionType
673
                */
674 45
                $unionType = $this->findSomeTypeFromAttribute(
675 45
                    $type,
676 45
                    $node,
677 45
                    $typeName
678
                );
679 45
                $type->addUnion($unionType);
680
            }
681
        }
682 45
        $addCallback = function (SimpleType $unType) use ($type) {
683 45
            $type->addUnion($unType);
684 45
        };
685
686 45
        Type::loadTypeWithCallbackOnChildNodes(
687 45
            $this,
688 45
            $type->getSchema(),
689 45
            $node,
690 45
            $addCallback
691
        );
692 45
    }
693
694
    /**
695
    * @param bool $checkAbstract
696
    */
697 45
    private function fillTypeNode(Type $type, DOMElement $node, $checkAbstract = false)
698
    {
699
700 45
        if ($checkAbstract) {
701 45
            $type->setAbstract($node->getAttribute("abstract") === "true" || $node->getAttribute("abstract") === "1");
702
        }
703
704 45
        static $methods = [
705
            'restriction' => 'loadRestriction',
706
            'extension' => 'maybeLoadExtensionFromBaseComplexType',
707
            'simpleContent' => 'fillTypeNode',
708
            'complexContent' => 'fillTypeNode',
709
        ];
710
711 45
        foreach ($node->childNodes as $childNode) {
712 45
            $this->maybeCallMethod(
713 45
                $methods,
714 45
                (string) $childNode->localName,
715 45
                $childNode,
716 45
                $type,
717 45
                $childNode
718
            );
719
        }
720 45
    }
721
722 45
    private function loadExtension(BaseComplexType $type, DOMElement $node)
723
    {
724 45
        $extension = new Extension();
725 45
        $type->setExtension($extension);
726
727 45
        if ($node->hasAttribute("base")) {
728 45
            $this->findAndSetSomeBase(
729 45
                $type,
730 45
                $extension,
731 45
                $node
732
            );
733
        }
734
735 45
        $seqFromElement = function (DOMElement $childNode) use ($type) {
736 45
            $this->maybeLoadSequenceFromElementContainer(
737 45
                $type,
738 45
                $childNode
739
            );
740 45
        };
741
742
        $methods = [
743 45
            'sequence' => $seqFromElement,
744 45
            'choice' => $seqFromElement,
745 45
            'all' => $seqFromElement,
746 45
            'attribute' => function (
747
                DOMElement $childNode
748
            ) use (
749 45
                $node,
750 45
                $type
751
            ) {
752 45
                $attribute = Attribute::getAttributeFromAttributeOrRef(
753 45
                    $this,
754 45
                    $childNode,
755 45
                    $type->getSchema(),
756 45
                    $node
757
                );
758 45
                $type->addAttribute($attribute);
759 45
            },
760 45
            'attributeGroup' => function (
761
                DOMElement $childNode
762
            ) use (
763 45
                $node,
764 45
                $type
765
            ) {
766 45
                AttributeGroup::findSomethingLikeThis(
767 45
                    $this,
768 45
                    $type->getSchema(),
769 45
                    $node,
770 45
                    $childNode,
771 45
                    $type
772
                );
773 45
            },
774
        ];
775
776 45
        foreach ($node->childNodes as $childNode) {
777 45
            if (isset($methods[$childNode->localName])) {
778 45
                $method = $methods[$childNode->localName];
779 45
                $method($childNode);
780
            }
781
        }
782 45
    }
783
784 45
    public function findAndSetSomeBase(
785
        Type $type,
786
        Base $setBaseOnThis,
787
        DOMElement $node
788
    ) {
789
        /**
790
        * @var Type $parent
791
        */
792 45
        $parent = $this->findSomeType($type, $node, 'base');
793 45
        $setBaseOnThis->setBase($parent);
794 45
    }
795
796 45
    private function maybeLoadExtensionFromBaseComplexType(
797
        Type $type,
798
        DOMElement $childNode
799
    ) {
800 45
        if (! ($type instanceof BaseComplexType)) {
801
            throw new RuntimeException(
802
                'Argument 1 passed to ' .
803
                __METHOD__ .
804
                ' needs to be an instance of ' .
805
                BaseComplexType::class .
806
                ' when passed onto ' .
807
                static::class .
808
                '::loadExtension(), ' .
809
                get_class($type) .
810
                ' given.'
811
            );
812
        }
813 45
        $this->loadExtension($type, $childNode);
814 45
    }
815
816 45
    private function loadRestriction(Type $type, DOMElement $node)
817
    {
818 45
        Restriction::loadRestriction($this, $type, $node);
819 45
    }
820
821
    /**
822
    * @param string $typeName
823
    *
824
    * @return mixed[]
825
    */
826 45
    private static function splitParts(DOMElement $node, $typeName)
827
    {
828 45
        $prefix = null;
829 45
        $name = $typeName;
830 45
        if (strpos($typeName, ':') !== false) {
831 45
            list ($prefix, $name) = explode(':', $typeName);
832
        }
833
834 45
        $namespace = $node->lookupNamespaceUri($prefix ?: '');
835
        return array(
836 45
            $name,
837 45
            $namespace,
838 45
            $prefix
839
        );
840
    }
841
842
    /**
843
     *
844
     * @param string $finder
845
     * @param Schema $schema
846
     * @param DOMElement $node
847
     * @param string $typeName
848
     * @throws TypeException
849
     * @return ElementItem|Group|AttributeItem|AttributeGroup|Type
850
     */
851 45
    public function findSomething($finder, Schema $schema, DOMElement $node, $typeName)
852
    {
853 45
        list ($name, $namespace) = self::splitParts($node, $typeName);
854
855 45
        $namespace = $namespace ?: $schema->getTargetNamespace();
856
857
        try {
858 45
            return $schema->$finder($name, $namespace);
859
        } catch (TypeNotFoundException $e) {
860
            throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", strtolower(substr($finder, 4)), $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e);
861
        }
862
    }
863
864
    /**
865
    * @return Closure
866
    */
867 45
    private function loadElementDef(Schema $schema, DOMElement $node)
868
    {
869 45
        return $this->loadAttributeOrElementDef($schema, $node, false);
870
    }
871
872 45
    public function fillItem(Item $element, DOMElement $node)
873
    {
874 45
        foreach ($node->childNodes as $childNode) {
875
            if (
876 45
                in_array(
877 45
                    $childNode->localName,
878
                    [
879 45
                        'complexType',
880
                        'simpleType',
881
                    ]
882
                )
883
            ) {
884 45
                Type::loadTypeWithCallback(
885 45
                    $this,
886 45
                    $element->getSchema(),
887 45
                    $childNode,
888 45
                    function (Type $type) use ($element) {
889 45
                        $element->setType($type);
890 45
                    }
891
                );
892 45
                return;
893
            }
894
        }
895
896 45
        $this->fillItemNonLocalType($element, $node);
897 45
    }
898
899 45
    private function fillItemNonLocalType(Item $element, DOMElement $node)
900
    {
901 45
        if ($node->getAttribute("type")) {
902
            /**
903
            * @var Type $type
904
            */
905 45
            $type = $this->findSomeType($element, $node, 'type');
906
        } else {
907
            /**
908
            * @var Type $type
909
            */
910 45
            $type = $this->findSomeTypeFromAttribute(
911 45
                $element,
912 45
                $node,
913 45
                ($node->lookupPrefix(self::XSD_NS) . ':anyType')
914
            );
915
        }
916
917 45
        $element->setType($type);
918 45
    }
919
920
    /**
921
    * @return Closure
922
    */
923 45
    private function loadImport(Schema $schema, DOMElement $node)
924
    {
925 45
        $base = urldecode($node->ownerDocument->documentURI);
926 45
        $file = UrlUtils::resolveRelativeUrl($base, $node->getAttribute("schemaLocation"));
927
928 45
        $namespace = $node->getAttribute("namespace");
929
930
        if (
931
            (
932 45
                isset(self::$globalSchemaInfo[$namespace]) &&
933 45
                Schema::hasLoadedFile(
934 45
                    $loadedFilesKey = self::$globalSchemaInfo[$namespace]
935
                )
936
            ) ||
937 3
            Schema::hasLoadedFile(
938 3
                $loadedFilesKey = $this->getNamespaceSpecificFileIndex(
939 3
                    $file,
940 3
                    $namespace
941
                )
942
            ) ||
943 1
            Schema::hasLoadedFile($loadedFilesKey = $file)
944
        ) {
945 45
            $schema->addSchema(Schema::getLoadedFile($loadedFilesKey));
946
947 45
            return function() {
948 45
            };
949
        }
950
951 1
        return $this->loadImportFresh($schema, $node, $file, $namespace);
952
    }
953
954
    /**
955
    * @param string $file
956
    * @param string $namespace
957
    *
958
    * @return Closure
959
    */
960 1
    private function loadImportFresh(
961
        Schema $schema,
962
        DOMElement $node,
963
        $file,
964
        $namespace
965
    ) {
966 1
        if (! $namespace) {
967 1
            $newSchema = Schema::setLoadedFile($file, $schema);
968
        } else {
969
            $newSchema = Schema::setLoadedFile($file, new Schema());
970
            $newSchema->addSchema($this->getGlobalSchema());
971
        }
972
973 1
        $xml = $this->getDOM(isset($this->knownLocationSchemas[$file]) ? $this->knownLocationSchemas[$file] : $file);
974
975 1
        $callbacks = $this->schemaNode($newSchema, $xml->documentElement, $schema);
976
977 1
        if ($namespace) {
978
            $schema->addSchema($newSchema);
979
        }
980
981
982 1
        return function () use ($callbacks) {
983 1
            foreach ($callbacks as $callback) {
984 1
                $callback();
985
            }
986 1
        };
987
    }
988
989
    /**
990
    * @var Schema|null
991
    */
992
    private $globalSchema;
993
994
    /**
995
     *
996
     * @return Schema
997
     */
998 45
    public function getGlobalSchema()
999
    {
1000 45
        if (!$this->globalSchema) {
1001 45
            $callbacks = array();
1002 45
            $globalSchemas = array();
1003 45
            foreach (self::$globalSchemaInfo as $namespace => $uri) {
1004 45
                Schema::setLoadedFile(
1005 45
                    $uri,
1006 45
                    $globalSchemas[$namespace] = $schema = new Schema()
1007
                );
1008 45
                if ($namespace === self::XSD_NS) {
1009 45
                    $this->globalSchema = $schema;
1010
                }
1011 45
                $xml = $this->getDOM($this->knownLocationSchemas[$uri]);
1012 45
                $callbacks = array_merge($callbacks, $this->schemaNode($schema, $xml->documentElement));
1013
            }
1014
1015 45
            $globalSchemas[self::XSD_NS]->addType(new SimpleType($globalSchemas[self::XSD_NS], "anySimpleType"));
1016 45
            $globalSchemas[self::XSD_NS]->addType(new SimpleType($globalSchemas[self::XSD_NS], "anyType"));
1017
1018 45
            $globalSchemas[self::XML_NS]->addSchema($globalSchemas[self::XSD_NS], self::XSD_NS);
1019 45
            $globalSchemas[self::XSD_NS]->addSchema($globalSchemas[self::XML_NS], self::XML_NS);
1020
1021 45
            foreach ($callbacks as $callback) {
1022 45
                $callback();
1023
            }
1024
        }
1025
1026
        /**
1027
        * @var Schema $out
1028
        */
1029 45
        $out = $this->globalSchema;
1030
1031 45
        return $out;
1032
    }
1033
1034
    /**
1035
     * @param DOMElement $node
1036
     * @param string  $file
1037
     *
1038
     * @return Schema
1039
     */
1040 45
    public function readNode(DOMElement $node, $file = 'schema.xsd')
1041
    {
1042 45
        $fileKey = $node->hasAttribute('targetNamespace') ? $this->getNamespaceSpecificFileIndex($file, $node->getAttribute('targetNamespace')) : $file;
1043 45
        Schema::setLoadedFile($fileKey, $rootSchema = new Schema());
1044
1045 45
        $rootSchema->addSchema($this->getGlobalSchema());
1046 45
        $callbacks = $this->schemaNode($rootSchema, $node);
1047
1048 45
        foreach ($callbacks as $callback) {
1049 39
            call_user_func($callback);
1050
        }
1051
1052 45
        return $rootSchema;
1053
    }
1054
1055
    /**
1056
     * It is possible that a single file contains multiple <xsd:schema/> nodes, for instance in a WSDL file.
1057
     *
1058
     * Each of these  <xsd:schema/> nodes typically target a specific namespace. Append the target namespace to the
1059
     * file to distinguish between multiple schemas in a single file.
1060
     *
1061
     * @param string $file
1062
     * @param string $targetNamespace
1063
     *
1064
     * @return string
1065
     */
1066 45
    private function getNamespaceSpecificFileIndex($file, $targetNamespace)
1067
    {
1068 45
        return $file . '#' . $targetNamespace;
1069
    }
1070
1071
    /**
1072
     * @param string $content
1073
     * @param string $file
1074
     *
1075
     * @return Schema
1076
     *
1077
     * @throws IOException
1078
     */
1079 44
    public function readString($content, $file = 'schema.xsd')
1080
    {
1081 44
        $xml = new DOMDocument('1.0', 'UTF-8');
1082 44
        if (!$xml->loadXML($content)) {
1083
            throw new IOException("Can't load the schema");
1084
        }
1085 44
        $xml->documentURI = $file;
1086
1087 44
        return $this->readNode($xml->documentElement, $file);
1088
    }
1089
1090
    /**
1091
     * @param string $file
1092
     *
1093
     * @return Schema
1094
     */
1095 1
    public function readFile($file)
1096
    {
1097 1
        $xml = $this->getDOM($file);
1098 1
        return $this->readNode($xml->documentElement, $file);
1099
    }
1100
1101
    /**
1102
     * @param string $file
1103
     *
1104
     * @return DOMDocument
1105
     *
1106
     * @throws IOException
1107
     */
1108 45
    private function getDOM($file)
1109
    {
1110 45
        $xml = new DOMDocument('1.0', 'UTF-8');
1111 45
        if (!$xml->load($file)) {
1112
            throw new IOException("Can't load the file $file");
1113
        }
1114 45
        return $xml;
1115
    }
1116
}
1117