DOMCaster.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * @internal since Symfony 7.3
  20. */
  21. class DOMCaster
  22. {
  23. private const ERROR_CODES = [
  24. 0 => 'DOM_PHP_ERR',
  25. \DOM_INDEX_SIZE_ERR => 'DOM_INDEX_SIZE_ERR',
  26. \DOMSTRING_SIZE_ERR => 'DOMSTRING_SIZE_ERR',
  27. \DOM_HIERARCHY_REQUEST_ERR => 'DOM_HIERARCHY_REQUEST_ERR',
  28. \DOM_WRONG_DOCUMENT_ERR => 'DOM_WRONG_DOCUMENT_ERR',
  29. \DOM_INVALID_CHARACTER_ERR => 'DOM_INVALID_CHARACTER_ERR',
  30. \DOM_NO_DATA_ALLOWED_ERR => 'DOM_NO_DATA_ALLOWED_ERR',
  31. \DOM_NO_MODIFICATION_ALLOWED_ERR => 'DOM_NO_MODIFICATION_ALLOWED_ERR',
  32. \DOM_NOT_FOUND_ERR => 'DOM_NOT_FOUND_ERR',
  33. \DOM_NOT_SUPPORTED_ERR => 'DOM_NOT_SUPPORTED_ERR',
  34. \DOM_INUSE_ATTRIBUTE_ERR => 'DOM_INUSE_ATTRIBUTE_ERR',
  35. \DOM_INVALID_STATE_ERR => 'DOM_INVALID_STATE_ERR',
  36. \DOM_SYNTAX_ERR => 'DOM_SYNTAX_ERR',
  37. \DOM_INVALID_MODIFICATION_ERR => 'DOM_INVALID_MODIFICATION_ERR',
  38. \DOM_NAMESPACE_ERR => 'DOM_NAMESPACE_ERR',
  39. \DOM_INVALID_ACCESS_ERR => 'DOM_INVALID_ACCESS_ERR',
  40. \DOM_VALIDATION_ERR => 'DOM_VALIDATION_ERR',
  41. ];
  42. private const NODE_TYPES = [
  43. \XML_ELEMENT_NODE => 'XML_ELEMENT_NODE',
  44. \XML_ATTRIBUTE_NODE => 'XML_ATTRIBUTE_NODE',
  45. \XML_TEXT_NODE => 'XML_TEXT_NODE',
  46. \XML_CDATA_SECTION_NODE => 'XML_CDATA_SECTION_NODE',
  47. \XML_ENTITY_REF_NODE => 'XML_ENTITY_REF_NODE',
  48. \XML_ENTITY_NODE => 'XML_ENTITY_NODE',
  49. \XML_PI_NODE => 'XML_PI_NODE',
  50. \XML_COMMENT_NODE => 'XML_COMMENT_NODE',
  51. \XML_DOCUMENT_NODE => 'XML_DOCUMENT_NODE',
  52. \XML_DOCUMENT_TYPE_NODE => 'XML_DOCUMENT_TYPE_NODE',
  53. \XML_DOCUMENT_FRAG_NODE => 'XML_DOCUMENT_FRAG_NODE',
  54. \XML_NOTATION_NODE => 'XML_NOTATION_NODE',
  55. \XML_HTML_DOCUMENT_NODE => 'XML_HTML_DOCUMENT_NODE',
  56. \XML_DTD_NODE => 'XML_DTD_NODE',
  57. \XML_ELEMENT_DECL_NODE => 'XML_ELEMENT_DECL_NODE',
  58. \XML_ATTRIBUTE_DECL_NODE => 'XML_ATTRIBUTE_DECL_NODE',
  59. \XML_ENTITY_DECL_NODE => 'XML_ENTITY_DECL_NODE',
  60. \XML_NAMESPACE_DECL_NODE => 'XML_NAMESPACE_DECL_NODE',
  61. ];
  62. public static function castException(\DOMException|\Dom\Exception $e, array $a, Stub $stub, bool $isNested): array
  63. {
  64. $k = Caster::PREFIX_PROTECTED.'code';
  65. if (isset($a[$k], self::ERROR_CODES[$a[$k]])) {
  66. $a[$k] = new ConstStub(self::ERROR_CODES[$a[$k]], $a[$k]);
  67. }
  68. return $a;
  69. }
  70. public static function castLength($dom, array $a, Stub $stub, bool $isNested): array
  71. {
  72. return $a;
  73. }
  74. public static function castImplementation(\DOMImplementation|\Dom\Implementation $dom, array $a, Stub $stub, bool $isNested): array
  75. {
  76. $a += [
  77. Caster::PREFIX_VIRTUAL.'Core' => '1.0',
  78. Caster::PREFIX_VIRTUAL.'XML' => '2.0',
  79. ];
  80. return $a;
  81. }
  82. public static function castNode(\DOMNode|\Dom\Node $dom, array $a, Stub $stub, bool $isNested): array
  83. {
  84. return self::castDom($dom, $a, $stub, $isNested);
  85. }
  86. public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, bool $isNested): array
  87. {
  88. return self::castDom($dom, $a, $stub, $isNested);
  89. }
  90. public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  91. {
  92. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  93. $formatOutput = $dom->formatOutput;
  94. $dom->formatOutput = true;
  95. $a += [Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML()];
  96. $dom->formatOutput = $formatOutput;
  97. }
  98. return $a;
  99. }
  100. public static function castXMLDocument(\Dom\XMLDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  101. {
  102. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  103. $formatOutput = $dom->formatOutput;
  104. $dom->formatOutput = true;
  105. $a += [Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML()];
  106. $dom->formatOutput = $formatOutput;
  107. }
  108. return $a;
  109. }
  110. public static function castHTMLDocument(\Dom\HTMLDocument $dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  111. {
  112. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  113. $a += [Caster::PREFIX_VIRTUAL.'html' => $dom->saveHTML()];
  114. }
  115. return $a;
  116. }
  117. public static function castCharacterData(\DOMCharacterData|\Dom\CharacterData $dom, array $a, Stub $stub, bool $isNested): array
  118. {
  119. return $a;
  120. }
  121. public static function castAttr(\DOMAttr|\Dom\Attr $dom, array $a, Stub $stub, bool $isNested): array
  122. {
  123. return $a;
  124. }
  125. public static function castElement(\DOMElement|\Dom\Element $dom, array $a, Stub $stub, bool $isNested): array
  126. {
  127. return $a;
  128. }
  129. public static function castText(\DOMText|\Dom\Text $dom, array $a, Stub $stub, bool $isNested): array
  130. {
  131. return $a;
  132. }
  133. public static function castDocumentType(\DOMDocumentType|\Dom\DocumentType $dom, array $a, Stub $stub, bool $isNested): array
  134. {
  135. return $a;
  136. }
  137. public static function castNotation(\DOMNotation|\Dom\Notation $dom, array $a, Stub $stub, bool $isNested): array
  138. {
  139. return $a;
  140. }
  141. public static function castEntity(\DOMEntity|\Dom\Entity $dom, array $a, Stub $stub, bool $isNested): array
  142. {
  143. return $a;
  144. }
  145. public static function castProcessingInstruction(\DOMProcessingInstruction|\Dom\ProcessingInstruction $dom, array $a, Stub $stub, bool $isNested): array
  146. {
  147. return $a;
  148. }
  149. public static function castXPath(\DOMXPath|\Dom\XPath $dom, array $a, Stub $stub, bool $isNested): array
  150. {
  151. return self::castDom($dom, $a, $stub, $isNested);
  152. }
  153. public static function castDom($dom, array $a, Stub $stub, bool $isNested, int $filter = 0): array
  154. {
  155. foreach ($a as $k => $v) {
  156. if ('encoding' === $k && $dom instanceof \DOMEntity
  157. || \in_array($k, ['actualEncoding', 'config', 'standalone', 'version'], true)
  158. ) {
  159. continue; // deprecated properties
  160. }
  161. $v = $dom->$k;
  162. $a[$k] = match (true) {
  163. $v instanceof \DOMNode || $v instanceof \Dom\Node => new CutStub($v),
  164. 'nodeType' === $k => new ConstStub(self::NODE_TYPES[$v], $v),
  165. 'baseURI' === $k && $v,
  166. 'documentURI' === $k && $v => new LinkStub($v),
  167. default => $v,
  168. };
  169. }
  170. if ($dom instanceof \IteratorAggregate) {
  171. foreach ($dom as $k => $v) {
  172. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  173. }
  174. }
  175. return $a;
  176. }
  177. }