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

Union::getSimpleType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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