Passed
Push — master ( d693eb...c92125 )
by Tim
12:15
created

NameID::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\XML\Exception\ArrayValidationException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\Exception\ArrayValidationException 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...
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
15
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
16
17
use function array_change_key_case;
18
use function array_filter;
19
use function array_key_exists;
20
use function array_keys;
21
22
/**
23
 * Class representing the saml:NameID element.
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
final class NameID extends NameIDType implements EncryptableElementInterface
28
{
29
    use EncryptableElementTrait;
30
31
    /**
32
     * Initialize a saml:NameID
33
     *
34
     * @param string $value
35
     * @param string|null $NameQualifier
36
     * @param string|null $SPNameQualifier
37
     * @param string|null $Format
38
     * @param string|null $SPProvidedID
39
     */
40
    public function __construct(
41
        string $value,
42
        ?string $NameQualifier = null,
43
        ?string $SPNameQualifier = null,
44
        ?string $Format = null,
45
        ?string $SPProvidedID = null,
46
    ) {
47
        parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
48
    }
49
50
51
    /**
52
     * Convert XML into an NameID
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, 'NameID', InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, NameID::NS, InvalidDOMElementException::class);
64
65
        $NameQualifier = self::getOptionalAttribute($xml, 'NameQualifier', null);
66
        $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', null);
67
        $Format = self::getOptionalAttribute($xml, 'Format', null);
68
        $SPProvidedID = self::getOptionalAttribute($xml, 'SPProvidedID', null);
69
70
        return new static($xml->textContent, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
71
    }
72
73
74
    public function getBlacklistedAlgorithms(): ?array
75
    {
76
        $container = ContainerSingleton::getInstance();
77
        return $container->getBlacklistedEncryptionAlgorithms();
78
    }
79
80
81
    public function getEncryptionBackend(): ?EncryptionBackend
82
    {
83
        // return the encryption backend you want to use,
84
        // or null if you are fine with the default
85
        return null;
86
    }
87
88
89
    /**
90
     * Create a class from an array
91
     *
92
     * @param array $data
93
     * @return static
94
     */
95
    public static function fromArray(array $data): static
96
    {
97
        $data = self::processArrayContents($data);
98
99
        return new static(
100
            $data['value'],
101
            $data['NameQualifier'] ?? null,
102
            $data['SPNameQualifier'] ?? null,
103
            $data['Format'] ?? null,
104
            $data['SPProvidedID'] ?? null,
105
        );
106
    }
107
108
109
    /**
110
     * Validates an array representation of this object and returns the same array with
111
     * rationalized keys (casing) and parsed sub-elements.
112
     *
113
     * @param array $data
114
     * @return array $data
115
     */
116
    private static function processArrayContents(array $data): array
117
    {
118
        $data = array_change_key_case($data, CASE_LOWER);
119
120
        // Make sure the array keys are known for this kind of object
121
        Assert::allOneOf(
122
            array_keys($data),
123
            [
124
                'value',
125
                'format',
126
                'namequalifier',
127
                'spnamequalifier',
128
                'spprovidedid',
129
            ],
130
            ArrayValidationException::class,
131
        );
132
133
        Assert::keyExists($data, 'value', ArrayValidationException::class);
134
        Assert::string($data['value'], ArrayValidationException::class);
135
        $retval = ['value' => $data['value']];
136
137
        if (array_key_exists('format', $data)) {
138
            Assert::string($data['format'], ArrayValidationException::class);
139
            $retval['Format'] = $data['format'];
140
        }
141
142
        if (array_key_exists('namequalifier', $data)) {
143
            Assert::string($data['namequalifier'], ArrayValidationException::class);
144
            $retval['NameQualifier'] = $data['namequalifier'];
145
        }
146
147
        if (array_key_exists('spnamequalifier', $data)) {
148
            Assert::string($data['spnamequalifier'], ArrayValidationException::class);
149
            $retval['SPNameQualifier'] = $data['spnamequalifier'];
150
        }
151
152
        if (array_key_exists('spprovidedid', $data)) {
153
            Assert::string($data['spprovidedid'], ArrayValidationException::class);
154
            $retval['SPProvidedID'] = $data['spprovidedid'];
155
        }
156
157
        return $retval;
158
    }
159
160
161
    /**
162
     * Create an array from this class
163
     *
164
     * @return array
165
     */
166
    public function toArray(): array
167
    {
168
        $data = [
169
            'value' => $this->getContent(),
170
            'Format' => $this->getFormat(),
171
            'NameQualifier' => $this->getNameQualifier(),
172
            'SPNameQualifier' => $this->getSPNameQualifier(),
173
            'SPProvidedID' => $this->getSPProvidedID(),
174
        ];
175
176
        return array_filter($data);
177
    }
178
}
179