Passed
Push — master ( 6db269...70aff8 )
by Jaime Pérez
02:49
created

UIInfo::toXML()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 22
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\mdui;
6
7
use DOMElement;
8
use SAML2\Utils;
9
use SAML2\XML\Chunk;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Class for handling the metadata extensions for login and discovery user interface
14
 *
15
 * @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/sstc-saml-metadata-ui-v1.0.pdf
16
 * @package simplesamlphp/saml2
17
 */
18
final class UIInfo extends AbstractMduiElement
19
{
20
    /**
21
     * Array with child elements.
22
     *
23
     * The elements can be any of the other \SAML2\XML\mdui\* elements.
24
     *
25
     * @var \SAML2\XML\Chunk[]
26
     */
27
    protected $children = [];
28
29
    /**
30
     * The DisplayName, as an array of language => translation.
31
     *
32
     * @var string[]
33
     */
34
    protected $DisplayName = [];
35
36
    /**
37
     * The Description, as an array of language => translation.
38
     *
39
     * @var string[]
40
     */
41
    protected $Description = [];
42
43
    /**
44
     * The InformationURL, as an array of language => url.
45
     *
46
     * @var string[]
47
     */
48
    protected $InformationURL = [];
49
50
    /**
51
     * The PrivacyStatementURL, as an array of language => url.
52
     *
53
     * @var array
54
     */
55
    protected $PrivacyStatementURL = [];
56
57
    /**
58
     * The Keywords, as an array of Keywords objects
59
     *
60
     * @var \SAML2\XML\mdui\Keywords[]
61
     */
62
    protected $Keywords = [];
63
64
    /**
65
     * The Logo, as an array of Logo objects
66
     *
67
     * @var \SAML2\XML\mdui\Logo[]
68
     */
69
    protected $Logo = [];
70
71
72
    /**
73
     * Create a UIInfo element.
74
     *
75
     * @param string[] $DisplayName
76
     * @param string[] $Description
77
     * @param string[] $InformationURL
78
     * @param string[] $PrivacyStatementURL
79
     * @param \SAML2\XML\mdui\Keywords[] $Keywords
80
     * @param \SAML2\XML\mdui\Logo[] $Logo
81
     * @param \SAML2\XML\Chunk[] $children
82
     */
83
    public function __construct(
84
        array $DisplayName = [],
85
        array $Description = [],
86
        array $InformationURL = [],
87
        array $PrivacyStatementURL = [],
88
        array $Keywords = [],
89
        array $Logo = [],
90
        array $children = []
91
    ) {
92
        $this->setDisplayName($DisplayName);
93
        $this->setDescription($Description);
94
        $this->setInformationURL($InformationURL);
95
        $this->setPrivacyStatementURL($PrivacyStatementURL);
96
        $this->setKeywords($Keywords);
97
        $this->setLogo($Logo);
98
        $this->setChildren($children);
99
    }
100
101
102
    /**
103
     * Collect the value of the Keywords-property
104
     *
105
     * @return \SAML2\XML\mdui\Keywords[]
106
     */
107
    public function getKeywords(): array
108
    {
109
        return $this->Keywords;
110
    }
111
112
113
    /**
114
     * Set the value of the Keywords-property
115
     *
116
     * @param \SAML2\XML\mdui\Keywords[] $keywords
117
     * @return void
118
     *
119
     * @throws \InvalidArgumentException if assertions are false
120
     */
121
    private function setKeywords(array $keywords): void
122
    {
123
        Assert::allIsInstanceOf($keywords, Keywords::class);
124
        $this->Keywords = $keywords;
125
    }
126
127
128
    /**
129
     * Add the value to the Keywords-property
130
     *
131
     * @param \SAML2\XML\mdui\Keywords $keyword
132
     * @return void
133
     */
134
    public function addKeyword(Keywords $keyword): void
135
    {
136
        $this->Keywords[] = $keyword;
137
    }
138
139
140
    /**
141
     * Collect the value of the DisplayName-property
142
     *
143
     * @return string[]
144
     */
145
    public function getDisplayName(): array
146
    {
147
        return $this->DisplayName;
148
    }
149
150
151
    /**
152
     * Set the value of the DisplayName-property
153
     *
154
     * @param string[] $displayName
155
     * @return void
156
     */
157
    private function setDisplayName(array $displayName): void
158
    {
159
        $this->DisplayName = $displayName;
160
    }
161
162
163
    /**
164
     * Collect the value of the Description-property
165
     *
166
     * @return string[]
167
     */
168
    public function getDescription(): array
169
    {
170
        return $this->Description;
171
    }
172
173
174
    /**
175
     * Set the value of the Description-property
176
     *
177
     * @param string[] $description
178
     * @return void
179
     */
180
    private function setDescription(array $description): void
181
    {
182
        $this->Description = $description;
183
    }
184
185
186
    /**
187
     * Collect the value of the InformationURL-property
188
     * @return string[]
189
     */
190
    public function getInformationURL(): array
191
    {
192
        return $this->InformationURL;
193
    }
194
195
196
    /**
197
     * Set the value of the InformationURL-property
198
     *
199
     * @param string[] $informationURL
200
     * @return void
201
     */
202
    private function setInformationURL(array $informationURL): void
203
    {
204
        $this->InformationURL = $informationURL;
205
    }
206
207
208
    /**
209
     * Collect the value of the PrivacyStatementURL-property
210
     *
211
     * @return string[]
212
     */
213
    public function getPrivacyStatementURL(): array
214
    {
215
        return $this->PrivacyStatementURL;
216
    }
217
218
219
    /**
220
     * Set the value of the PrivacyStatementURL-property
221
     *
222
     * @param string[] $privacyStatementURL
223
     * @return void
224
     */
225
    private function setPrivacyStatementURL(array $privacyStatementURL): void
226
    {
227
        $this->PrivacyStatementURL = $privacyStatementURL;
228
    }
229
230
231
    /**
232
     * Collect the value of the Logo-property
233
     *
234
     * @return \SAML2\XML\mdui\Logo[]
235
     */
236
    public function getLogo(): array
237
    {
238
        return $this->Logo;
239
    }
240
241
242
    /**
243
     * Set the value of the Logo-property
244
     *
245
     * @param \SAML2\XML\mdui\Logo[] $logo
246
     * @return void
247
     */
248
    private function setLogo(array $logo): void
249
    {
250
        $this->Logo = $logo;
251
    }
252
253
254
    /**
255
     * Add the value to the Logo-property
256
     *
257
     * @param \SAML2\XML\mdui\Logo $logo
258
     * @return void
259
     */
260
    public function addLogo(Logo $logo): void
261
    {
262
        $this->Logo[] = $logo;
263
    }
264
265
266
    /**
267
     * Collect the value of the children-property
268
     *
269
     * @return \SAML2\XML\Chunk[]
270
     */
271
    public function getChildren(): array
272
    {
273
        return $this->children;
274
    }
275
276
277
    /**
278
     * Set the value of the childen-property
279
     *
280
     * @param \SAML2\XML\Chunk[] $children
281
     * @return void
282
     */
283
    private function setChildren(array $children): void
284
    {
285
        $this->children = $children;
286
    }
287
288
289
    /**
290
     * Add the value to the children-property
291
     *
292
     * @param \SAML2\XML\Chunk $child
293
     * @return void
294
     */
295
    public function addChild(Chunk $child): void
296
    {
297
        $this->children[] = $child;
298
    }
299
300
301
    /**
302
     * Test if an object, at the state it's in, would produce an empty XML-element
303
     *
304
     * @return bool
305
     */
306
    public function isEmptyElement(): bool
307
    {
308
        return (
309
            empty($this->DisplayName)
310
            && empty($this->Description)
311
            && empty($this->InformationURL)
312
            && empty($this->PrivacyStatementURL)
313
            && empty($this->Keywords)
314
            && empty($this->Logo)
315
            && empty($this->children)
316
        );
317
    }
318
319
320
    /**
321
     * Convert XML into a UIInfo
322
     *
323
     * @param \DOMElement $xml The XML element we should load
324
     * @return self
325
     * @throws \InvalidArgumentException if the qualified name of the supplied element is wrong
326
     */
327
    public static function fromXML(DOMElement $xml): object
328
    {
329
        Assert::same($xml->localName, 'UIInfo');
330
        Assert::same($xml->namespaceURI, UIInfo::NS);
331
332
        $DisplayName = Utils::extractLocalizedStrings($xml, UIInfo::NS, 'DisplayName');
333
        $Description = Utils::extractLocalizedStrings($xml, UIInfo::NS, 'Description');
334
        $InformationURL = Utils::extractLocalizedStrings($xml, UIInfo::NS, 'InformationURL');
335
        $PrivacyStatementURL = Utils::extractLocalizedStrings($xml, UIInfo::NS, 'PrivacyStatementURL');
336
        $Keywords = $Logo = $children = [];
337
338
        /** @var \DOMElement $node */
339
        foreach (Utils::xpQuery($xml, './*') as $node) {
340
            if ($node->namespaceURI === UIInfo::NS) {
341
                switch ($node->localName) {
342
                    case 'Keywords':
343
                        $Keywords[] = Keywords::fromXML($node);
344
                        break;
345
                    case 'Logo':
346
                        $Logo[] = Logo::fromXML($node);
347
                        break;
348
                }
349
            } else {
350
                $children[] = new Chunk($node);
351
            }
352
        }
353
354
        return new self(
355
            $DisplayName,
356
            $Description,
357
            $InformationURL,
358
            $PrivacyStatementURL,
359
            $Keywords,
360
            $Logo,
361
            $children
362
        );
363
    }
364
365
366
    /**
367
     * Convert this UIInfo to XML.
368
     *
369
     * @param \DOMElement|null $parent The element we should append to.
370
     * @return \DOMElement
371
     */
372
    public function toXML(DOMElement $parent = null): DOMElement
373
    {
374
        $e = $this->instantiateParentElement($parent);
375
376
        Utils::addStrings($e, UIInfo::NS, 'mdui:DisplayName', true, $this->DisplayName);
377
        Utils::addStrings($e, UIInfo::NS, 'mdui:Description', true, $this->Description);
378
        Utils::addStrings($e, UIInfo::NS, 'mdui:InformationURL', true, $this->InformationURL);
379
        Utils::addStrings($e, UIInfo::NS, 'mdui:PrivacyStatementURL', true, $this->PrivacyStatementURL);
380
381
        foreach ($this->Keywords as $child) {
382
            $child->toXML($e);
383
        }
384
385
        foreach ($this->Logo as $child) {
386
            $child->toXML($e);
387
        }
388
389
        foreach ($this->children as $child) {
390
            $child->toXML($e);
391
        }
392
393
        return $e;
394
    }
395
}
396