1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace SimpleSAML\XML; |
||||||
6 | |||||||
7 | use DOMElement; |
||||||
8 | use SimpleSAML\XML\Assert\Assert; |
||||||
9 | use SimpleSAML\XML\DOMDocumentFactory; |
||||||
10 | use SimpleSAML\XML\SerializableElementTrait; |
||||||
11 | use SimpleSAML\XMLSchema\Exception\MissingAttributeException; |
||||||
12 | use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
||||||
13 | use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface; |
||||||
14 | use SimpleSAML\XMLSchema\Type\StringValue; |
||||||
15 | |||||||
16 | /** |
||||||
17 | * Serializable class used to hold an XML element. |
||||||
18 | * |
||||||
19 | * @package simplesamlphp/xml-common |
||||||
20 | */ |
||||||
21 | final class Chunk implements SerializableElementInterface |
||||||
22 | { |
||||||
23 | use SerializableElementTrait; |
||||||
24 | |||||||
25 | |||||||
26 | /** |
||||||
27 | * The localName of the element. |
||||||
28 | * |
||||||
29 | * @var string |
||||||
30 | */ |
||||||
31 | protected string $localName; |
||||||
32 | |||||||
33 | /** |
||||||
34 | * The namespaceURI of this element. |
||||||
35 | * |
||||||
36 | * @var string|null |
||||||
37 | */ |
||||||
38 | protected ?string $namespaceURI; |
||||||
39 | |||||||
40 | /** |
||||||
41 | * The prefix of this element. |
||||||
42 | * |
||||||
43 | * @var string |
||||||
44 | */ |
||||||
45 | protected string $prefix; |
||||||
46 | |||||||
47 | |||||||
48 | /** |
||||||
49 | * Create an XML Chunk from a copy of the given \DOMElement. |
||||||
50 | * |
||||||
51 | * @param \DOMElement $xml The element we should copy. |
||||||
52 | */ |
||||||
53 | public function __construct( |
||||||
54 | protected DOMElement $xml, |
||||||
55 | ) { |
||||||
56 | $this->setLocalName($xml->localName); |
||||||
57 | $this->setNamespaceURI($xml->namespaceURI); |
||||||
58 | $this->setPrefix($xml->prefix); |
||||||
59 | } |
||||||
60 | |||||||
61 | |||||||
62 | /** |
||||||
63 | * Collect the value of the localName-property |
||||||
64 | * |
||||||
65 | * @return string |
||||||
66 | */ |
||||||
67 | public function getLocalName(): string |
||||||
68 | { |
||||||
69 | return $this->localName; |
||||||
70 | } |
||||||
71 | |||||||
72 | |||||||
73 | /** |
||||||
74 | * Set the value of the localName-property |
||||||
75 | * |
||||||
76 | * @param string $localName |
||||||
77 | * @throws \SimpleSAML\Assert\AssertionFailedException if $localName is an empty string |
||||||
78 | */ |
||||||
79 | public function setLocalName(string $localName): void |
||||||
80 | { |
||||||
81 | Assert::validNCName($localName, SchemaViolationException::class); // Covers the empty string |
||||||
82 | $this->localName = $localName; |
||||||
83 | } |
||||||
84 | |||||||
85 | |||||||
86 | /** |
||||||
87 | * Collect the value of the namespaceURI-property |
||||||
88 | * |
||||||
89 | * @return string|null |
||||||
90 | */ |
||||||
91 | public function getNamespaceURI(): ?string |
||||||
92 | { |
||||||
93 | return $this->namespaceURI; |
||||||
94 | } |
||||||
95 | |||||||
96 | |||||||
97 | /** |
||||||
98 | * Set the value of the namespaceURI-property |
||||||
99 | * |
||||||
100 | * @param string|null $namespaceURI |
||||||
101 | */ |
||||||
102 | protected function setNamespaceURI(?string $namespaceURI = null): void |
||||||
103 | { |
||||||
104 | Assert::nullOrValidURI($namespaceURI, SchemaViolationException::class); |
||||||
105 | $this->namespaceURI = $namespaceURI; |
||||||
106 | } |
||||||
107 | |||||||
108 | |||||||
109 | /** |
||||||
110 | * Get this \DOMElement. |
||||||
111 | * |
||||||
112 | * @return \DOMElement This element. |
||||||
113 | */ |
||||||
114 | public function getXML(): DOMElement |
||||||
115 | { |
||||||
116 | return $this->xml; |
||||||
117 | } |
||||||
118 | |||||||
119 | |||||||
120 | /** |
||||||
121 | * Collect the value of the prefix-property |
||||||
122 | * |
||||||
123 | * @return string |
||||||
124 | */ |
||||||
125 | public function getPrefix(): string |
||||||
126 | { |
||||||
127 | return $this->prefix; |
||||||
128 | } |
||||||
129 | |||||||
130 | |||||||
131 | /** |
||||||
132 | * Set the value of the prefix-property |
||||||
133 | * |
||||||
134 | * @param string|null $prefix |
||||||
135 | */ |
||||||
136 | protected function setPrefix(?string $prefix = null): void |
||||||
137 | { |
||||||
138 | $this->prefix = strval($prefix); |
||||||
139 | } |
||||||
140 | |||||||
141 | |||||||
142 | /** |
||||||
143 | * Get the XML qualified name (prefix:name, or just name when not prefixed) |
||||||
144 | * of the element represented by this class. |
||||||
145 | * |
||||||
146 | * @return string |
||||||
147 | */ |
||||||
148 | public function getQualifiedName(): string |
||||||
149 | { |
||||||
150 | $prefix = $this->getPrefix(); |
||||||
151 | |||||||
152 | if (empty($prefix)) { |
||||||
153 | return $this->getLocalName(); |
||||||
154 | } else { |
||||||
155 | return $prefix . ':' . $this->getLocalName(); |
||||||
156 | } |
||||||
157 | } |
||||||
158 | |||||||
159 | |||||||
160 | /** |
||||||
161 | * Get the value of an attribute from a given element. |
||||||
162 | * |
||||||
163 | * @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface |
||||||
164 | * @param \DOMElement $xml The element where we should search for the attribute. |
||||||
165 | * @param string $name The name of the attribute. |
||||||
166 | * @param class-string<T> $type The type of the attribute value. |
||||||
167 | * @return T |
||||||
168 | * |
||||||
169 | * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException if the attribute is missing from the element |
||||||
170 | */ |
||||||
171 | public static function getAttribute( |
||||||
172 | DOMElement $xml, |
||||||
173 | string $name, |
||||||
174 | string $type = StringValue::class, |
||||||
175 | ): ValueTypeInterface { |
||||||
176 | Assert::isAOf($type, ValueTypeInterface::class); |
||||||
177 | |||||||
178 | Assert::true( |
||||||
179 | $xml->hasAttribute($name), |
||||||
180 | 'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.', |
||||||
181 | MissingAttributeException::class, |
||||||
182 | ); |
||||||
183 | |||||||
184 | $value = $xml->getAttribute($name); |
||||||
185 | return $type::fromString($value); |
||||||
186 | } |
||||||
187 | |||||||
188 | |||||||
189 | /** |
||||||
190 | * Get the value of an attribute from a given element. |
||||||
191 | * |
||||||
192 | * @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface |
||||||
193 | * @param \DOMElement $xml The element where we should search for the attribute. |
||||||
194 | * @param string $name The name of the attribute. |
||||||
195 | * @param class-string<T> $type The type of the attribute value. |
||||||
196 | * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface|null $default |
||||||
197 | * The default to return in case the attribute does not exist and it is optional. |
||||||
198 | * @return ($default is \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface ? T : T|null) |
||||||
199 | */ |
||||||
200 | public static function getOptionalAttribute( |
||||||
201 | DOMElement $xml, |
||||||
202 | string $name, |
||||||
203 | string $type = StringValue::class, |
||||||
204 | ?ValueTypeInterface $default = null, |
||||||
205 | ): ?ValueTypeInterface { |
||||||
206 | if (!$xml->hasAttribute($name)) { |
||||||
207 | return $default; |
||||||
208 | } |
||||||
209 | |||||||
210 | return self::getAttribute($xml, $name, $type); |
||||||
211 | } |
||||||
212 | |||||||
213 | |||||||
214 | /** |
||||||
215 | * Test if an object, at the state it's in, would produce an empty XML-element |
||||||
216 | * |
||||||
217 | * @return bool |
||||||
218 | */ |
||||||
219 | public function isEmptyElement(): bool |
||||||
220 | { |
||||||
221 | /** @var \DOMElement $xml */ |
||||||
222 | $xml = $this->getXML(); |
||||||
223 | return ($xml->childNodes->length === 0) && ($xml->attributes->count() === 0); |
||||||
0 ignored issues
–
show
|
|||||||
224 | } |
||||||
225 | |||||||
226 | |||||||
227 | /** |
||||||
228 | * @param \DOMElement $xml |
||||||
229 | * @return static |
||||||
230 | */ |
||||||
231 | public static function fromXML(DOMElement $xml): static |
||||||
232 | { |
||||||
233 | return new static($xml); |
||||||
234 | } |
||||||
235 | |||||||
236 | |||||||
237 | /** |
||||||
238 | * Append this XML element to a different XML element. |
||||||
239 | * |
||||||
240 | * @param \DOMElement|null $parent The element we should append this element to. |
||||||
241 | * @return \DOMElement The new element. |
||||||
242 | */ |
||||||
243 | public function toXML(?DOMElement $parent = null): DOMElement |
||||||
244 | { |
||||||
245 | if ($parent === null) { |
||||||
246 | $doc = DOMDocumentFactory::create(); |
||||||
247 | } else { |
||||||
248 | $doc = $parent->ownerDocument; |
||||||
249 | Assert::notNull($doc); |
||||||
250 | } |
||||||
251 | |||||||
252 | if ($parent === null) { |
||||||
253 | $parent = $doc; |
||||||
254 | } |
||||||
255 | |||||||
256 | $parent->appendChild($doc->importNode($this->getXML(), true)); |
||||||
0 ignored issues
–
show
The method
appendChild() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
257 | |||||||
258 | return $doc->documentElement; |
||||||
259 | } |
||||||
260 | } |
||||||
261 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.