Passed
Pull Request — master (#21)
by Jaime Pérez
02:15
created

InclusiveNamespaces::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ec;
6
7
use DOMElement;
8
use Webmozart\Assert\Assert;
9
10
/**
11
 * Class implementing InclusiveNamespaces
12
 *
13
 * @package simplesamlphp/xml-security
14
 */
15
class InclusiveNamespaces extends AbstractEcElement
16
{
17
    /** @var string[] */
18
    protected array $prefixes;
19
20
21
    /**
22
     * Initialize the InclusiveNamespaces element.
23
     *
24
     * @param string[] $prefixes
25
     */
26
    public function __construct(array $prefixes)
27
    {
28
        $this->setPrefixes($prefixes);
29
    }
30
31
32
    /**
33
     * Get the prefixes specified by this element.
34
     *
35
     * @return string[]
36
     */
37
    public function getPrefixes(): array
38
    {
39
        return $this->prefixes;
40
    }
41
42
43
    /**
44
     * Set the prefixes to specify in this element.
45
     *
46
     * @param string[] $prefixes
47
     */
48
    private function setPrefixes(array $prefixes): void
49
    {
50
        Assert::allString($prefixes, 'Can only add string InclusiveNamespaces prefixes.');
51
        $this->prefixes = $prefixes;
52
    }
53
54
55
    /**
56
     * Convert XML into an InclusiveNamespaces element.
57
     *
58
     * @param \DOMElement $xml The XML element we should load.
59
     * @return self
60
     */
61
    public static function fromXML(DOMElement $xml): object
62
    {
63
        $prefixes = self::getAttribute($xml, 'PrefixList', '');
64
65
        return new self(explode(' ', $prefixes));
66
    }
67
68
    /**
69
     * Convert this InclusiveNamespaces to XML.
70
     *
71
     * @param \DOMElement|null $parent The element we should append this InclusiveNamespaces to.
72
     * @return \DOMElement
73
     */
74
    public function toXML(DOMElement $parent = null): DOMElement
75
    {
76
        $e = $this->instantiateParentElement($parent);
77
78
        if (!empty($this->prefixes)) {
79
            $e->setAttribute('PrefixList', join(' ', $this->prefixes));
80
        }
81
82
        return $e;
83
    }
84
}