AbstractClaimType::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
rs 9.2222
cc 6
nc 2
nop 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\auth;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\auth\ConstrainedValue;
10
use SimpleSAML\WSSecurity\XML\auth\Description;
11
use SimpleSAML\WSSecurity\XML\auth\DisplayName;
12
use SimpleSAML\WSSecurity\XML\auth\DisplayValue;
13
use SimpleSAML\WSSecurity\XML\auth\EncryptedValue;
14
use SimpleSAML\WSSecurity\XML\auth\StructuredValue;
15
use SimpleSAML\WSSecurity\XML\auth\Value;
16
use SimpleSAML\XML\Chunk;
17
use SimpleSAML\XML\Exception\InvalidDOMElementException;
18
use SimpleSAML\XML\Exception\TooManyElementsException;
19
use SimpleSAML\XML\ExtendableAttributesTrait;
20
use SimpleSAML\XML\SerializableElementInterface;
21
use SimpleSAML\XML\XsNamespace as NS;
22
23
use function array_filter;
24
use function array_merge;
25
use function array_pop;
26
27
/**
28
 * Class defining the ClaimType element
29
 *
30
 * @package simplesamlphp/ws-security
31
 */
32
abstract class AbstractClaimType extends AbstractAuthElement
33
{
34
    use ExtendableAttributesTrait;
35
36
    /** The namespace-attribute for the xs:anyAttribute */
37
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
38
39
40
    /**
41
     * AbstractClaimType constructor
42
     *
43
     * @param string $uri
44
     * @param bool|null $optional
45
     * @param \SimpleSAML\WSSecurity\XML\auth\DisplayName|null $displayName
46
     * @param \SimpleSAML\WSSecurity\XML\auth\Description|null $description
47
     * @param \SimpleSAML\WSSecurity\XML\auth\DisplayValue|null $displayValue
48
     * @param (
49
     *   \SimpleSAML\WSSecurity\XML\auth\Value|
50
     *   \SimpleSAML\WSSecurity\XML\auth\EncryptedValue|
51
     *   \SimpleSAML\WSSecurity\XML\auth\StructuredValue|
52
     *   \SimpleSAML\WSSecurity\XML\auth\ConstrainedValue|
53
     *   \SimpleSAML\XML\SerializableElementInterface|
54
     *   null
55
     * ) $value
56
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\auth\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     */
58
    final public function __construct(
59
        protected string $uri,
60
        protected ?bool $optional = null,
61
        protected ?DisplayName $displayName = null,
62
        protected ?Description $description = null,
63
        protected ?DisplayValue $displayValue = null,
64
        protected null|Value|EncryptedValue|StructuredValue|ConstrainedValue|SerializableElementInterface $value = null,
65
        array $namespacedAttributes = [],
66
    ) {
67
        Assert::validURI($uri);
68
        if (
69
            !($value === null ||
70
                $value instanceof Value ||
71
                $value instanceof ConstrainedValue ||
72
                $value instanceof StructuredValue ||
73
                $value instanceof EncryptedValue)
74
        ) {
75
            /** @psalm-var \SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk $value */
76
            Assert::notSame($value->getNamespaceURI(), static::NS);
77
        }
78
        $this->setAttributesNS($namespacedAttributes);
79
    }
80
81
82
    /**
83
     * Get the value of the $value property.
84
     *
85
     * @return (
0 ignored issues
show
Documentation Bug introduced by
The doc comment ( at position 1 could not be parsed: the token is null at position 1.
Loading history...
86
     *   \SimpleSAML\WSSecurity\XML\auth\Value|
87
     *   \SimpleSAML\WSSecurity\XML\auth\EncryptedValue|
88
     *   \SimpleSAML\WSSecurity\XML\auth\StructuredValue|
89
     *   \SimpleSAML\WSSecurity\XML\auth\ConstrainedValue|
90
     *   \SimpleSAML\XML\SerializableElementInterface|
91
     *   null
92
     * ) $value
93
     */
94
    public function getValue(): null|Value|EncryptedValue|StructuredValue|ConstrainedValue|SerializableElementInterface
95
    {
96
        return $this->value;
97
    }
98
99
100
    /**
101
     * Get the value of the displayName property.
102
     *
103
     * @return \SimpleSAML\WSSecurity\XML\auth\DisplayName|null
104
     */
105
    public function getDisplayName(): ?DisplayName
106
    {
107
        return $this->displayName;
108
    }
109
110
111
    /**
112
     * Get the value of the displayValue property.
113
     *
114
     * @return \SimpleSAML\WSSecurity\XML\auth\DisplayValue|null
115
     */
116
    public function getDisplayValue(): ?DisplayValue
117
    {
118
        return $this->displayValue;
119
    }
120
121
122
    /**
123
     * Get the value of the description property.
124
     *
125
     * @return \SimpleSAML\WSSecurity\XML\auth\Description|null
126
     */
127
    public function getDescription(): ?Description
128
    {
129
        return $this->description;
130
    }
131
132
133
    /**
134
     * Get the value of the uri property.
135
     *
136
     * @return string
137
     */
138
    public function getURI(): string
139
    {
140
        return $this->uri;
141
    }
142
143
144
    /**
145
     * Get the value of the optional property.
146
     *
147
     * @return bool|null
148
     */
149
    public function getOptional(): ?bool
150
    {
151
        return $this->optional;
152
    }
153
154
155
    /**
156
     * Create an instance of this object from its XML representation.
157
     *
158
     * @param \DOMElement $xml
159
     * @return static
160
     *
161
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
162
     *   if the qualified name of the supplied element is wrong
163
     */
164
    public static function fromXML(DOMElement $xml): static
165
    {
166
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
167
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
168
169
        $displayName = DisplayName::getChildrenOfClass($xml);
170
        Assert::maxCount($displayName, 1, TooManyElementsException::class);
171
172
        $displayValue = DisplayValue::getChildrenOfClass($xml);
173
        Assert::maxCount($displayValue, 1, TooManyElementsException::class);
174
175
        $description = Description::getChildrenOfClass($xml);
176
        Assert::maxCount($description, 1, TooManyElementsException::class);
177
178
        $simpleValue = Value::getChildrenOfClass($xml);
179
        $structuredValue = StructuredValue::getChildrenOfClass($xml);
180
        $encryptedValue = EncryptedValue::getChildrenOfClass($xml);
181
        $constrainedValue = ConstrainedValue::getChildrenOfClass($xml);
182
183
        $otherValue = [];
184
        foreach ($xml->childNodes as $child) {
185
            if (!($child instanceof DOMElement)) {
186
                continue;
187
            } elseif ($child->namespaceURI !== static::NS) {
188
                $otherValue[] = new Chunk($child);
189
            }
190
        }
191
192
        $value = array_filter(array_merge(
193
            $simpleValue,
194
            $structuredValue,
195
            $encryptedValue,
196
            $constrainedValue,
197
            $otherValue,
198
        ));
199
        Assert::maxCount($value, 1, TooManyElementsException::class);
200
201
        return new static(
202
            self::getAttribute($xml, 'Uri'),
203
            self::getOptionalBooleanAttribute($xml, 'Optional', null),
204
            array_pop($displayName),
205
            array_pop($description),
206
            array_pop($displayValue),
207
            array_pop($value),
208
            self::getAttributesNSFromXML($xml),
209
        );
210
    }
211
212
213
    /**
214
     * Add this ClaimType to an XML element.
215
     *
216
     * @param \DOMElement $parent The element we should append this username token to.
217
     * @return \DOMElement
218
     */
219
    public function toXML(?DOMElement $parent = null): DOMElement
220
    {
221
        $e = $this->instantiateParentElement($parent);
222
223
        $e->setAttribute('Uri', $this->getURI());
224
        if ($this->getOptional() !== null) {
225
            $e->setAttribute('Optional', $this->getOptional() ? 'true' : 'false');
226
        }
227
228
        $this->getDisplayName()?->toXML($e);
229
        $this->getDescription()?->toXML($e);
230
        $this->getDisplayValue()?->toXML($e);
231
        $this->getValue()?->toXML($e);
232
233
        foreach ($this->getAttributesNS() as $attr) {
234
            $attr->toXML($e);
235
        }
236
237
        return $e;
238
    }
239
}
240