SimpleRestriction   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 55 1
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 simple version of the xs:restriction.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
final class SimpleRestriction extends AbstractSimpleRestrictionType
23
{
24
    /** @var string */
25
    public const LOCALNAME = 'restriction';
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
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
46
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
47
        $attributes = array_merge($localAttribute, $attributeGroup);
48
49
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
50
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
51
52
        $localSimpleType = LocalSimpleType::getChildrenOfClass($xml);
53
        Assert::maxCount($localSimpleType, 1, TooManyElementsException::class);
54
55
        $minExclusive = MinExclusive::getChildrenOfClass($xml);
56
        $minInclusive = MinInclusive::getChildrenOfClass($xml);
57
        $maxExclusive = MaxExclusive::getChildrenOfClass($xml);
58
        $maxInclusive = MaxInclusive::getChildrenOfClass($xml);
59
        $totalDigits = TotalDigits::getChildrenOfClass($xml);
60
        $fractionDigits = FractionDigits::getChildrenOfClass($xml);
61
        $length = Length::getChildrenOfClass($xml);
62
        $minLength = MinLength::getChildrenOfClass($xml);
63
        $maxLength = MaxLength::getChildrenOfClass($xml);
64
        $enumeration = Enumeration::getChildrenOfClass($xml);
65
        $whiteSpace = WhiteSpace::getChildrenOfClass($xml);
66
        $pattern = Pattern::getChildrenOfClass($xml);
67
68
        $facets = array_merge(
69
            $maxExclusive,
70
            $maxInclusive,
71
            $minExclusive,
72
            $minInclusive,
73
            $totalDigits,
74
            $fractionDigits,
75
            $length,
76
            $maxLength,
77
            $minLength,
78
            $enumeration,
79
            $whiteSpace,
80
            $pattern,
81
        );
82
83
        return new static(
84
            self::getAttribute($xml, 'base', QNameValue::class),
85
            array_pop($localSimpleType),
86
            $facets,
87
            $attributes,
88
            array_pop($anyAttribute),
89
            array_pop($annotation),
90
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
91
            self::getAttributesNSFromXML($xml),
92
        );
93
    }
94
}
95