Passed
Push — master ( faa376...c7ca4c )
by Tim
17:29 queued 14:52
created

XsList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
dl 0
loc 99
rs 10
c 0
b 0
f 0
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;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
use SimpleSAML\XMLSchema\Type\QNameValue;
15
use SimpleSAML\XMLSchema\XML\Interface\SimpleDerivationInterface;
16
17
use function strval;
18
19
/**
20
 * Class representing the list-element.
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
final class XsList extends AbstractAnnotated implements
25
    SchemaValidatableElementInterface,
26
    SimpleDerivationInterface
27
{
28
    use SchemaValidatableElementTrait;
29
30
31
    public const string LOCALNAME = 'list';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 31 at column 24
Loading history...
32
33
34
    /**
35
     * Notation constructor
36
     *
37
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $simpleType
38
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $itemType
39
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
40
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
41
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
42
     */
43
    public function __construct(
44
        protected ?LocalSimpleType $simpleType = null,
45
        protected ?QNameValue $itemType = null,
46
        ?Annotation $annotation = null,
47
        ?IDValue $id = null,
48
        array $namespacedAttributes = [],
49
    ) {
50
        parent::__construct($annotation, $id, $namespacedAttributes);
51
    }
52
53
54
    /**
55
     * Collect the value of the simpleType-property
56
     *
57
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|null
58
     */
59
    public function getSimpleType(): ?LocalSimpleType
60
    {
61
        return $this->simpleType;
62
    }
63
64
65
    /**
66
     * Collect the value of the itemType-property
67
     *
68
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
69
     */
70
    public function getItemType(): ?QNameValue
71
    {
72
        return $this->itemType;
73
    }
74
75
76
    /**
77
     * Add this XsList to an XML element.
78
     *
79
     * @param \DOMElement|null $parent The element we should append this list to.
80
     * @return \DOMElement
81
     */
82
    public function toXML(?DOMElement $parent = null): DOMElement
83
    {
84
        $e = parent::toXML($parent);
85
86
        if ($this->getItemType() !== null) {
87
            $e->setAttribute('itemType', strval($this->getItemType()));
88
        }
89
90
        $this->getSimpleType()?->toXML($e);
91
92
        return $e;
93
    }
94
95
96
    /**
97
     * Create an instance of this object from its XML representation.
98
     *
99
     * @param \DOMElement $xml
100
     * @return static
101
     *
102
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
103
     *   if the qualified name of the supplied element is wrong
104
     */
105
    public static function fromXML(DOMElement $xml): static
106
    {
107
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
108
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
109
110
        $annotation = Annotation::getChildrenOfClass($xml);
111
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
112
113
        $simpleType = LocalSimpleType::getChildrenOfClass($xml);
114
        Assert::maxCount($simpleType, 1, TooManyElementsException::class);
115
116
        return new static(
117
            array_pop($simpleType),
118
            self::getOptionalAttribute($xml, 'itemType', QNameValue::class),
119
            array_pop($annotation),
120
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
121
            self::getAttributesNSFromXML($xml),
122
        );
123
    }
124
}
125