Organization::getOrganizationName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\ArrayizableElementInterface;
13
use SimpleSAML\XML\Attribute as XMLAttribute;
14
use SimpleSAML\XML\Constants as C;
15
use SimpleSAML\XML\Exception\InvalidDOMElementException;
16
use SimpleSAML\XML\Exception\MissingElementException;
17
use SimpleSAML\XML\Exception\TooManyElementsException;
18
use SimpleSAML\XML\ExtendableAttributesTrait;
19
use SimpleSAML\XML\SchemaValidatableElementInterface;
20
use SimpleSAML\XML\SchemaValidatableElementTrait;
21
use SimpleSAML\XML\XsNamespace as NS;
22
23
use function array_change_key_case;
24
use function array_filter;
25
use function array_key_exists;
26
use function array_keys;
27
use function array_merge;
28
29
/**
30
 * Class representing SAML 2 Organization element.
31
 *
32
 * @package simplesamlphp/saml2
33
 */
34
final class Organization extends AbstractMdElement implements
35
    ArrayizableElementInterface,
36
    SchemaValidatableElementInterface
37
{
38
    use ExtendableAttributesTrait;
39
    use ExtendableElementTrait;
40
    use SchemaValidatableElementTrait;
41
42
    /** The namespace-attribute for the xs:anyAttribute element */
43
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
44
45
46
    /**
47
     * Organization constructor.
48
     *
49
     * @param \SimpleSAML\SAML2\XML\md\OrganizationName[] $organizationName
50
     * @param \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[] $organizationDisplayName
51
     * @param \SimpleSAML\SAML2\XML\md\OrganizationURL[] $organizationURL
52
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
53
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list 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...
54
     */
55
    public function __construct(
56
        protected array $organizationName,
57
        protected array $organizationDisplayName,
58
        protected array $organizationURL,
59
        ?Extensions $extensions = null,
60
        array $namespacedAttributes = [],
61
    ) {
62
        Assert::maxCount($organizationName, C::UNBOUNDED_LIMIT);
63
        Assert::maxCount($organizationDisplayName, C::UNBOUNDED_LIMIT);
64
        Assert::maxCount($organizationURL, C::UNBOUNDED_LIMIT);
65
66
        // [One or More]
67
        Assert::minCount($organizationName, 1, ProtocolViolationException::class);
68
        Assert::minCount($organizationDisplayName, 1, ProtocolViolationException::class);
69
        Assert::minCount($organizationURL, 1, ProtocolViolationException::class);
70
71
        Assert::allIsInstanceOf($organizationName, OrganizationName::class);
72
        Assert::allIsInstanceOf($organizationDisplayName, OrganizationDisplayName::class);
73
        Assert::allIsInstanceOf($organizationURL, OrganizationURL::class);
74
75
        $this->setExtensions($extensions);
76
        $this->setAttributesNS($namespacedAttributes);
77
    }
78
79
80
    /**
81
     * Collect the value of the OrganizationName property.
82
     *
83
     * @return \SimpleSAML\SAML2\XML\md\OrganizationName[]
84
     */
85
    public function getOrganizationName(): array
86
    {
87
        return $this->organizationName;
88
    }
89
90
91
    /**
92
     * Collect the value of the OrganizationDisplayName property.
93
     *
94
     * @return \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[]
95
     */
96
    public function getOrganizationDisplayName(): array
97
    {
98
        return $this->organizationDisplayName;
99
    }
100
101
102
    /**
103
     * Collect the value of the OrganizationURL property.
104
     *
105
     * @return \SimpleSAML\SAML2\XML\md\OrganizationURL[]
106
     */
107
    public function getOrganizationURL(): array
108
    {
109
        return $this->organizationURL;
110
    }
111
112
113
    /**
114
     * Initialize an Organization element.
115
     *
116
     * @param \DOMElement $xml The XML element we should load.
117
     * @return static
118
     *
119
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
120
     *   if the qualified name of the supplied element is wrong
121
     * @throws \SimpleSAML\XML\Exception\MissingElementException
122
     *   if one of the mandatory child-elements is missing
123
     */
124
    public static function fromXML(DOMElement $xml): static
125
    {
126
        Assert::same($xml->localName, 'Organization', InvalidDOMElementException::class);
127
        Assert::same($xml->namespaceURI, Organization::NS, InvalidDOMElementException::class);
128
129
        $names = OrganizationName::getChildrenOfClass($xml);
130
        Assert::minCount($names, 1, 'Missing at least one OrganizationName.', MissingElementException::class);
131
132
        $displayNames = OrganizationDisplayName::getChildrenOfClass($xml);
133
        Assert::minCount(
134
            $displayNames,
135
            1,
136
            'Missing at least one OrganizationDisplayName',
137
            MissingElementException::class,
138
        );
139
140
        $urls = OrganizationURL::getChildrenOfClass($xml);
141
        Assert::minCount($urls, 1, 'Missing at least one OrganizationURL', MissingElementException::class);
142
143
        $extensions = Extensions::getChildrenOfClass($xml);
144
        Assert::maxCount(
145
            $extensions,
146
            1,
147
            'Cannot process more than one md:Extensions element.',
148
            TooManyElementsException::class,
149
        );
150
151
        return new static(
152
            $names,
153
            $displayNames,
154
            $urls,
155
            !empty($extensions) ? $extensions[0] : null,
156
            self::getAttributesNSFromXML($xml),
157
        );
158
    }
159
160
161
    /**
162
     * Convert this Organization to XML.
163
     *
164
     * @param \DOMElement|null $parent The element we should add this organization to.
165
     * @return \DOMElement This Organization-element.
166
     */
167
    public function toXML(?DOMElement $parent = null): DOMElement
168
    {
169
        $e = $this->instantiateParentElement($parent);
170
171
        foreach ($this->getAttributesNS() as $attr) {
172
            $attr->toXML($e);
173
        }
174
175
        $this->getExtensions()?->toXML($e);
176
177
        foreach ($this->getOrganizationName() as $name) {
178
            $name->toXML($e);
179
        }
180
181
        foreach ($this->getOrganizationDisplayName() as $displayName) {
182
            $displayName->toXML($e);
183
        }
184
185
        foreach ($this->getOrganizationURL() as $url) {
186
            $url->toXML($e);
187
        }
188
189
        return $e;
190
    }
191
192
193
    /**
194
     * Create a class from an array
195
     *
196
     * @param array $data
197
     * @return static
198
     */
199
    public static function fromArray(array $data): static
200
    {
201
        $data = self::processArrayContents($data);
202
203
        return new static(
204
            $data['OrganizationName'],
205
            $data['OrganizationDisplayName'],
206
            $data['OrganizationURL'],
207
            $data['Extensions'] ?? null,
208
            $data['attributes'] ?? [],
209
        );
210
    }
211
212
213
    /**
214
     * Validates an array representation of this object and returns the same array with
215
     * rationalized keys (casing) and parsed sub-elements.
216
     *
217
     * @param array $data
218
     * @return array $data
219
     */
220
    private static function processArrayContents(array $data): array
221
    {
222
        $data = array_change_key_case($data, CASE_LOWER);
223
224
        // Make sure the array keys are known for this kind of object
225
        Assert::allOneOf(
226
            array_keys($data),
227
            [
228
                'organizationname',
229
                'organizationdisplayname',
230
                'organizationurl',
231
                'extensions',
232
                'attributes',
233
            ],
234
            ArrayValidationException::class,
235
        );
236
237
        Assert::keyExists($data, 'organizationname', ArrayValidationException::class);
238
        Assert::keyExists($data, 'organizationdisplayname', ArrayValidationException::class);
239
        Assert::keyExists($data, 'organizationurl', ArrayValidationException::class);
240
241
        // The minimum count is validated by the constructor
242
        Assert::isArray($data['organizationname'], ArrayValidationException::class);
243
        Assert::isArray($data['organizationdisplayname'], ArrayValidationException::class);
244
        Assert::isArray($data['organizationurl'], ArrayValidationException::class);
245
246
        foreach ($data['organizationname'] as $lang => $orgName) {
247
            $data['organizationname'][$lang] = OrganizationName::fromArray([$lang => $orgName]);
248
        }
249
250
        foreach ($data['organizationdisplayname'] as $lang => $orgDisplayName) {
251
            $data['organizationdisplayname'][$lang] = OrganizationDisplayName::fromArray([$lang => $orgDisplayName]);
252
        }
253
254
        foreach ($data['organizationurl'] as $lang => $orgUrl) {
255
            $data['organizationurl'][$lang] = OrganizationURL::fromArray([$lang => $orgUrl]);
256
        }
257
258
        $retval = [
259
            'OrganizationName' => $data['organizationname'],
260
            'OrganizationDisplayName' => $data['organizationdisplayname'],
261
            'OrganizationURL' => $data['organizationurl'],
262
        ];
263
264
        if (array_key_exists('extensions', $data)) {
265
            Assert::isArray($data['extensions'], ArrayValidationException::class);
266
            $retval['Extensions'] = new Extensions($data['extensions']);
267
        }
268
269
        if (array_key_exists('attributes', $data)) {
270
            Assert::isArray($data['attributes'], ArrayValidationException::class);
271
            Assert::allIsArray($data['attributes'], ArrayValidationException::class);
272
            foreach ($data['attributes'] as $i => $attr) {
273
                $retval['attributes'][] = XMLAttribute::fromArray($attr);
274
            }
275
        }
276
277
        return $retval;
278
    }
279
280
281
    /**
282
     * Create an array from this class
283
     *
284
     * @return array
285
     */
286
    public function toArray(): array
287
    {
288
        $data = [
289
            'OrganizationName' => [],
290
            'OrganizationDisplayName' => [],
291
            'OrganizationURL' => [],
292
            'Extensions' => $this->getExtensions()?->getList(),
0 ignored issues
show
Bug introduced by
The method getList() does not exist on SimpleSAML\XML\AbstractElement. It seems like you code against a sub-type of SimpleSAML\XML\AbstractElement such as SimpleSAML\SAML2\XML\md\Extensions or SimpleSAML\SAML2\XML\samlp\Extensions. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

292
            'Extensions' => $this->getExtensions()?->/** @scrutinizer ignore-call */ getList(),
Loading history...
293
            'attributes' => [],
294
        ];
295
296
        foreach ($this->getOrganizationName() as $orgName) {
297
            $data['OrganizationName'] = array_merge($data['OrganizationName'], $orgName->toArray());
298
        }
299
300
        foreach ($this->getOrganizationDisplayName() as $orgDisplayName) {
301
            $data['OrganizationDisplayName'] = array_merge(
302
                $data['OrganizationDisplayName'],
303
                $orgDisplayName->toArray(),
304
            );
305
        }
306
307
        foreach ($this->getOrganizationURL() as $orgURL) {
308
            $data['OrganizationURL'] = array_merge($data['OrganizationURL'], $orgURL->toArray());
309
        }
310
311
        foreach ($this->getAttributesNS() as $attr) {
312
            $data['attributes'][] = $attr->toArray();
313
        }
314
315
        return array_filter($data);
316
    }
317
}
318