blob: 4dd16e0ee7461c0328fce83779869a522dfb80aa [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Caster;
13
14use Symfony\Component\VarDumper\Cloner\Stub;
15
16/**
17 * Casts DOM related classes to array representation.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 *
21 * @final
22 */
23class DOMCaster
24{
25 private const ERROR_CODES = [
26 \DOM_PHP_ERR => 'DOM_PHP_ERR',
27 \DOM_INDEX_SIZE_ERR => 'DOM_INDEX_SIZE_ERR',
28 \DOMSTRING_SIZE_ERR => 'DOMSTRING_SIZE_ERR',
29 \DOM_HIERARCHY_REQUEST_ERR => 'DOM_HIERARCHY_REQUEST_ERR',
30 \DOM_WRONG_DOCUMENT_ERR => 'DOM_WRONG_DOCUMENT_ERR',
31 \DOM_INVALID_CHARACTER_ERR => 'DOM_INVALID_CHARACTER_ERR',
32 \DOM_NO_DATA_ALLOWED_ERR => 'DOM_NO_DATA_ALLOWED_ERR',
33 \DOM_NO_MODIFICATION_ALLOWED_ERR => 'DOM_NO_MODIFICATION_ALLOWED_ERR',
34 \DOM_NOT_FOUND_ERR => 'DOM_NOT_FOUND_ERR',
35 \DOM_NOT_SUPPORTED_ERR => 'DOM_NOT_SUPPORTED_ERR',
36 \DOM_INUSE_ATTRIBUTE_ERR => 'DOM_INUSE_ATTRIBUTE_ERR',
37 \DOM_INVALID_STATE_ERR => 'DOM_INVALID_STATE_ERR',
38 \DOM_SYNTAX_ERR => 'DOM_SYNTAX_ERR',
39 \DOM_INVALID_MODIFICATION_ERR => 'DOM_INVALID_MODIFICATION_ERR',
40 \DOM_NAMESPACE_ERR => 'DOM_NAMESPACE_ERR',
41 \DOM_INVALID_ACCESS_ERR => 'DOM_INVALID_ACCESS_ERR',
42 \DOM_VALIDATION_ERR => 'DOM_VALIDATION_ERR',
43 ];
44
45 private const NODE_TYPES = [
46 \XML_ELEMENT_NODE => 'XML_ELEMENT_NODE',
47 \XML_ATTRIBUTE_NODE => 'XML_ATTRIBUTE_NODE',
48 \XML_TEXT_NODE => 'XML_TEXT_NODE',
49 \XML_CDATA_SECTION_NODE => 'XML_CDATA_SECTION_NODE',
50 \XML_ENTITY_REF_NODE => 'XML_ENTITY_REF_NODE',
51 \XML_ENTITY_NODE => 'XML_ENTITY_NODE',
52 \XML_PI_NODE => 'XML_PI_NODE',
53 \XML_COMMENT_NODE => 'XML_COMMENT_NODE',
54 \XML_DOCUMENT_NODE => 'XML_DOCUMENT_NODE',
55 \XML_DOCUMENT_TYPE_NODE => 'XML_DOCUMENT_TYPE_NODE',
56 \XML_DOCUMENT_FRAG_NODE => 'XML_DOCUMENT_FRAG_NODE',
57 \XML_NOTATION_NODE => 'XML_NOTATION_NODE',
58 \XML_HTML_DOCUMENT_NODE => 'XML_HTML_DOCUMENT_NODE',
59 \XML_DTD_NODE => 'XML_DTD_NODE',
60 \XML_ELEMENT_DECL_NODE => 'XML_ELEMENT_DECL_NODE',
61 \XML_ATTRIBUTE_DECL_NODE => 'XML_ATTRIBUTE_DECL_NODE',
62 \XML_ENTITY_DECL_NODE => 'XML_ENTITY_DECL_NODE',
63 \XML_NAMESPACE_DECL_NODE => 'XML_NAMESPACE_DECL_NODE',
64 ];
65
66 public static function castException(\DOMException $e, array $a, Stub $stub, bool $isNested)
67 {
68 $k = Caster::PREFIX_PROTECTED.'code';
69 if (isset($a[$k], self::ERROR_CODES[$a[$k]])) {
70 $a[$k] = new ConstStub(self::ERROR_CODES[$a[$k]], $a[$k]);
71 }
72
73 return $a;
74 }
75
76 public static function castLength($dom, array $a, Stub $stub, bool $isNested)
77 {
78 $a += [
79 'length' => $dom->length,
80 ];
81
82 return $a;
83 }
84
85 public static function castImplementation(\DOMImplementation $dom, array $a, Stub $stub, bool $isNested)
86 {
87 $a += [
88 Caster::PREFIX_VIRTUAL.'Core' => '1.0',
89 Caster::PREFIX_VIRTUAL.'XML' => '2.0',
90 ];
91
92 return $a;
93 }
94
95 public static function castNode(\DOMNode $dom, array $a, Stub $stub, bool $isNested)
96 {
97 $a += [
98 'nodeName' => $dom->nodeName,
99 'nodeValue' => new CutStub($dom->nodeValue),
100 'nodeType' => new ConstStub(self::NODE_TYPES[$dom->nodeType], $dom->nodeType),
101 'parentNode' => new CutStub($dom->parentNode),
102 'childNodes' => $dom->childNodes,
103 'firstChild' => new CutStub($dom->firstChild),
104 'lastChild' => new CutStub($dom->lastChild),
105 'previousSibling' => new CutStub($dom->previousSibling),
106 'nextSibling' => new CutStub($dom->nextSibling),
107 'attributes' => $dom->attributes,
108 'ownerDocument' => new CutStub($dom->ownerDocument),
109 'namespaceURI' => $dom->namespaceURI,
110 'prefix' => $dom->prefix,
111 'localName' => $dom->localName,
112 'baseURI' => $dom->baseURI ? new LinkStub($dom->baseURI) : $dom->baseURI,
113 'textContent' => new CutStub($dom->textContent),
114 ];
115
116 return $a;
117 }
118
119 public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, bool $isNested)
120 {
121 $a += [
122 'nodeName' => $dom->nodeName,
123 'nodeValue' => new CutStub($dom->nodeValue),
124 'nodeType' => new ConstStub(self::NODE_TYPES[$dom->nodeType], $dom->nodeType),
125 'prefix' => $dom->prefix,
126 'localName' => $dom->localName,
127 'namespaceURI' => $dom->namespaceURI,
128 'ownerDocument' => new CutStub($dom->ownerDocument),
129 'parentNode' => new CutStub($dom->parentNode),
130 ];
131
132 return $a;
133 }
134
135 public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0)
136 {
137 $a += [
138 'doctype' => $dom->doctype,
139 'implementation' => $dom->implementation,
140 'documentElement' => new CutStub($dom->documentElement),
141 'actualEncoding' => $dom->actualEncoding,
142 'encoding' => $dom->encoding,
143 'xmlEncoding' => $dom->xmlEncoding,
144 'standalone' => $dom->standalone,
145 'xmlStandalone' => $dom->xmlStandalone,
146 'version' => $dom->version,
147 'xmlVersion' => $dom->xmlVersion,
148 'strictErrorChecking' => $dom->strictErrorChecking,
149 'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
150 'config' => $dom->config,
151 'formatOutput' => $dom->formatOutput,
152 'validateOnParse' => $dom->validateOnParse,
153 'resolveExternals' => $dom->resolveExternals,
154 'preserveWhiteSpace' => $dom->preserveWhiteSpace,
155 'recover' => $dom->recover,
156 'substituteEntities' => $dom->substituteEntities,
157 ];
158
159 if (!($filter & Caster::EXCLUDE_VERBOSE)) {
160 $formatOutput = $dom->formatOutput;
161 $dom->formatOutput = true;
162 $a += [Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML()];
163 $dom->formatOutput = $formatOutput;
164 }
165
166 return $a;
167 }
168
169 public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub $stub, bool $isNested)
170 {
171 $a += [
172 'data' => $dom->data,
173 'length' => $dom->length,
174 ];
175
176 return $a;
177 }
178
179 public static function castAttr(\DOMAttr $dom, array $a, Stub $stub, bool $isNested)
180 {
181 $a += [
182 'name' => $dom->name,
183 'specified' => $dom->specified,
184 'value' => $dom->value,
185 'ownerElement' => $dom->ownerElement,
186 'schemaTypeInfo' => $dom->schemaTypeInfo,
187 ];
188
189 return $a;
190 }
191
192 public static function castElement(\DOMElement $dom, array $a, Stub $stub, bool $isNested)
193 {
194 $a += [
195 'tagName' => $dom->tagName,
196 'schemaTypeInfo' => $dom->schemaTypeInfo,
197 ];
198
199 return $a;
200 }
201
202 public static function castText(\DOMText $dom, array $a, Stub $stub, bool $isNested)
203 {
204 $a += [
205 'wholeText' => $dom->wholeText,
206 ];
207
208 return $a;
209 }
210
211 public static function castTypeinfo(\DOMTypeinfo $dom, array $a, Stub $stub, bool $isNested)
212 {
213 $a += [
214 'typeName' => $dom->typeName,
215 'typeNamespace' => $dom->typeNamespace,
216 ];
217
218 return $a;
219 }
220
221 public static function castDomError(\DOMDomError $dom, array $a, Stub $stub, bool $isNested)
222 {
223 $a += [
224 'severity' => $dom->severity,
225 'message' => $dom->message,
226 'type' => $dom->type,
227 'relatedException' => $dom->relatedException,
228 'related_data' => $dom->related_data,
229 'location' => $dom->location,
230 ];
231
232 return $a;
233 }
234
235 public static function castLocator(\DOMLocator $dom, array $a, Stub $stub, bool $isNested)
236 {
237 $a += [
238 'lineNumber' => $dom->lineNumber,
239 'columnNumber' => $dom->columnNumber,
240 'offset' => $dom->offset,
241 'relatedNode' => $dom->relatedNode,
242 'uri' => $dom->uri ? new LinkStub($dom->uri, $dom->lineNumber) : $dom->uri,
243 ];
244
245 return $a;
246 }
247
248 public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $stub, bool $isNested)
249 {
250 $a += [
251 'name' => $dom->name,
252 'entities' => $dom->entities,
253 'notations' => $dom->notations,
254 'publicId' => $dom->publicId,
255 'systemId' => $dom->systemId,
256 'internalSubset' => $dom->internalSubset,
257 ];
258
259 return $a;
260 }
261
262 public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, bool $isNested)
263 {
264 $a += [
265 'publicId' => $dom->publicId,
266 'systemId' => $dom->systemId,
267 ];
268
269 return $a;
270 }
271
272 public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, bool $isNested)
273 {
274 $a += [
275 'publicId' => $dom->publicId,
276 'systemId' => $dom->systemId,
277 'notationName' => $dom->notationName,
278 'actualEncoding' => $dom->actualEncoding,
279 'encoding' => $dom->encoding,
280 'version' => $dom->version,
281 ];
282
283 return $a;
284 }
285
286 public static function castProcessingInstruction(\DOMProcessingInstruction $dom, array $a, Stub $stub, bool $isNested)
287 {
288 $a += [
289 'target' => $dom->target,
290 'data' => $dom->data,
291 ];
292
293 return $a;
294 }
295
296 public static function castXPath(\DOMXPath $dom, array $a, Stub $stub, bool $isNested)
297 {
298 $a += [
299 'document' => $dom->document,
300 ];
301
302 return $a;
303 }
304}