IDPEntry::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Type\EntityIDValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Type\EntityIDValue 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\SAML2\Type\SAMLAnyURIValue;
12
use SimpleSAML\SAML2\Type\SAMLStringValue;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
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 for handling SAML2 IDPEntry.
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
final class IDPEntry extends AbstractSamlpElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\AbstractSamlpElement 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...
28
{
29
    use SchemaValidatableElementTrait;
30
31
32
    /**
33
     * Initialize an IDPEntry element.
34
     *
35
     * @param \SimpleSAML\SAML2\Type\EntityIDValue $providerId
36
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $name
37
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $loc
38
     */
39
    public function __construct(
40
        protected EntityIDValue $providerId,
41
        protected ?SAMLStringValue $name = null,
42
        protected ?SAMLAnyURIValue $loc = null,
43
    ) {
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\SAML2\Type\EntityIDValue
49
     */
50
    public function getProviderId(): EntityIDValue
51
    {
52
        return $this->providerId;
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
58
     */
59
    public function getName(): ?SAMLStringValue
60
    {
61
        return $this->name;
62
    }
63
64
65
    /**
66
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
67
     */
68
    public function getLoc(): ?SAMLAnyURIValue
69
    {
70
        return $this->loc;
71
    }
72
73
74
    /**
75
     * Convert XML into a IDPEntry-element
76
     *
77
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
78
     *   if the qualified name of the supplied element is wrong
79
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
80
     *   if the supplied element is missing one of the mandatory attributes
81
     */
82
    public static function fromXML(DOMElement $xml): static
83
    {
84
        Assert::same($xml->localName, 'IDPEntry', InvalidDOMElementException::class);
85
        Assert::same($xml->namespaceURI, IDPEntry::NS, InvalidDOMElementException::class);
86
87
        return new static(
88
            self::getAttribute($xml, 'ProviderID', EntityIDValue::class),
89
            self::getOptionalAttribute($xml, 'Name', SAMLStringValue::class, null),
90
            self::getOptionalAttribute($xml, 'Loc', SAMLAnyURIValue::class, null),
91
        );
92
    }
93
94
95
    /**
96
     * Convert this IDPEntry to XML.
97
     */
98
    public function toXML(?DOMElement $parent = null): DOMElement
99
    {
100
        $e = $this->instantiateParentElement($parent);
101
        $e->setAttribute('ProviderID', $this->getProviderId()->getValue());
102
103
        if ($this->getName() !== null) {
104
            $e->setAttribute('Name', $this->getName()->getValue());
105
        }
106
107
        if ($this->getLoc() !== null) {
108
            $e->setAttribute('Loc', $this->getLoc()->getValue());
109
        }
110
111
        return $e;
112
    }
113
114
115
    /**
116
     * Create a class from an array
117
     *
118
     * @param array{
119
     *   'ProviderID': string,
120
     *   'Name'?: string,
121
     *   'Loc'?: string,
122
     * } $data
123
     */
124
    public static function fromArray(array $data): static
125
    {
126
        $data = self::processArrayContents($data);
127
128
        return new static(
129
            EntityIDValue::fromString($data['ProviderID']),
130
            $data['Name'] !== null ? SAMLStringValue::fromString($data['Name']) : null,
131
            $data['Loc'] !== null ? SAMLAnyURIValue::fromString($data['Loc']) : null,
132
        );
133
    }
134
135
136
    /**
137
     * Validates an array representation of this object and returns the same array with
138
     * rationalized keys (casing) and parsed sub-elements.
139
     *
140
     * @param array{
141
     *   'ProviderID': string,
142
     *   'Name'?: string,
143
     *   'Loc'?: string,
144
     * } $data
145
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
146
     *   'ProviderID': string,
147
     *   'Name'?: string,
148
     *   'Loc'?: string,
149
     * }
150
     */
151
    private static function processArrayContents(array $data): array
152
    {
153
        $data = array_change_key_case($data, CASE_LOWER);
154
155
        // Make sure the array keys are known for this kind of object
156
        Assert::allOneOf(
157
            array_keys($data),
158
            [
159
                'providerid',
160
                'name',
161
                'loc',
162
            ],
163
            ArrayValidationException::class,
164
        );
165
166
        Assert::keyExists($data, 'providerid', ArrayValidationException::class);
167
        Assert::string($data['providerid'], ArrayValidationException::class);
168
169
        $retval = ['ProviderID' => $data['providerid']];
170
171
        if (array_key_exists('name', $data)) {
172
            Assert::string($data['name'], ArrayValidationException::class);
173
            $retval['Name'] = $data['name'];
174
        }
175
176
        if (array_key_exists('loc', $data)) {
177
            Assert::string($data['loc'], ArrayValidationException::class);
178
            $retval['Loc'] = $data['loc'];
179
        }
180
181
        return $retval;
182
    }
183
184
185
    /**
186
     * Create an array from this class
187
     *
188
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
189
     *   'ProviderID': string,
190
     *   'Name'?: string,
191
     *   'Loc'?: string,
192
     * }
193
     */
194
    public function toArray(): array
195
    {
196
        $data = [
197
            'ProviderID' => $this->getProviderID()->getValue(),
198
            'Name' => $this->getName()?->getValue(),
199
            'Loc' => $this->getLoc()?->getValue(),
200
        ];
201
202
        return array_filter($data);
203
    }
204
}
205