NameIDPolicy::toXML()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
11
use SimpleSAML\SAML2\Type\SAMLStringValue;
12
use SimpleSAML\XML\ArrayizableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
16
use SimpleSAML\XMLSchema\Type\BooleanValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\BooleanValue 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...
17
18
use function array_change_key_case;
19
use function array_filter;
20
use function array_key_exists;
21
use function array_keys;
22
use function var_export;
23
24
/**
25
 * Class for handling SAML2 NameIDPolicy.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
final class NameIDPolicy extends AbstractSamlpElement implements
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\AbstractSamlpElement 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...
30
    ArrayizableElementInterface,
31
    SchemaValidatableElementInterface
32
{
33
    use SchemaValidatableElementTrait;
34
35
36
    /**
37
     * Initialize a NameIDPolicy.
38
     *
39
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
40
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
41
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $AllowCreate
42
     */
43
    public function __construct(
44
        protected ?SAMLAnyURIValue $Format = null,
45
        protected ?SAMLStringValue $SPNameQualifier = null,
46
        protected ?BooleanValue $AllowCreate = null,
47
    ) {
48
    }
49
50
51
    /**
52
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
53
     */
54
    public function getFormat(): ?SAMLAnyURIValue
55
    {
56
        return $this->Format;
57
    }
58
59
60
    /**
61
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
62
     */
63
    public function getSPNameQualifier(): ?SAMLStringValue
64
    {
65
        return $this->SPNameQualifier;
66
    }
67
68
69
    /**
70
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
71
     */
72
    public function getAllowCreate(): ?BooleanValue
73
    {
74
        return $this->AllowCreate;
75
    }
76
77
78
    /**
79
     * Test if an object, at the state it's in, would produce an empty XML-element
80
     */
81
    public function isEmptyElement(): bool
82
    {
83
        return empty($this->getFormat())
84
            && empty($this->getSPNameQualifier())
85
            && empty($this->getAllowCreate());
86
    }
87
88
89
    /**
90
     * Convert XML into a NameIDPolicy
91
     *
92
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
93
     *   if the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): static
96
    {
97
        Assert::same($xml->localName, 'NameIDPolicy', InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, NameIDPolicy::NS, InvalidDOMElementException::class);
99
100
        $Format = self::getOptionalAttribute($xml, 'Format', SAMLAnyURIValue::class, null);
101
        $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', SAMLStringValue::class, null);
102
        $AllowCreate = self::getOptionalAttribute($xml, 'AllowCreate', BooleanValue::class, null);
103
104
        return new static(
105
            $Format,
106
            $SPNameQualifier,
107
            $AllowCreate,
108
        );
109
    }
110
111
112
    /**
113
     * Convert this NameIDPolicy to XML.
114
     */
115
    public function toXML(?DOMElement $parent = null): DOMElement
116
    {
117
        $e = $this->instantiateParentElement($parent);
118
119
        if ($this->getFormat() !== null) {
120
            $e->setAttribute('Format', $this->getFormat()->getValue());
121
        }
122
123
        if ($this->getSPNameQualifier() !== null) {
124
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()->getValue());
125
        }
126
127
        if ($this->getAllowCreate() !== null) {
128
            $e->setAttribute('AllowCreate', var_export($this->getAllowCreate()->toBoolean(), true));
129
        }
130
131
        return $e;
132
    }
133
134
135
    /**
136
     * Create a class from an array
137
     *
138
     * @param array{
139
     *   'Format'?: string,
140
     *   'SPNameQualifier'?: string,
141
     *   'AllowCreate'?: string,
142
     * } $data
143
     */
144
    public static function fromArray(array $data): static
145
    {
146
        $data = self::processArrayContents($data);
147
148
        return new static(
149
            $data['Format'] !== null ? SAMLAnyURIValue::fromString($data['Format']) : null,
150
            $data['SPNameQualifier'] !== null ? SAMLStringValue::fromString($data['SPNameQualifier']) : null,
151
            $data['AllowCreate'] !== null ? BooleanValue::fromBoolean($data['AllowCreate']) : null,
152
        );
153
    }
154
155
156
    /**
157
     * Validates an array representation of this object and returns the same array with
158
     * rationalized keys (casing) and parsed sub-elements.
159
     *
160
     * @param array{
161
     *   'Format'?: string,
162
     *   'SPNameQualifier'?: string,
163
     *   'AllowCreate'?: string,
164
     * } $data
165
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
166
     *   'Format'?: string,
167
     *   'SPNameQualifier'?: string,
168
     *   'AllowCreate'?: string,
169
     * }
170
     */
171
    private static function processArrayContents(array $data): array
172
    {
173
        $data = array_change_key_case($data, CASE_LOWER);
174
175
        // Make sure the array keys are known for this kind of object
176
        Assert::allOneOf(
177
            array_keys($data),
178
            [
179
                'format',
180
                'spnamequalifier',
181
                'allowcreate',
182
            ],
183
            ArrayValidationException::class,
184
        );
185
186
        Assert::string($data['format'], ArrayValidationException::class);
187
188
        $retval = ['Format' => $data['format']];
189
190
        if (array_key_exists('spnamequalifier', $data)) {
191
            Assert::string($data['spnamequalifier'], ArrayValidationException::class);
192
            $retval['SPNameQualifier'] = $data['spnamequalifier'];
193
        }
194
195
        if (array_key_exists('allowcreate', $data)) {
196
            Assert::boolean($data['allowcreate'], ArrayValidationException::class);
197
            $retval['AllowCreate'] = $data['allowcreate'];
198
        }
199
200
        return $retval;
201
    }
202
203
204
    /**
205
     * Create an array from this class
206
     *
207
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
208
     *   'Format'?: string,
209
     *   'SPNameQualifier'?: string,
210
     *   'AllowCreate'?: string,
211
     * }
212
     */
213
    public function toArray(): array
214
    {
215
        $data = [
216
            'Format' => $this->getFormat()?->getValue(),
217
            'SPNameQualifier' => $this->getSPNameQualifier()?->getValue(),
218
            'AllowCreate' => $this->getAllowCreate()?->toBoolean(),
219
        ];
220
221
        return array_filter($data);
222
    }
223
}
224