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

LocalElement   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 61
rs 10
c 1
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\xs;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\Builtin\{BooleanValue, IDValue, NCNameValue, QNameValue, StringValue};
11
use SimpleSAML\XMLSchema\Type\{BlockSetValue, DerivationSetValue, FormChoiceValue, MinOccursValue, MaxOccursValue};
12
13
use function array_merge;
14
use function array_pop;
15
16
/**
17
 * Class representing the local element-element.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class LocalElement extends AbstractLocalElement implements NestedParticleInterface
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
        $annotation = Annotation::getChildrenOfClass($xml);
42
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
43
44
        // The local type
45
        $localSimpleType = LocalSimpleType::getChildrenOfClass($xml);
46
        Assert::maxCount($localSimpleType, 1, TooManyElementsException::class);
47
48
        $localComplexType = LocalComplexType::getChildrenOfClass($xml);
49
        Assert::maxCount($localComplexType, 1, TooManyElementsException::class);
50
51
        $localType = array_merge($localSimpleType, $localComplexType);
52
        Assert::maxCount($localType, 1, TooManyElementsException::class);
53
54
        // The identity constraint
55
        $key = Key::getChildrenOfClass($xml);
56
        Assert::maxCount($key, 1, TooManyElementsException::class);
57
58
        $keyref = Keyref::getChildrenOfClass($xml);
59
        Assert::maxCount($keyref, 1, TooManyElementsException::class);
60
61
        $unique = Unique::getChildrenOfClass($xml);
62
        Assert::maxCount($unique, 1, TooManyElementsException::class);
63
64
        $identityConstraint = array_merge($key, $keyref, $unique);
65
66
        return new static(
67
            self::getOptionalAttribute($xml, 'name', NCNameValue::class, null),
68
            self::getOptionalAttribute($xml, 'ref', QNameValue::class, null),
69
            array_pop($localType),
70
            $identityConstraint,
71
            self::getOptionalAttribute($xml, 'type', QNameValue::class, null),
72
            self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null),
73
            self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null),
74
            self::getOptionalAttribute($xml, 'default', StringValue::class, null),
75
            self::getOptionalAttribute($xml, 'fixed', StringValue::class, null),
76
            self::getOptionalAttribute($xml, 'nillable', BooleanValue::class, null),
77
            self::getOptionalAttribute($xml, 'block', BlockSetValue::class, null),
78
            self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null),
79
            array_pop($annotation),
80
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
81
            self::getAttributesNSFromXML($xml),
82
        );
83
    }
84
}
85