Passed
Push — master ( 10ad29...95b17c )
by Tim
02:20
created

Organization::toXML()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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