DOMCaster.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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
  18. */
  19. class DOMCaster
  20. {
  21. private const ERROR_CODES = [
  22. 0 => '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. /**
  61. * @return array
  62. */
  63. public static function castException(\DOMException $e, array $a, Stub $stub, bool $isNested)
  64. {
  65. $k = Caster::PREFIX_PROTECTED.'code';
  66. if (isset($a[$k], self::ERROR_CODES[$a[$k]])) {
  67. $a[$k] = new ConstStub(self::ERROR_CODES[$a[$k]], $a[$k]);
  68. }
  69. return $a;
  70. }
  71. /**
  72. * @return array
  73. */
  74. public static function castLength($dom, array $a, Stub $stub, bool $isNested)
  75. {
  76. $a += [
  77. 'length' => $dom->length,
  78. ];
  79. return $a;
  80. }
  81. /**
  82. * @return array
  83. */
  84. public static function castImplementation(\DOMImplementation $dom, array $a, Stub $stub, bool $isNested)
  85. {
  86. $a += [
  87. Caster::PREFIX_VIRTUAL.'Core' => '1.0',
  88. Caster::PREFIX_VIRTUAL.'XML' => '2.0',
  89. ];
  90. return $a;
  91. }
  92. /**
  93. * @return array
  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. return $a;
  116. }
  117. /**
  118. * @return array
  119. */
  120. public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, bool $isNested)
  121. {
  122. $a += [
  123. 'nodeName' => $dom->nodeName,
  124. 'nodeValue' => new CutStub($dom->nodeValue),
  125. 'nodeType' => new ConstStub(self::NODE_TYPES[$dom->nodeType], $dom->nodeType),
  126. 'prefix' => $dom->prefix,
  127. 'localName' => $dom->localName,
  128. 'namespaceURI' => $dom->namespaceURI,
  129. 'ownerDocument' => new CutStub($dom->ownerDocument),
  130. 'parentNode' => new CutStub($dom->parentNode),
  131. ];
  132. return $a;
  133. }
  134. /**
  135. * @return array
  136. */
  137. public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0)
  138. {
  139. $a += [
  140. 'doctype' => $dom->doctype,
  141. 'implementation' => $dom->implementation,
  142. 'documentElement' => new CutStub($dom->documentElement),
  143. 'encoding' => $dom->encoding,
  144. 'xmlEncoding' => $dom->xmlEncoding,
  145. 'xmlStandalone' => $dom->xmlStandalone,
  146. 'xmlVersion' => $dom->xmlVersion,
  147. 'strictErrorChecking' => $dom->strictErrorChecking,
  148. 'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
  149. 'formatOutput' => $dom->formatOutput,
  150. 'validateOnParse' => $dom->validateOnParse,
  151. 'resolveExternals' => $dom->resolveExternals,
  152. 'preserveWhiteSpace' => $dom->preserveWhiteSpace,
  153. 'recover' => $dom->recover,
  154. 'substituteEntities' => $dom->substituteEntities,
  155. ];
  156. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  157. $formatOutput = $dom->formatOutput;
  158. $dom->formatOutput = true;
  159. $a += [Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML()];
  160. $dom->formatOutput = $formatOutput;
  161. }
  162. return $a;
  163. }
  164. /**
  165. * @return array
  166. */
  167. public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub $stub, bool $isNested)
  168. {
  169. $a += [
  170. 'data' => $dom->data,
  171. 'length' => $dom->length,
  172. ];
  173. return $a;
  174. }
  175. /**
  176. * @return array
  177. */
  178. public static function castAttr(\DOMAttr $dom, array $a, Stub $stub, bool $isNested)
  179. {
  180. $a += [
  181. 'name' => $dom->name,
  182. 'specified' => $dom->specified,
  183. 'value' => $dom->value,
  184. 'ownerElement' => $dom->ownerElement,
  185. 'schemaTypeInfo' => $dom->schemaTypeInfo,
  186. ];
  187. return $a;
  188. }
  189. /**
  190. * @return array
  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. return $a;
  199. }
  200. /**
  201. * @return array
  202. */
  203. public static function castText(\DOMText $dom, array $a, Stub $stub, bool $isNested)
  204. {
  205. $a += [
  206. 'wholeText' => $dom->wholeText,
  207. ];
  208. return $a;
  209. }
  210. /**
  211. * @return array
  212. */
  213. public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $stub, bool $isNested)
  214. {
  215. $a += [
  216. 'name' => $dom->name,
  217. 'entities' => $dom->entities,
  218. 'notations' => $dom->notations,
  219. 'publicId' => $dom->publicId,
  220. 'systemId' => $dom->systemId,
  221. 'internalSubset' => $dom->internalSubset,
  222. ];
  223. return $a;
  224. }
  225. /**
  226. * @return array
  227. */
  228. public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, bool $isNested)
  229. {
  230. $a += [
  231. 'publicId' => $dom->publicId,
  232. 'systemId' => $dom->systemId,
  233. ];
  234. return $a;
  235. }
  236. /**
  237. * @return array
  238. */
  239. public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, bool $isNested)
  240. {
  241. $a += [
  242. 'publicId' => $dom->publicId,
  243. 'systemId' => $dom->systemId,
  244. 'notationName' => $dom->notationName,
  245. ];
  246. return $a;
  247. }
  248. /**
  249. * @return array
  250. */
  251. public static function castProcessingInstruction(\DOMProcessingInstruction $dom, array $a, Stub $stub, bool $isNested)
  252. {
  253. $a += [
  254. 'target' => $dom->target,
  255. 'data' => $dom->data,
  256. ];
  257. return $a;
  258. }
  259. /**
  260. * @return array
  261. */
  262. public static function castXPath(\DOMXPath $dom, array $a, Stub $stub, bool $isNested)
  263. {
  264. $a += [
  265. 'document' => $dom->document,
  266. ];
  267. return $a;
  268. }
  269. }