Passed
Pull Request — master (#351)
by Tim
02:34
created

NameID::fromXML()   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 1
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 SimpleSAML\Assert\Assert;
8
use SimpleSAML\SAML2\Compat\ContainerSingleton;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
11
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
12
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
13
14
use function array_change_key_case;
15
use function array_filter;
16
use function array_key_exists;
17
use function array_keys;
18
19
/**
20
 * Class representing the saml:NameID element.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class NameID extends NameIDType implements EncryptableElementInterface
25
{
26
    use EncryptableElementTrait;
27
28
    /**
29
     * Initialize a saml:NameID
30
     *
31
     * @param string $value
32
     * @param string|null $NameQualifier
33
     * @param string|null $SPNameQualifier
34
     * @param string|null $Format
35
     * @param string|null $SPProvidedID
36
     */
37
    public function __construct(
38
        string $value,
39
        ?string $NameQualifier = null,
40
        ?string $SPNameQualifier = null,
41
        ?string $Format = null,
42
        ?string $SPProvidedID = null,
43
    ) {
44
        parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
45
    }
46
47
48
    public function getBlacklistedAlgorithms(): ?array
49
    {
50
        $container = ContainerSingleton::getInstance();
51
        return $container->getBlacklistedEncryptionAlgorithms();
52
    }
53
54
55
    public function getEncryptionBackend(): ?EncryptionBackend
56
    {
57
        // return the encryption backend you want to use,
58
        // or null if you are fine with the default
59
        return null;
60
    }
61
62
63
    /**
64
     * Create a class from an array
65
     *
66
     * @param array $data
67
     * @return static
68
     */
69
    public static function fromArray(array $data): static
70
    {
71
        $data = self::processArrayContents($data);
72
73
        return new static(
74
            $data['value'],
75
            $data['NameQualifier'] ?? null,
76
            $data['SPNameQualifier'] ?? null,
77
            $data['Format'] ?? null,
78
            $data['SPProvidedID'] ?? null,
79
        );
80
    }
81
82
83
    /**
84
     * Validates an array representation of this object and returns the same array with
85
     * rationalized keys (casing) and parsed sub-elements.
86
     *
87
     * @param array $data
88
     * @return array $data
89
     */
90
    private static function processArrayContents(array $data): array
91
    {
92
        $data = array_change_key_case($data, CASE_LOWER);
93
94
        // Make sure the array keys are known for this kind of object
95
        Assert::allOneOf(
96
            array_keys($data),
97
            [
98
                'value',
99
                'format',
100
                'namequalifier',
101
                'spnamequalifier',
102
                'spprovidedid',
103
            ],
104
            ArrayValidationException::class,
105
        );
106
107
        Assert::keyExists($data, 'value', ArrayValidationException::class);
108
        Assert::string($data['value'], ArrayValidationException::class);
109
        $retval = ['value' => $data['value']];
110
111
        if (array_key_exists('format', $data)) {
112
            Assert::string($data['format'], ArrayValidationException::class);
113
            $retval['Format'] = $data['format'];
114
        }
115
116
        if (array_key_exists('namequalifier', $data)) {
117
            Assert::string($data['namequalifier'], ArrayValidationException::class);
118
            $retval['NameQualifier'] = $data['namequalifier'];
119
        }
120
121
        if (array_key_exists('spnamequalifier', $data)) {
122
            Assert::string($data['spnamequalifier'], ArrayValidationException::class);
123
            $retval['SPNameQualifier'] = $data['spnamequalifier'];
124
        }
125
126
        if (array_key_exists('spprovidedid', $data)) {
127
            Assert::string($data['spprovidedid'], ArrayValidationException::class);
128
            $retval['SPProvidedID'] = $data['spprovidedid'];
129
        }
130
131
        return $retval;
132
    }
133
134
135
    /**
136
     * Create an array from this class
137
     *
138
     * @return array
139
     */
140
    public function toArray(): array
141
    {
142
        $data = [
143
            'value' => $this->getContent(),
144
            'Format' => $this->getFormat(),
145
            'NameQualifier' => $this->getNameQualifier(),
146
            'SPNameQualifier' => $this->getSPNameQualifier(),
147
            'SPProvidedID' => $this->getSPProvidedID(),
148
        ];
149
150
        return array_filter($data);
151
    }
152
}
153