Union   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 35
dl 0
loc 119
rs 10
c 0
b 0
f 0

5 Methods

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