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

NarrowMaxMinElement   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 46 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, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\{BooleanValue, IDValue, NCNameValue, QNameValue, StringValue};
11
use SimpleSAML\XMLSchema\Type\Schema\{
12
    BlockSetValue,
13
    DerivationSetValue,
14
    FormChoiceValue,
15
    MaxOccursValue,
16
    MinOccursValue,
17
};
18
use SimpleSAML\XMLSchema\XML\Interface\NestedParticleInterface;
19
20
use function array_merge;
21
use function array_pop;
22
23
/**
24
 * Class representing the local narrowMaxMin-element.
25
 *
26
 * @package simplesamlphp/xml-common
27
 */
28
final class NarrowMaxMinElement extends AbstractNarrowMaxMin implements NestedParticleInterface
29
{
30
    /** @var string */
31
    public const LOCALNAME = 'element';
32
33
34
    /**
35
     * Create an instance of this object from its XML representation.
36
     *
37
     * @param \DOMElement $xml
38
     * @return static
39
     *
40
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
41
     *   if the qualified name of the supplied element is wrong
42
     */
43
    public static function fromXML(DOMElement $xml): static
44
    {
45
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
46
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
47
48
        $annotation = Annotation::getChildrenOfClass($xml);
49
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
50
51
        // The local type
52
        $localSimpleType = LocalSimpleType::getChildrenOfClass($xml);
53
        Assert::maxCount($localSimpleType, 1, TooManyElementsException::class);
54
55
        $localComplexType = LocalComplexType::getChildrenOfClass($xml);
56
        Assert::maxCount($localComplexType, 1, TooManyElementsException::class);
57
58
        $localType = array_merge($localSimpleType, $localComplexType);
59
        Assert::maxCount($localType, 1, TooManyElementsException::class);
60
61
        // The identity constraint
62
        $key = Key::getChildrenOfClass($xml);
63
        Assert::maxCount($key, 1, TooManyElementsException::class);
64
65
        $keyref = Keyref::getChildrenOfClass($xml);
66
        Assert::maxCount($keyref, 1, TooManyElementsException::class);
67
68
        $unique = Unique::getChildrenOfClass($xml);
69
        Assert::maxCount($unique, 1, TooManyElementsException::class);
70
71
        $identityConstraint = array_merge($key, $keyref, $unique);
72
73
        return new static(
74
            self::getOptionalAttribute($xml, 'name', NCNameValue::class, null),
75
            self::getOptionalAttribute($xml, 'ref', QNameValue::class, null),
76
            array_pop($localType),
77
            $identityConstraint,
78
            self::getOptionalAttribute($xml, 'type', QNameValue::class, null),
79
            self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null),
80
            self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null),
81
            self::getOptionalAttribute($xml, 'default', StringValue::class, null),
82
            self::getOptionalAttribute($xml, 'fixed', StringValue::class, null),
83
            self::getOptionalAttribute($xml, 'nillable', BooleanValue::class, null),
84
            self::getOptionalAttribute($xml, 'block', BlockSetValue::class, null),
85
            self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null),
86
            array_pop($annotation),
87
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
88
            self::getAttributesNSFromXML($xml),
89
        );
90
    }
91
}
92