DOMCaster.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. public static function castException(\DOMException|\Dom\Exception $e, array $a, Stub $stub, bool $isNested): array
  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, bool $isNested): array
  69. {
  70. $a += [
  71. 'length' => $dom->length,
  72. ];
  73. return $a;
  74. }
  75. public static function castImplementation(\DOMImplementation|\Dom\Implementation $dom, array $a, Stub $stub, bool $isNested): array
  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\Node $dom, array $a, Stub $stub, bool $isNested): array
  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. 'ownerDocument' => new CutStub($dom->ownerDocument),
  96. 'baseURI' => $dom->baseURI ? new LinkStub($dom->baseURI) : $dom->baseURI,
  97. 'textContent' => new CutStub($dom->textContent),
  98. ];
  99. if ($dom instanceof \DOMNode || $dom instanceof \Dom\Element) {
  100. $a += [
  101. 'attributes' => $dom->attributes,
  102. 'namespaceURI' => $dom->namespaceURI,
  103. 'prefix' => $dom->prefix,
  104. 'localName' => $dom->localName,
  105. ];
  106. }
  107. return $a;
  108. }
  109. public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, bool $isNested): array
  110. {
  111. $a += [
  112. 'nodeName' => $dom->nodeName,
  113. 'nodeValue' => new CutStub($dom->nodeValue),
  114. 'nodeType' => new ConstStub(self::NODE_TYPES[$dom->nodeType], $dom->nodeType),
  115. 'prefix' => $dom->prefix,
  116. 'localName' => $dom->localName,
  117. 'namespaceURI' => $dom->namespaceURI,
  118. 'ownerDocument' => new CutStub($dom->ownerDocument),
  119. 'parentNode' => new CutStub($dom->parentNode),
  120. ];
  121. return $a;
  122. }
  123. public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  124. {
  125. $a += [
  126. 'doctype' => $dom->doctype,
  127. 'implementation' => $dom->implementation,
  128. 'documentElement' => new CutStub($dom->documentElement),
  129. 'encoding' => $dom->encoding,
  130. 'xmlEncoding' => $dom->xmlEncoding,
  131. 'xmlStandalone' => $dom->xmlStandalone,
  132. 'xmlVersion' => $dom->xmlVersion,
  133. 'strictErrorChecking' => $dom->strictErrorChecking,
  134. 'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
  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 castXMLDocument(\Dom\XMLDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  151. {
  152. $a += [
  153. 'doctype' => $dom->doctype,
  154. 'implementation' => $dom->implementation,
  155. 'documentElement' => new CutStub($dom->documentElement),
  156. 'inputEncoding' => $dom->inputEncoding,
  157. 'xmlEncoding' => $dom->xmlEncoding,
  158. 'xmlStandalone' => $dom->xmlStandalone,
  159. 'xmlVersion' => $dom->xmlVersion,
  160. 'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
  161. 'formatOutput' => $dom->formatOutput,
  162. ];
  163. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  164. $formatOutput = $dom->formatOutput;
  165. $dom->formatOutput = true;
  166. $a += [Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML()];
  167. $dom->formatOutput = $formatOutput;
  168. }
  169. return $a;
  170. }
  171. public static function castHTMLDocument(\Dom\HTMLDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  172. {
  173. $a += [
  174. 'doctype' => $dom->doctype,
  175. 'implementation' => $dom->implementation,
  176. 'documentElement' => new CutStub($dom->documentElement),
  177. 'inputEncoding' => $dom->inputEncoding,
  178. 'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
  179. ];
  180. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  181. $a += [Caster::PREFIX_VIRTUAL.'html' => $dom->saveHTML()];
  182. }
  183. return $a;
  184. }
  185. public static function castCharacterData(\DOMCharacterData|\Dom\CharacterData $dom, array $a, Stub $stub, bool $isNested): array
  186. {
  187. $a += [
  188. 'data' => $dom->data,
  189. 'length' => $dom->length,
  190. ];
  191. return $a;
  192. }
  193. public static function castAttr(\DOMAttr|\Dom\Attr $dom, array $a, Stub $stub, bool $isNested): array
  194. {
  195. $a += [
  196. 'name' => $dom->name,
  197. 'specified' => $dom->specified,
  198. 'value' => $dom->value,
  199. 'ownerElement' => $dom->ownerElement,
  200. ];
  201. if ($dom instanceof \DOMAttr) {
  202. $a += [
  203. 'schemaTypeInfo' => $dom->schemaTypeInfo,
  204. ];
  205. }
  206. return $a;
  207. }
  208. public static function castElement(\DOMElement|\Dom\Element $dom, array $a, Stub $stub, bool $isNested): array
  209. {
  210. $a += [
  211. 'tagName' => $dom->tagName,
  212. ];
  213. if ($dom instanceof \DOMElement) {
  214. $a += [
  215. 'schemaTypeInfo' => $dom->schemaTypeInfo,
  216. ];
  217. }
  218. return $a;
  219. }
  220. public static function castText(\DOMText|\Dom\Text $dom, array $a, Stub $stub, bool $isNested): array
  221. {
  222. $a += [
  223. 'wholeText' => $dom->wholeText,
  224. ];
  225. return $a;
  226. }
  227. public static function castDocumentType(\DOMDocumentType|\Dom\DocumentType $dom, array $a, Stub $stub, bool $isNested): array
  228. {
  229. $a += [
  230. 'name' => $dom->name,
  231. 'entities' => $dom->entities,
  232. 'notations' => $dom->notations,
  233. 'publicId' => $dom->publicId,
  234. 'systemId' => $dom->systemId,
  235. 'internalSubset' => $dom->internalSubset,
  236. ];
  237. return $a;
  238. }
  239. public static function castNotation(\DOMNotation|\Dom\Notation $dom, array $a, Stub $stub, bool $isNested): array
  240. {
  241. $a += [
  242. 'publicId' => $dom->publicId,
  243. 'systemId' => $dom->systemId,
  244. ];
  245. return $a;
  246. }
  247. public static function castEntity(\DOMEntity|\Dom\Entity $dom, array $a, Stub $stub, bool $isNested): array
  248. {
  249. $a += [
  250. 'publicId' => $dom->publicId,
  251. 'systemId' => $dom->systemId,
  252. 'notationName' => $dom->notationName,
  253. ];
  254. return $a;
  255. }
  256. public static function castProcessingInstruction(\DOMProcessingInstruction|\Dom\ProcessingInstruction $dom, array $a, Stub $stub, bool $isNested): array
  257. {
  258. $a += [
  259. 'target' => $dom->target,
  260. 'data' => $dom->data,
  261. ];
  262. return $a;
  263. }
  264. public static function castXPath(\DOMXPath|\Dom\XPath $dom, array $a, Stub $stub, bool $isNested): array
  265. {
  266. $a += [
  267. 'document' => $dom->document,
  268. ];
  269. return $a;
  270. }
  271. }