Passed
Pull Request — master (#337)
by Tim
02:20
created

PublicationPath::validateArray()   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 1
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\mdrpi;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\ArrayizableElementInterface;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
14
/**
15
 * Class for handling the mdrpi:PublicationPath element.
16
 *
17
 * @link: http://docs.oasis-open.org/security/saml/Post2.0/saml-metadata-rpi/v1.0/saml-metadata-rpi-v1.0.pdf
18
 * @package simplesamlphp/saml2
19
 */
20
final class PublicationPath extends AbstractMdrpiElement implements ArrayizableElementInterface
21
{
22
    /**
23
     * Create/parse a mdrpi:PublicationPath element.
24
     *
25
     * @param \SimpleSAML\SAML2\XML\mdrpi\Publication[] $publication
26
     */
27
    public function __construct(
28
        protected array $publication = [],
29
    ) {
30
        Assert::allIsInstanceOf($publication, Publication::class);
31
    }
32
33
34
    /**
35
     * Collect the value of the Publication-property
36
     *
37
     * @return \SimpleSAML\SAML2\XML\mdrpi\Publication[]
38
     */
39
    public function getPublication(): array
40
    {
41
        return $this->publication;
42
    }
43
44
45
    /**
46
     * Test if an object, at the state it's in, would produce an empty XML-element
47
     *
48
     * @return bool
49
     */
50
    public function isEmptyElement(): bool
51
    {
52
        return empty($this->publication);
53
    }
54
55
56
    /**
57
     * Convert XML into a PublicationPath
58
     *
59
     * @param \DOMElement $xml The XML element we should load
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
65
     *   if the supplied element is missing one of the mandatory attributes
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, 'PublicationPath', InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, PublicationPath::NS, InvalidDOMElementException::class);
71
72
        $Publication = Publication::getChildrenOfClass($xml);
73
74
        return new static($Publication);
75
    }
76
77
78
    /**
79
     * Convert this element to XML.
80
     *
81
     * @param \DOMElement|null $parent The element we should append to.
82
     * @return \DOMElement
83
     */
84
    public function toXML(DOMElement $parent = null): DOMElement
85
    {
86
        $e = $this->instantiateParentElement($parent);
87
88
        foreach ($this->getPublication() as $pub) {
89
            $pub->toXML($e);
90
        }
91
92
        return $e;
93
    }
94
95
96
    /**
97
     * Create a class from an array
98
     *
99
     * @param array $data
100
     * @return static
101
     */
102
    public static function fromArray(array $data): static
103
    {
104
        self::validateArray($data);
105
106
        $publication = [];
107
        foreach ($data as $p) {
108
            $publication[] = Publication::fromArray($p);
109
        }
110
111
        return new static($publication);
112
    }
113
114
115
    /**
116
     * Validate an array
117
     *
118
     * @param array $data
119
     * @return void
120
     */
121
    public static function validateArray(array $data): void
122
    {
123
        Assert::allIsArray($data);
124
        // Actual contents are tested Publication::validateArray
125
    }
126
127
128
    /**
129
     * Create an array from this class
130
     *
131
     * @return array
132
     */
133
    public function toArray(): array
134
    {
135
        $data = [];
136
        foreach ($this->getPublication() as $p) {
137
            $data[] = $p->toArray();
138
        }
139
140
        return $data;
141
    }
142
}
143