DOMCaster.php 9.8 KB

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