Passed
Push — master ( c66cd3...78be34 )
by Tim
12:20
created

Organization::getOrganizationName()   A

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 DOMDocument;
8
use DOMElement;
9
use Exception;
10
use SimpleSAML\Assert\Assert;
11
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
12
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\Constants as C;
14
use SimpleSAML\XML\ArrayizableElementInterface;
15
use SimpleSAML\XML\Attribute as XMLAttribute;
16
use SimpleSAML\XML\Exception\InvalidDOMElementException;
17
use SimpleSAML\XML\Exception\MissingElementException;
18
use SimpleSAML\XML\Exception\TooManyElementsException;
19
use SimpleSAML\XML\ExtendableAttributesTrait;
20
use SimpleSAML\XML\Utils as XMLUtils;
21
22
use function array_key_exists;
23
use function array_merge;
24
25
/**
26
 * Class representing SAML 2 Organization element.
27
 *
28
 * @package simplesamlphp/saml2
29
 */
30
final class Organization extends AbstractMdElement implements ArrayizableElementInterface
31
{
32
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\Organization: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
33
    use ExtendableElementTrait;
34
35
    /** The namespace-attribute for the xs:anyAttribute element */
36
    public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_OTHER;
37
38
39
    /**
40
     * Organization constructor.
41
     *
42
     * @param \SimpleSAML\SAML2\XML\md\OrganizationName[] $organizationName
43
     * @param \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[] $organizationDisplayName
44
     * @param \SimpleSAML\SAML2\XML\md\OrganizationURL[] $organizationURL
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
46
     * @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...
47
     */
48
    public function __construct(
49
        protected array $organizationName,
50
        protected array $organizationDisplayName,
51
        protected array $organizationURL,
52
        ?Extensions $extensions = null,
53
        array $namespacedAttributes = [],
54
    ) {
55
        Assert::allIsInstanceOf($organizationName, OrganizationName::class);
56
        Assert::allIsInstanceOf($organizationDisplayName, OrganizationDisplayName::class);
57
        Assert::allIsInstanceOf($organizationURL, OrganizationURL::class);
58
59
        // [One or More]
60
        Assert::minCount($organizationName, 1, ProtocolViolationException::class);
61
        Assert::minCount($organizationDisplayName, 1, ProtocolViolationException::class);
62
        Assert::minCount($organizationURL, 1, ProtocolViolationException::class);
63
64
        $this->setExtensions($extensions);
65
        $this->setAttributesNS($namespacedAttributes);
66
    }
67
68
69
    /**
70
     * Collect the value of the OrganizationName property.
71
     *
72
     * @return \SimpleSAML\SAML2\XML\md\OrganizationName[]
73
     */
74
    public function getOrganizationName(): array
75
    {
76
        return $this->organizationName;
77
    }
78
79
80
    /**
81
     * Collect the value of the OrganizationDisplayName property.
82
     *
83
     * @return \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[]
84
     */
85
    public function getOrganizationDisplayName(): array
86
    {
87
        return $this->organizationDisplayName;
88
    }
89
90
91
    /**
92
     * Collect the value of the OrganizationURL property.
93
     *
94
     * @return \SimpleSAML\SAML2\XML\md\OrganizationURL[]
95
     */
96
    public function getOrganizationURL(): array
97
    {
98
        return $this->organizationURL;
99
    }
100
101
102
    /**
103
     * Initialize an Organization element.
104
     *
105
     * @param \DOMElement $xml The XML element we should load.
106
     * @return static
107
     *
108
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
109
     *   if the qualified name of the supplied element is wrong
110
     * @throws \SimpleSAML\XML\Exception\MissingElementException
111
     *   if one of the mandatory child-elements is missing
112
     */
113
    public static function fromXML(DOMElement $xml): static
114
    {
115
        Assert::same($xml->localName, 'Organization', InvalidDOMElementException::class);
116
        Assert::same($xml->namespaceURI, Organization::NS, InvalidDOMElementException::class);
117
118
        $names = OrganizationName::getChildrenOfClass($xml);
119
        Assert::minCount($names, 1, 'Missing at least one OrganizationName.', MissingElementException::class);
120
121
        $displayNames = OrganizationDisplayName::getChildrenOfClass($xml);
122
        Assert::minCount(
123
            $displayNames,
124
            1,
125
            'Missing at least one OrganizationDisplayName',
126
            MissingElementException::class,
127
        );
128
129
        $urls = OrganizationURL::getChildrenOfClass($xml);
130
        Assert::minCount($urls, 1, 'Missing at least one OrganizationURL', MissingElementException::class);
131
132
        $extensions = Extensions::getChildrenOfClass($xml);
133
        Assert::maxCount(
134
            $extensions,
135
            1,
136
            'Cannot process more than one md:Extensions element.',
137
            TooManyElementsException::class,
138
        );
139
140
        return new static(
141
            $names,
142
            $displayNames,
143
            $urls,
144
            !empty($extensions) ? $extensions[0] : null,
145
            self::getAttributesNSFromXML($xml),
146
        );
147
    }
148
149
150
    /**
151
     * Convert this Organization to XML.
152
     *
153
     * @param \DOMElement|null $parent The element we should add this organization to.
154
     * @return \DOMElement This Organization-element.
155
     */
156
    public function toXML(DOMElement $parent = null): DOMElement
157
    {
158
        $e = $this->instantiateParentElement($parent);
159
160
        foreach ($this->getAttributesNS() as $attr) {
161
            $attr->toXML($e);
162
        }
163
164
        $this->getExtensions()?->toXML($e);
165
166
        foreach ($this->getOrganizationName() as $name) {
167
            $name->toXML($e);
168
        }
169
170
        foreach ($this->getOrganizationDisplayName() as $displayName) {
171
            $displayName->toXML($e);
172
        }
173
174
        foreach ($this->getOrganizationURL() as $url) {
175
            $url->toXML($e);
176
        }
177
178
        return $e;
179
    }
180
181
182
    /**
183
     * Create a class from an array
184
     *
185
     * @param array $data
186
     * @return static
187
     */
188
    public static function fromArray(array $data): static
189
    {
190
        $orgNames = [];
191
        if (array_key_exists('OrganizationName', $data)) {
192
            Assert::count($data['OrganizationName'], 1);
193
            $orgNames[] = OrganizationName::fromArray($data['OrganizationName']);
194
        }
195
196
        $orgDisplayNames = [];
197
        if (array_key_exists('OrganizationDisplayName', $data)) {
198
            Assert::count($data['OrganizationDisplayName'], 1);
199
            $orgDisplayNames[] = OrganizationDisplayName::fromArray($data['OrganizationDisplayName']);
200
        }
201
202
        $orgURLs = [];
203
        if (array_key_exists('OrganizationURL', $data)) {
204
            Assert::count($data['OrganizationURL'], 1);
205
            $orgURLs[] = OrganizationURL::fromArray($data['OrganizationURL']);
206
        }
207
208
        $Extensions = $data['Extensions'] ?? null;
209
210
        $attributes = [];
211
        if (array_key_exists('attributes', $data)) {
212
            foreach ($data['attributes'] as $attr) {
213
                Assert::keyExists($attr, 'namespaceURI');
214
                Assert::keyExists($attr, 'namespacePrefix');
215
                Assert::keyExists($attr, 'attrName');
216
                Assert::keyExists($attr, 'attrValue');
217
218
                $attributes[] = new XMLAttribute(
219
                    $attr['namespaceURI'],
220
                    $attr['namespacePrefix'],
221
                    $attr['attrName'],
222
                    $attr['attrValue'],
223
                );
224
            }
225
        }
226
227
        return new static(
228
            $orgNames,
229
            $orgDisplayNames,
230
            $orgURLs,
231
            $Extensions,
232
            $attributes,
233
        );
234
    }
235
236
237
    /**
238
     * Create an array from this class
239
     *
240
     * @return array
241
     */
242
    public function toArray(): array
243
    {
244
        $data = [
245
            'OrganizationName' => [],
246
            'OrganizationDisplayName' => [],
247
            'OrganizationURL' => [],
248
            'Extensions' => $this->getExtensions(),
249
            'attributes' => [],
250
        ];
251
252
        foreach ($this->getOrganizationName() as $orgName) {
253
            $data['OrganizationName'] = array_merge($data['OrganizationName'], $orgName->toArray());
254
        }
255
256
        foreach ($this->getOrganizationDisplayName() as $orgDisplayName) {
257
            $data['OrganizationDisplayName'] = array_merge(
258
                $data['OrganizationDisplayName'],
259
                $orgDisplayName->toArray(),
260
            );
261
        }
262
263
        foreach ($this->getOrganizationURL() as $orgURL) {
264
            $data['OrganizationURL'] = array_merge($data['OrganizationURL'], $orgURL->toArray());
265
        }
266
267
        foreach ($this->getAttributesNS() as $attr) {
268
            $data['attributes'][] = $attr->toArray();
269
        }
270
271
        return $data;
272
    }
273
}
274