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