NameIDPolicy::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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