NameID   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 11 1
A fromArray() 0 10 1
A getEncryptionBackend() 0 5 1
A processArrayContents() 0 42 5
A __construct() 0 39 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\SAML2\Constants as C;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
14
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
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
21
/**
22
 * Class representing the saml:NameID element.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
final class NameID extends NameIDType implements
27
    EncryptableElementInterface,
28
    SchemaValidatableElementInterface
29
{
30
    use EncryptableElementTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\SAML2\XML\EncryptableElementTrait requires the property $ownerDocument which is not provided by SimpleSAML\SAML2\XML\saml\NameID.
Loading history...
31
    use SchemaValidatableElementTrait;
32
33
    /**
34
     * Initialize a saml:NameID
35
     *
36
     * @param string $value
37
     * @param string|null $NameQualifier
38
     * @param string|null $SPNameQualifier
39
     * @param string|null $Format
40
     * @param string|null $SPProvidedID
41
     */
42
    public function __construct(
43
        string $value,
44
        ?string $NameQualifier = null,
45
        ?string $SPNameQualifier = null,
46
        ?string $Format = null,
47
        ?string $SPProvidedID = null,
48
    ) {
49
        switch ($Format) {
50
            case C::NAMEID_EMAIL_ADDRESS:
51
                Assert::email(
52
                    $value,
53
                    "The content %s of the NameID was not in the format specified by the Format attribute",
54
                );
55
                break;
56
            case C::NAMEID_ENTITY:
57
                /* 8.3.6: he NameQualifier, SPNameQualifier, and SPProvidedID attributes MUST be omitted. */
58
                Assert::null($NameQualifier, "Entity Identifier included a disallowed NameQualifier attribute.");
59
                Assert::null($SPNameQualifier, "Entity Identifier included a disallowed SPNameQualifier attribute.");
60
                Assert::null($SPProvidedID, "Entity Identifier included a disallowed SPProvidedID attribute.");
61
                break;
62
            case C::NAMEID_PERSISTENT:
63
                /* 8.3.7: Persistent name identifier values MUST NOT exceed a length of 256 characters. */
64
                Assert::maxLength(
65
                    $value,
66
                    256,
67
                    "Persistent name identifier values MUST NOT exceed a length of 256 characters.",
68
                );
69
                break;
70
            case C::NAMEID_TRANSIENT:
71
                /* 8.3.8: Transient name identifier values MUST NOT exceed a length of 256 characters. */
72
                Assert::maxLength(
73
                    $value,
74
                    256,
75
                    "Transient name identifier values MUST NOT exceed a length of 256 characters.",
76
                );
77
                break;
78
        }
79
80
        parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
81
    }
82
83
84
    public function getEncryptionBackend(): ?EncryptionBackend
85
    {
86
        // return the encryption backend you want to use,
87
        // or null if you are fine with the default
88
        return null;
89
    }
90
91
92
    /**
93
     * Create a class from an array
94
     *
95
     * @param array $data
96
     * @return static
97
     */
98
    public static function fromArray(array $data): static
99
    {
100
        $data = self::processArrayContents($data);
101
102
        return new static(
103
            $data['value'],
104
            $data['NameQualifier'] ?? null,
105
            $data['SPNameQualifier'] ?? null,
106
            $data['Format'] ?? null,
107
            $data['SPProvidedID'] ?? null,
108
        );
109
    }
110
111
112
    /**
113
     * Validates an array representation of this object and returns the same array with
114
     * rationalized keys (casing) and parsed sub-elements.
115
     *
116
     * @param array $data
117
     * @return array $data
118
     */
119
    private static function processArrayContents(array $data): array
120
    {
121
        $data = array_change_key_case($data, CASE_LOWER);
122
123
        // Make sure the array keys are known for this kind of object
124
        Assert::allOneOf(
125
            array_keys($data),
126
            [
127
                'value',
128
                'format',
129
                'namequalifier',
130
                'spnamequalifier',
131
                'spprovidedid',
132
            ],
133
            ArrayValidationException::class,
134
        );
135
136
        Assert::keyExists($data, 'value', ArrayValidationException::class);
137
        Assert::string($data['value'], ArrayValidationException::class);
138
        $retval = ['value' => $data['value']];
139
140
        if (array_key_exists('format', $data)) {
141
            Assert::string($data['format'], ArrayValidationException::class);
142
            $retval['Format'] = $data['format'];
143
        }
144
145
        if (array_key_exists('namequalifier', $data)) {
146
            Assert::string($data['namequalifier'], ArrayValidationException::class);
147
            $retval['NameQualifier'] = $data['namequalifier'];
148
        }
149
150
        if (array_key_exists('spnamequalifier', $data)) {
151
            Assert::string($data['spnamequalifier'], ArrayValidationException::class);
152
            $retval['SPNameQualifier'] = $data['spnamequalifier'];
153
        }
154
155
        if (array_key_exists('spprovidedid', $data)) {
156
            Assert::string($data['spprovidedid'], ArrayValidationException::class);
157
            $retval['SPProvidedID'] = $data['spprovidedid'];
158
        }
159
160
        return $retval;
161
    }
162
163
164
    /**
165
     * Create an array from this class
166
     *
167
     * @return array
168
     */
169
    public function toArray(): array
170
    {
171
        $data = [
172
            'value' => $this->getContent(),
173
            'Format' => $this->getFormat(),
174
            'NameQualifier' => $this->getNameQualifier(),
175
            'SPNameQualifier' => $this->getSPNameQualifier(),
176
            'SPProvidedID' => $this->getSPProvidedID(),
177
        ];
178
179
        return array_filter($data);
180
    }
181
}
182