Extension::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 1
dl 0
loc 38
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
11
use SimpleSAML\XMLSchema\Type\IDValue;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
14
use function array_merge;
15
use function array_pop;
16
17
/**
18
 * Class representing the local version of xs:extension.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
final class Extension extends AbstractExtensionType
23
{
24
    /** @var string */
25
    public const LOCALNAME = 'extension';
26
27
28
    /**
29
     * Create an instance of this object from its XML representation.
30
     *
31
     * @param \DOMElement $xml
32
     * @return static
33
     *
34
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
35
     *   if the qualified name of the supplied element is wrong
36
     */
37
    public static function fromXML(DOMElement $xml): static
38
    {
39
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
40
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
41
42
        $annotation = Annotation::getChildrenOfClass($xml);
43
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
44
45
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
46
        Assert::maxCount($referencedGroup, 1, TooManyElementsException::class);
47
48
        $all = All::getChildrenOfClass($xml);
49
        Assert::maxCount($all, 1, TooManyElementsException::class);
50
51
        $choice = Choice::getChildrenOfClass($xml);
52
        Assert::maxCount($choice, 1, TooManyElementsException::class);
53
54
        $sequence = Sequence::getChildrenOfClass($xml);
55
        Assert::maxCount($sequence, 1, TooManyElementsException::class);
56
57
        $particles = array_merge($referencedGroup, $all, $choice, $sequence);
58
        Assert::maxCount($particles, 1, TooManyElementsException::class);
59
60
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
61
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
62
        $attributes = array_merge($localAttribute, $attributeGroup);
63
64
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
65
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
66
67
        return new static(
68
            self::getAttribute($xml, 'base', QNameValue::class),
69
            array_pop($particles),
70
            $attributes,
71
            array_pop($anyAttribute),
72
            array_pop($annotation),
73
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
74
            self::getAttributesNSFromXML($xml),
75
        );
76
    }
77
}
78