Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

ComplexRestriction::fromXML()   A

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, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\{IDValue, QNameValue};
11
12
use function array_merge;
13
use function array_pop;
14
15
/**
16
 * Class representing the complexRestrictionType restriction-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class ComplexRestriction extends AbstractComplexRestrictionType
21
{
22
    /** @var string */
23
    public const LOCALNAME = 'restriction';
24
25
26
    /**
27
     * Create an instance of this object from its XML representation.
28
     *
29
     * @param \DOMElement $xml
30
     * @return static
31
     *
32
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
33
     *   if the qualified name of the supplied element is wrong
34
     */
35
    public static function fromXML(DOMElement $xml): static
36
    {
37
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
38
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
39
40
        $annotation = Annotation::getChildrenOfClass($xml);
41
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
42
43
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
44
        Assert::maxCount($referencedGroup, 1, TooManyElementsException::class);
45
46
        $all = All::getChildrenOfClass($xml);
47
        Assert::maxCount($all, 1, TooManyElementsException::class);
48
49
        $choice = Choice::getChildrenOfClass($xml);
50
        Assert::maxCount($choice, 1, TooManyElementsException::class);
51
52
        $sequence = Sequence::getChildrenOfClass($xml);
53
        Assert::maxCount($sequence, 1, TooManyElementsException::class);
54
55
        $particles = array_merge($referencedGroup, $all, $choice, $sequence);
56
        Assert::maxCount($particles, 1, TooManyElementsException::class);
57
58
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
59
        $referencedAttributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
60
        $attributes = array_merge($localAttribute, $referencedAttributeGroup);
61
62
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
63
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
64
65
        return new static(
66
            self::getAttribute($xml, 'base', QNameValue::class),
67
            array_pop($particles),
68
            $attributes,
69
            array_pop($anyAttribute),
70
            array_pop($annotation),
71
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
72
            self::getAttributesNSFromXML($xml),
73
        );
74
    }
75
}
76