1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Chunk; |
10
|
|
|
use SimpleSAML\XML\Constants; |
11
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
12
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
13
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class representing a ds:Object element. |
17
|
|
|
* |
18
|
|
|
* @package simplesamlphp/xml-security |
19
|
|
|
*/ |
20
|
|
|
final class DsObject extends AbstractDsElement |
21
|
|
|
{ |
22
|
|
|
use ExtendableElementTrait; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public const LOCALNAME = 'Object'; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
public const NAMESPACE = Constants::XS_ANY_NS_ANY; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The Id. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected ?string $Id; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The MIME type. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
protected ?string $MimeType; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The encoding. |
46
|
|
|
* |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
protected ?string $Encoding; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize a ds:Object element. |
54
|
|
|
* |
55
|
|
|
* @param string|null $Id |
56
|
|
|
* @param string|null $MimeType |
57
|
|
|
* @param string|null $Encoding |
58
|
|
|
* @param \SimpleSAML\XML\XMLElementInterface[] $elements |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
?string $Id = null, |
62
|
|
|
?string $MimeType = null, |
63
|
|
|
?string $Encoding = null, |
64
|
|
|
array $elements = [] |
65
|
|
|
) { |
66
|
|
|
$this->setId($Id); |
67
|
|
|
$this->setMimeType($MimeType); |
68
|
|
|
$this->setEncoding($Encoding); |
69
|
|
|
$this->setElements($elements); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Collect the value of the Id-property |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function getId(): ?string |
79
|
|
|
{ |
80
|
|
|
return $this->Id; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the value of the Id-property |
86
|
|
|
* |
87
|
|
|
* @param string $Id |
88
|
|
|
*/ |
89
|
|
|
private function setId(?string $Id): void |
90
|
|
|
{ |
91
|
|
|
Assert::nullOrValidNCName($Id); |
92
|
|
|
$this->Id = $Id; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Collect the value of the MimeType-property |
98
|
|
|
* |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
public function getMimeType(): ?string |
102
|
|
|
{ |
103
|
|
|
return $this->MimeType; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the value of the MimeType-property |
109
|
|
|
* |
110
|
|
|
* @param string|null $MimeType |
111
|
|
|
*/ |
112
|
|
|
private function setMimeType(?string $MimeType): void |
113
|
|
|
{ |
114
|
|
|
$this->MimeType = $MimeType; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Collect the value of the Encoding-property |
120
|
|
|
* |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public function getEncoding(): ?string |
124
|
|
|
{ |
125
|
|
|
return $this->Encoding; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set the value of the Encoding-property |
131
|
|
|
* |
132
|
|
|
* @param string|null $Encoding |
133
|
|
|
*/ |
134
|
|
|
private function setEncoding(?string $Encoding): void |
135
|
|
|
{ |
136
|
|
|
Assert::nullOrValidURI($Encoding); |
137
|
|
|
$this->Encoding = $Encoding; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Test if an object, at the state it's in, would produce an empty XML-element |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isEmptyElement(): bool |
147
|
|
|
{ |
148
|
|
|
return ( |
149
|
|
|
empty($this->elements) |
150
|
|
|
&& empty($this->Id) |
151
|
|
|
&& empty($this->MimeType) |
152
|
|
|
&& empty($this->Encoding) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Convert XML into a ds:Object |
159
|
|
|
* |
160
|
|
|
* @param \DOMElement $xml The XML element we should load |
161
|
|
|
* @return self |
162
|
|
|
* |
163
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
164
|
|
|
* If the qualified name of the supplied element is wrong |
165
|
|
|
*/ |
166
|
|
|
public static function fromXML(DOMElement $xml): object |
167
|
|
|
{ |
168
|
|
|
Assert::same($xml->localName, 'Object', InvalidDOMElementException::class); |
169
|
|
|
Assert::same($xml->namespaceURI, DsObject::NS, InvalidDOMElementException::class); |
170
|
|
|
|
171
|
|
|
$Id = DsObject::getAttribute($xml, 'Id'); |
172
|
|
|
$MimeType = DsObject::getAttribute($xml, 'MimeType'); |
173
|
|
|
$Encoding = DsObject::getAttribute($xml, 'Encoding'); |
174
|
|
|
|
175
|
|
|
$elements = []; |
176
|
|
|
foreach ($xml->childNodes as $elt) { |
177
|
|
|
if (!($elt instanceof DOMElement)) { |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$elements[] = new Chunk($elt); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return new self($Id, $MimeType, $Encoding, $elements); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Convert this ds:Object element to XML. |
190
|
|
|
* |
191
|
|
|
* @param \DOMElement|null $parent The element we should append this ds:Object element to. |
192
|
|
|
* @return \DOMElement |
193
|
|
|
*/ |
194
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
195
|
|
|
{ |
196
|
|
|
$e = $this->instantiateParentElement($parent); |
197
|
|
|
|
198
|
|
|
if ($this->Id !== null) { |
199
|
|
|
$e->setAttribute('Id', $this->Id); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if ($this->MimeType !== null) { |
203
|
|
|
$e->setAttribute('MimeType', $this->MimeType); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($this->Encoding !== null) { |
207
|
|
|
$e->setAttribute('Encoding', $this->Encoding); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
foreach ($this->elements as $elt) { |
211
|
|
|
$elt->toXML($e); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $e; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|