XmlReaderCaster.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 XmlReader class to array representation.
  14. *
  15. * @author Baptiste Clavié <clavie.b@gmail.com>
  16. *
  17. * @final
  18. */
  19. class XmlReaderCaster
  20. {
  21. private const NODE_TYPES = [
  22. \XMLReader::NONE => 'NONE',
  23. \XMLReader::ELEMENT => 'ELEMENT',
  24. \XMLReader::ATTRIBUTE => 'ATTRIBUTE',
  25. \XMLReader::TEXT => 'TEXT',
  26. \XMLReader::CDATA => 'CDATA',
  27. \XMLReader::ENTITY_REF => 'ENTITY_REF',
  28. \XMLReader::ENTITY => 'ENTITY',
  29. \XMLReader::PI => 'PI (Processing Instruction)',
  30. \XMLReader::COMMENT => 'COMMENT',
  31. \XMLReader::DOC => 'DOC',
  32. \XMLReader::DOC_TYPE => 'DOC_TYPE',
  33. \XMLReader::DOC_FRAGMENT => 'DOC_FRAGMENT',
  34. \XMLReader::NOTATION => 'NOTATION',
  35. \XMLReader::WHITESPACE => 'WHITESPACE',
  36. \XMLReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE',
  37. \XMLReader::END_ELEMENT => 'END_ELEMENT',
  38. \XMLReader::END_ENTITY => 'END_ENTITY',
  39. \XMLReader::XML_DECLARATION => 'XML_DECLARATION',
  40. ];
  41. public static function castXmlReader(\XMLReader $reader, array $a, Stub $stub, bool $isNested)
  42. {
  43. $props = Caster::PREFIX_VIRTUAL.'parserProperties';
  44. $info = [
  45. 'localName' => $reader->localName,
  46. 'prefix' => $reader->prefix,
  47. 'nodeType' => new ConstStub(self::NODE_TYPES[$reader->nodeType], $reader->nodeType),
  48. 'depth' => $reader->depth,
  49. 'isDefault' => $reader->isDefault,
  50. 'isEmptyElement' => \XMLReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
  51. 'xmlLang' => $reader->xmlLang,
  52. 'attributeCount' => $reader->attributeCount,
  53. 'value' => $reader->value,
  54. 'namespaceURI' => $reader->namespaceURI,
  55. 'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
  56. $props => [
  57. 'LOADDTD' => $reader->getParserProperty(\XMLReader::LOADDTD),
  58. 'DEFAULTATTRS' => $reader->getParserProperty(\XMLReader::DEFAULTATTRS),
  59. 'VALIDATE' => $reader->getParserProperty(\XMLReader::VALIDATE),
  60. 'SUBST_ENTITIES' => $reader->getParserProperty(\XMLReader::SUBST_ENTITIES),
  61. ],
  62. ];
  63. if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, [], $count)) {
  64. $info[$props] = new EnumStub($info[$props]);
  65. $info[$props]->cut = $count;
  66. }
  67. $info = Caster::filter($info, Caster::EXCLUDE_EMPTY, [], $count);
  68. // +2 because hasValue and hasAttributes are always filtered
  69. $stub->cut += $count + 2;
  70. return $a + $info;
  71. }
  72. }