Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

TopLevelElement   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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