Passed
Push — master ( d2025d...e9a2f4 )
by Tim
12:16
created

NameIDPolicy::processArrayContents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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