Passed
Pull Request — master (#280)
by Tim
02:43 queued 22s
created

AbstractMetadataDocument::setXML()   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
namespace SimpleSAML\SAML2\XML\md;
4
5
use DOMElement;
6
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
7
use SimpleSAML\XML\ExtendableAttributesTrait;
8
9
use function gmdate;
10
11
/**
12
 * Class to represent a metadata document
13
 *
14
 * @package simplesamlphp/saml2
15
 */
16
abstract class AbstractMetadataDocument extends AbstractSignedMdElement
17
{
18
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractMetadataDocument: $namespaceURI, $nodeName, $attributes, $value
Loading history...
19
    use ExtendableElementTrait;
20
21
    /**
22
     * The ID of this element.
23
     *
24
     * @var string|null
25
     */
26
    protected ?string $Id;
27
28
    /** @var \DOMElement $xml */
29
    protected DOMElement $xml;
30
31
    /**
32
     * How long this element is valid, as a unix timestamp.
33
     *
34
     * @var int|null
35
     */
36
    protected ?int $validUntil;
37
38
    /**
39
     * The length of time this element can be cached, as string.
40
     *
41
     * @var string|null
42
     */
43
    protected ?string $cacheDuration;
44
45
46
    /**
47
     * Generic constructor for SAML metadata documents.
48
     *
49
     * @param string|null $ID The ID for this document. Defaults to null.
50
     * @param int|null    $validUntil Unix time of validity for this document. Defaults to null.
51
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
52
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to null.
53
     * @param \DOMAttr[] $namespacedAttributes
54
     */
55
    public function __construct(
56
        ?string $ID = null,
57
        ?int $validUntil = null,
58
        ?string $cacheDuration = null,
59
        ?Extensions $extensions = null,
60
        $namespacedAttributes = []
61
    ) {
62
        $this->setID($ID);
63
        $this->setValidUntil($validUntil);
64
        $this->setCacheDuration($cacheDuration);
65
        $this->setExtensions($extensions);
66
        $this->setAttributesNS($namespacedAttributes);
67
    }
68
69
70
    /**
71
     * Collect the value of the ID property.
72
     *
73
     * @return string|null
74
     */
75
    public function getID(): ?string
76
    {
77
        return $this->Id;
78
    }
79
80
81
    /**
82
     * Set the value of the ID property.
83
     *
84
     * @param string|null $id
85
     */
86
    protected function setID(?string $id): void
87
    {
88
        $this->Id = $id;
89
    }
90
91
92
    /**
93
     * Collect the value of the validUntil property.
94
     *
95
     * @return int|null
96
     */
97
    public function getValidUntil(): ?int
98
    {
99
        return $this->validUntil;
100
    }
101
102
103
    /**
104
     * Set the value of the validUntil-property
105
     *
106
     * @param int|null $validUntil
107
     */
108
    protected function setValidUntil(?int $validUntil): void
109
    {
110
        $this->validUntil = $validUntil;
111
    }
112
113
114
    /**
115
     * Collect the value of the cacheDuration property.
116
     *
117
     * @return string|null
118
     */
119
    public function getCacheDuration(): ?string
120
    {
121
        return $this->cacheDuration;
122
    }
123
124
125
    /**
126
     * Set the value of the cacheDuration-property
127
     *
128
     * @param string|null $cacheDuration
129
     */
130
    protected function setCacheDuration(?string $cacheDuration): void
131
    {
132
        $this->cacheDuration = $cacheDuration;
133
    }
134
135
136
    /**
137
     * Get the XML element.
138
     *
139
     * @return \DOMElement
140
     */
141
    public function getXML(): DOMElement
142
    {
143
        return $this->xml;
144
    }
145
146
147
    /**
148
     * Set the XML element.
149
     *
150
     * @param \DOMElement $xml
151
     */
152
    protected function setXML(DOMElement $xml): void
153
    {
154
        $this->xml = $xml;
155
    }
156
157
158
    /**
159
     * @inheritDoc
160
     */
161
    protected function getOriginalXML(): DOMElement
162
    {
163
        return $this->xml;
164
    }
165
166
167
    /**
168
     * @param \DOMElement|null $parent
169
     *
170
     * @return \DOMElement
171
     */
172
    public function toXML(DOMElement $parent = null): DOMElement
173
    {
174
        $e = $this->instantiateParentElement($parent);
175
176
        foreach ($this->getAttributesNS() as $attr) {
177
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
178
        }
179
180
        if ($this->Id !== null) {
181
            $e->setAttribute('ID', $this->Id);
182
        }
183
184
        if ($this->validUntil !== null) {
185
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
186
        }
187
188
        if ($this->cacheDuration !== null) {
189
            $e->setAttribute('cacheDuration', $this->cacheDuration);
190
        }
191
192
        if ($this->Extensions !== null && !$this->Extensions->isEmptyElement()) {
193
            $this->Extensions->toXML($e);
194
        }
195
196
        return $e;
197
    }
198
}
199