No Description

DOMCaster.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (isset($a["\0*\0code"], self::$errorCodes[$a["\0*\0code"]])) {
  61. $a["\0*\0code"] = new ConstStub(self::$errorCodes[$a["\0*\0code"]], $a["\0*\0code"]);
  62. }
  63. return $a;
  64. }
  65. public static function castLength($dom, array $a, Stub $stub, $isNested)
  66. {
  67. $a += array(
  68. 'length' => $dom->length,
  69. );
  70. return $a;
  71. }
  72. public static function castImplementation($dom, array $a, Stub $stub, $isNested)
  73. {
  74. $a += array(
  75. "\0~\0Core" => '1.0',
  76. "\0~\0XML" => '2.0',
  77. );
  78. return $a;
  79. }
  80. public static function castNode(\DOMNode $dom, array $a, Stub $stub, $isNested)
  81. {
  82. $a += array(
  83. 'nodeName' => $dom->nodeName,
  84. 'nodeValue' => new CutStub($dom->nodeValue),
  85. 'nodeType' => new ConstStub(self::$nodeTypes[$dom->nodeType], $dom->nodeType),
  86. 'parentNode' => new CutStub($dom->parentNode),
  87. 'childNodes' => $dom->childNodes,
  88. 'firstChild' => new CutStub($dom->firstChild),
  89. 'lastChild' => new CutStub($dom->lastChild),
  90. 'previousSibling' => new CutStub($dom->previousSibling),
  91. 'nextSibling' => new CutStub($dom->nextSibling),
  92. 'attributes' => $dom->attributes,
  93. 'ownerDocument' => new CutStub($dom->ownerDocument),
  94. 'namespaceURI' => $dom->namespaceURI,
  95. 'prefix' => $dom->prefix,
  96. 'localName' => $dom->localName,
  97. 'baseURI' => $dom->baseURI,
  98. 'textContent' => new CutStub($dom->textContent),
  99. );
  100. return $a;
  101. }
  102. public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, $isNested)
  103. {
  104. $a += array(
  105. 'nodeName' => $dom->nodeName,
  106. 'nodeValue' => new CutStub($dom->nodeValue),
  107. 'nodeType' => new ConstStub(self::$nodeTypes[$dom->nodeType], $dom->nodeType),
  108. 'prefix' => $dom->prefix,
  109. 'localName' => $dom->localName,
  110. 'namespaceURI' => $dom->namespaceURI,
  111. 'ownerDocument' => new CutStub($dom->ownerDocument),
  112. 'parentNode' => new CutStub($dom->parentNode),
  113. );
  114. return $a;
  115. }
  116. public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested)
  117. {
  118. $formatOutput = $dom->formatOutput;
  119. $dom->formatOutput = true;
  120. $a += array(
  121. 'doctype' => $dom->doctype,
  122. 'implementation' => $dom->implementation,
  123. 'documentElement' => new CutStub($dom->documentElement),
  124. 'actualEncoding' => $dom->actualEncoding,
  125. 'encoding' => $dom->encoding,
  126. 'xmlEncoding' => $dom->xmlEncoding,
  127. 'standalone' => $dom->standalone,
  128. 'xmlStandalone' => $dom->xmlStandalone,
  129. 'version' => $dom->version,
  130. 'xmlVersion' => $dom->xmlVersion,
  131. 'strictErrorChecking' => $dom->strictErrorChecking,
  132. 'documentURI' => $dom->documentURI,
  133. 'config' => $dom->config,
  134. 'formatOutput' => $formatOutput,
  135. 'validateOnParse' => $dom->validateOnParse,
  136. 'resolveExternals' => $dom->resolveExternals,
  137. 'preserveWhiteSpace' => $dom->preserveWhiteSpace,
  138. 'recover' => $dom->recover,
  139. 'substituteEntities' => $dom->substituteEntities,
  140. "\0~\0xml" => $dom->saveXML(),
  141. );
  142. $dom->formatOutput = $formatOutput;
  143. return $a;
  144. }
  145. public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub $stub, $isNested)
  146. {
  147. $a += array(
  148. 'data' => $dom->data,
  149. 'length' => $dom->length,
  150. );
  151. return $a;
  152. }
  153. public static function castAttr(\DOMAttr $dom, array $a, Stub $stub, $isNested)
  154. {
  155. $a += array(
  156. 'name' => $dom->name,
  157. 'specified' => $dom->specified,
  158. 'value' => $dom->value,
  159. 'ownerElement' => $dom->ownerElement,
  160. 'schemaTypeInfo' => $dom->schemaTypeInfo,
  161. );
  162. return $a;
  163. }
  164. public static function castElement(\DOMElement $dom, array $a, Stub $stub, $isNested)
  165. {
  166. $a += array(
  167. 'tagName' => $dom->tagName,
  168. 'schemaTypeInfo' => $dom->schemaTypeInfo,
  169. );
  170. return $a;
  171. }
  172. public static function castText(\DOMText $dom, array $a, Stub $stub, $isNested)
  173. {
  174. $a += array(
  175. 'wholeText' => $dom->wholeText,
  176. );
  177. return $a;
  178. }
  179. public static function castTypeinfo(\DOMTypeinfo $dom, array $a, Stub $stub, $isNested)
  180. {
  181. $a += array(
  182. 'typeName' => $dom->typeName,
  183. 'typeNamespace' => $dom->typeNamespace,
  184. );
  185. return $a;
  186. }
  187. public static function castDomError(\DOMDomError $dom, array $a, Stub $stub, $isNested)
  188. {
  189. $a += array(
  190. 'severity' => $dom->severity,
  191. 'message' => $dom->message,
  192. 'type' => $dom->type,
  193. 'relatedException' => $dom->relatedException,
  194. 'related_data' => $dom->related_data,
  195. 'location' => $dom->location,
  196. );
  197. return $a;
  198. }
  199. public static function castLocator(\DOMLocator $dom, array $a, Stub $stub, $isNested)
  200. {
  201. $a += array(
  202. 'lineNumber' => $dom->lineNumber,
  203. 'columnNumber' => $dom->columnNumber,
  204. 'offset' => $dom->offset,
  205. 'relatedNode' => $dom->relatedNode,
  206. 'uri' => $dom->uri,
  207. );
  208. return $a;
  209. }
  210. public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $stub, $isNested)
  211. {
  212. $a += array(
  213. 'name' => $dom->name,
  214. 'entities' => $dom->entities,
  215. 'notations' => $dom->notations,
  216. 'publicId' => $dom->publicId,
  217. 'systemId' => $dom->systemId,
  218. 'internalSubset' => $dom->internalSubset,
  219. );
  220. return $a;
  221. }
  222. public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, $isNested)
  223. {
  224. $a += array(
  225. 'publicId' => $dom->publicId,
  226. 'systemId' => $dom->systemId,
  227. );
  228. return $a;
  229. }
  230. public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, $isNested)
  231. {
  232. $a += array(
  233. 'publicId' => $dom->publicId,
  234. 'systemId' => $dom->systemId,
  235. 'notationName' => $dom->notationName,
  236. 'actualEncoding' => $dom->actualEncoding,
  237. 'encoding' => $dom->encoding,
  238. 'version' => $dom->version,
  239. );
  240. return $a;
  241. }
  242. public static function castProcessingInstruction(\DOMProcessingInstruction $dom, array $a, Stub $stub, $isNested)
  243. {
  244. $a += array(
  245. 'target' => $dom->target,
  246. 'data' => $dom->data,
  247. );
  248. return $a;
  249. }
  250. public static function castXPath(\DOMXPath $dom, array $a, Stub $stub, $isNested)
  251. {
  252. $a += array(
  253. 'document' => $dom->document,
  254. );
  255. return $a;
  256. }
  257. }