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

XsList::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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