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

TopLevelElement   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

1 Method

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