DOMCaster.php 9.8 KB

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