Passed
Push — master ( eb6d7f...017f7a )
by Tim
02:40
created

EntitiesDescriptor::toXML()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 32
nop 1
dl 0
loc 27
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
15
/**
16
 * Class representing SAML 2 EntitiesDescriptor element.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
final class EntitiesDescriptor extends AbstractMetadataDocument
21
{
22
    /**
23
     * The name of this entity collection.
24
     *
25
     * @var string|null
26
     */
27
    protected ?string $Name = null;
28
29
    /** @var \SimpleSAML\SAML2\XML\md\EntityDescriptor[] */
30
    protected array $entityDescriptors = [];
31
32
    /** @var \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] */
33
    protected array $entitiesDescriptors = [];
34
35
36
    /**
37
     * EntitiesDescriptor constructor.
38
     *
39
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
40
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
41
     * @param string|null $name
42
     * @param string|null $ID
43
     * @param int|null $validUntil
44
     * @param string|null $cacheDuration
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
46
     * @param \DOMAttr[] $namespacedAttributes
47
     */
48
    public function __construct(
49
        array $entityDescriptors = [],
50
        array $entitiesDescriptors = [],
51
        ?string $name = null,
52
        ?string $ID = null,
53
        ?int $validUntil = null,
54
        ?string $cacheDuration = null,
55
        ?Extensions $extensions = null,
56
        array $namespacedAttributes = []
57
    ) {
58
        Assert::true(
59
            !empty($entitiesDescriptors) || !empty($entityDescriptors),
60
            'At least one md:EntityDescriptor or md:EntitiesDescriptor element is required.',
61
            ProtocolViolationException::class,
62
        );
63
64
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
65
66
        $this->setName($name);
67
        $this->setEntityDescriptors($entityDescriptors);
68
        $this->setEntitiesDescriptors($entitiesDescriptors);
69
    }
70
71
72
    /**
73
     * Initialize an EntitiesDescriptor from an existing XML document.
74
     *
75
     * @param \DOMElement $xml The XML element we should load.
76
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor
77
     *
78
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
79
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, 'EntitiesDescriptor', InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, EntitiesDescriptor::NS, InvalidDOMElementException::class);
85
86
        $validUntil = self::getAttribute($xml, 'validUntil', null);
87
        $orgs = Organization::getChildrenOfClass($xml);
88
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
89
90
        $extensions = Extensions::getChildrenOfClass($xml);
91
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
92
93
        $signature = Signature::getChildrenOfClass($xml);
94
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
95
96
        $entities = new static(
97
            EntityDescriptor::getChildrenOfClass($xml),
98
            EntitiesDescriptor::getChildrenOfClass($xml),
99
            self::getAttribute($xml, 'Name', null),
100
            self::getAttribute($xml, 'ID', null),
101
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
102
            self::getAttribute($xml, 'cacheDuration', null),
103
            !empty($extensions) ? $extensions[0] : null,
104
            self::getAttributesNSFromXML($xml)
105
        );
106
107
        if (!empty($signature)) {
108
            $entities->setSignature($signature[0]);
109
            $entities->setXML($xml);
110
        }
111
112
        return $entities;
113
    }
114
115
116
    /**
117
     * Get the EntitiesDescriptor children objects
118
     *
119
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[]
120
     */
121
    public function getEntitiesDescriptors(): array
122
    {
123
        return $this->entitiesDescriptors;
124
    }
125
126
127
    /**
128
     * Set the EntitiesDescriptor children objects
129
     *
130
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
131
     */
132
    protected function setEntitiesDescriptors(array $entitiesDescriptors): void
133
    {
134
        Assert::allIsInstanceOf($entitiesDescriptors, EntitiesDescriptor::class);
135
        $this->entitiesDescriptors = $entitiesDescriptors;
136
    }
137
138
139
    /**
140
     * Get the EntityDescriptor children objects
141
     *
142
     * @return \SimpleSAML\SAML2\XML\md\EntityDescriptor[]
143
     */
144
    public function getEntityDescriptors(): array
145
    {
146
        return $this->entityDescriptors;
147
    }
148
149
150
151
    /**
152
     * Set the EntityDescriptor children objects
153
     *
154
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
155
     */
156
    protected function setEntityDescriptors(array $entityDescriptors): void
157
    {
158
        Assert::allIsInstanceOf($entityDescriptors, EntityDescriptor::class);
159
        $this->entityDescriptors = $entityDescriptors;
160
    }
161
162
163
164
    /**
165
     * Collect the value of the Name property.
166
     *
167
     * @return string|null
168
     */
169
    public function getName(): ?string
170
    {
171
        return $this->Name;
172
    }
173
174
175
    /**
176
     * Set the value of the Name property.
177
     *
178
     * @param string|null $name
179
     */
180
    protected function setName(?string $name = null): void
181
    {
182
        if ($name === null) {
183
            return;
184
        }
185
        $this->Name = $name;
186
    }
187
188
189
    /**
190
     * Convert this assertion to an unsigned XML document.
191
     * This method does not sign the resulting XML document.
192
     *
193
     * @return \DOMElement The root element of the DOM tree
194
     */
195
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
196
    {
197
        $e = parent::toUnsignedXML($parent);
198
199
        foreach ($this->getAttributesNS() as $attr) {
200
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
201
        }
202
203
        if ($this->Name !== null) {
204
            $e->setAttribute('Name', $this->Name);
205
        }
206
207
        foreach ($this->entitiesDescriptors as $entitiesDescriptor) {
208
            $entitiesDescriptor->toXML($e);
209
        }
210
211
        foreach ($this->entityDescriptors as $entityDescriptor) {
212
            $entityDescriptor->toXML($e);
213
        }
214
215
        return $e;
216
    }
217
}
218