AbstractClaimType::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 23
rs 8.8333
c 1
b 0
f 0
cc 7
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\ExtendableAttributesTrait;
18
use SimpleSAML\XML\ExtendableElementTrait;
19
use SimpleSAML\XML\SerializableElementInterface;
20
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
21
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
22
use SimpleSAML\XMLSchema\Type\AnyURIValue;
23
use SimpleSAML\XMLSchema\Type\BooleanValue;
24
use SimpleSAML\XMLSchema\XML\Constants\NS;
25
26
use function array_merge;
27
use function array_pop;
28
use function var_export;
29
30
/**
31
 * Class defining the ClaimType element
32
 *
33
 * @package simplesamlphp/ws-security
34
 */
35
abstract class AbstractClaimType extends AbstractAuthElement
36
{
37
    use ExtendableAttributesTrait;
38
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\auth\AbstractClaimType: $namespaceURI, $localName, $childNodes
Loading history...
39
40
41
    /** The namespace-attribute for the xs:anyAttribute */
42
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
43
44
    /** The namespace-attribute for the xs:any element */
45
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
46
47
48
    /**
49
     * AbstractClaimType constructor
50
     *
51
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $uri
52
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $optional
53
     * @param \SimpleSAML\WSSecurity\XML\auth\DisplayName|null $displayName
54
     * @param \SimpleSAML\WSSecurity\XML\auth\Description|null $description
55
     * @param \SimpleSAML\WSSecurity\XML\auth\DisplayValue|null $displayValue
56
     * @param (
57
     *   \SimpleSAML\WSSecurity\XML\auth\Value|
58
     *   \SimpleSAML\WSSecurity\XML\auth\EncryptedValue|
59
     *   \SimpleSAML\WSSecurity\XML\auth\StructuredValue|
60
     *   \SimpleSAML\WSSecurity\XML\auth\ConstrainedValue|
61
     *   \SimpleSAML\XML\SerializableElementInterface|
62
     *   null
63
     * ) $value
64
     * @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...
65
     */
66
    final public function __construct(
67
        protected AnyURIValue $uri,
68
        protected ?BooleanValue $optional = null,
69
        protected ?DisplayName $displayName = null,
70
        protected ?Description $description = null,
71
        protected ?DisplayValue $displayValue = null,
72
        protected null|Value|EncryptedValue|StructuredValue|ConstrainedValue|SerializableElementInterface $value = null,
73
        array $namespacedAttributes = [],
74
    ) {
75
        if (
76
            !($value === null ||
77
                $value instanceof Value ||
78
                $value instanceof ConstrainedValue ||
79
                $value instanceof StructuredValue ||
80
                $value instanceof EncryptedValue)
81
        ) {
82
            /** @var \SimpleSAML\XML\Chunk|\SimpleSAML\XML\AbstractElement $value */
83
            Assert::notSame(
84
                $value instanceof Chunk ? $value->getNamespaceURI() : $value::getNamespaceURI(),
85
                static::NS,
86
            );
87
        }
88
        $this->setAttributesNS($namespacedAttributes);
89
    }
90
91
92
    /**
93
     * Get the value of the $value property.
94
     *
95
     * @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...
96
     *   \SimpleSAML\WSSecurity\XML\auth\Value|
97
     *   \SimpleSAML\WSSecurity\XML\auth\EncryptedValue|
98
     *   \SimpleSAML\WSSecurity\XML\auth\StructuredValue|
99
     *   \SimpleSAML\WSSecurity\XML\auth\ConstrainedValue|
100
     *   \SimpleSAML\XML\SerializableElementInterface|
101
     *   null
102
     * ) $value
103
     */
104
    public function getValue(): null|Value|EncryptedValue|StructuredValue|ConstrainedValue|SerializableElementInterface
105
    {
106
        return $this->value;
107
    }
108
109
110
    /**
111
     * Get the value of the displayName property.
112
     *
113
     * @return \SimpleSAML\WSSecurity\XML\auth\DisplayName|null
114
     */
115
    public function getDisplayName(): ?DisplayName
116
    {
117
        return $this->displayName;
118
    }
119
120
121
    /**
122
     * Get the value of the displayValue property.
123
     *
124
     * @return \SimpleSAML\WSSecurity\XML\auth\DisplayValue|null
125
     */
126
    public function getDisplayValue(): ?DisplayValue
127
    {
128
        return $this->displayValue;
129
    }
130
131
132
    /**
133
     * Get the value of the description property.
134
     *
135
     * @return \SimpleSAML\WSSecurity\XML\auth\Description|null
136
     */
137
    public function getDescription(): ?Description
138
    {
139
        return $this->description;
140
    }
141
142
143
    /**
144
     * Get the value of the uri property.
145
     *
146
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
147
     */
148
    public function getURI(): AnyURIValue
149
    {
150
        return $this->uri;
151
    }
152
153
154
    /**
155
     * Get the value of the optional property.
156
     *
157
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
158
     */
159
    public function getOptional(): ?BooleanValue
160
    {
161
        return $this->optional;
162
    }
163
164
165
    /**
166
     * Create an instance of this object from its XML representation.
167
     *
168
     * @param \DOMElement $xml
169
     * @return static
170
     *
171
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
172
     *   if the qualified name of the supplied element is wrong
173
     */
174
    public static function fromXML(DOMElement $xml): static
175
    {
176
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
177
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
178
179
        $displayName = DisplayName::getChildrenOfClass($xml);
180
        Assert::maxCount($displayName, 1, TooManyElementsException::class);
181
182
        $displayValue = DisplayValue::getChildrenOfClass($xml);
183
        Assert::maxCount($displayValue, 1, TooManyElementsException::class);
184
185
        $description = Description::getChildrenOfClass($xml);
186
        Assert::maxCount($description, 1, TooManyElementsException::class);
187
188
        $simpleValue = Value::getChildrenOfClass($xml);
189
        $structuredValue = StructuredValue::getChildrenOfClass($xml);
190
        $encryptedValue = EncryptedValue::getChildrenOfClass($xml);
191
        $constrainedValue = ConstrainedValue::getChildrenOfClass($xml);
192
        $otherValue = self::getChildElementsFromXML($xml);
193
194
        $value = array_merge(
195
            $simpleValue,
196
            $structuredValue,
197
            $encryptedValue,
198
            $constrainedValue,
199
            $otherValue,
200
        );
201
        Assert::maxCount($value, 1, TooManyElementsException::class);
202
203
        return new static(
204
            self::getAttribute($xml, 'Uri', AnyURIValue::class),
205
            self::getOptionalAttribute($xml, 'Optional', BooleanValue::class, null),
206
            array_pop($displayName),
207
            array_pop($description),
208
            array_pop($displayValue),
209
            array_pop($value),
210
            self::getAttributesNSFromXML($xml),
211
        );
212
    }
213
214
215
    /**
216
     * Add this ClaimType to an XML element.
217
     *
218
     * @param \DOMElement $parent The element we should append this username token to.
219
     * @return \DOMElement
220
     */
221
    public function toXML(?DOMElement $parent = null): DOMElement
222
    {
223
        $e = $this->instantiateParentElement($parent);
224
225
        $e->setAttribute('Uri', $this->getURI()->getValue());
226
        if ($this->getOptional() !== null) {
227
            $e->setAttribute('Optional', var_export($this->getOptional()->toBoolean(), true));
228
        }
229
230
        $this->getDisplayName()?->toXML($e);
231
        $this->getDescription()?->toXML($e);
232
        $this->getDisplayValue()?->toXML($e);
233
        $this->getValue()?->toXML($e);
234
235
        foreach ($this->getAttributesNS() as $attr) {
236
            $attr->toXML($e);
237
        }
238
239
        return $e;
240
    }
241
}
242