XmlReaderCaster.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @internal since Symfony 7.3
  20. */
  21. class XmlReaderCaster
  22. {
  23. private const NODE_TYPES = [
  24. \XMLReader::NONE => 'NONE',
  25. \XMLReader::ELEMENT => 'ELEMENT',
  26. \XMLReader::ATTRIBUTE => 'ATTRIBUTE',
  27. \XMLReader::TEXT => 'TEXT',
  28. \XMLReader::CDATA => 'CDATA',
  29. \XMLReader::ENTITY_REF => 'ENTITY_REF',
  30. \XMLReader::ENTITY => 'ENTITY',
  31. \XMLReader::PI => 'PI (Processing Instruction)',
  32. \XMLReader::COMMENT => 'COMMENT',
  33. \XMLReader::DOC => 'DOC',
  34. \XMLReader::DOC_TYPE => 'DOC_TYPE',
  35. \XMLReader::DOC_FRAGMENT => 'DOC_FRAGMENT',
  36. \XMLReader::NOTATION => 'NOTATION',
  37. \XMLReader::WHITESPACE => 'WHITESPACE',
  38. \XMLReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE',
  39. \XMLReader::END_ELEMENT => 'END_ELEMENT',
  40. \XMLReader::END_ENTITY => 'END_ENTITY',
  41. \XMLReader::XML_DECLARATION => 'XML_DECLARATION',
  42. ];
  43. public static function castXmlReader(\XMLReader $reader, array $a, Stub $stub, bool $isNested): array
  44. {
  45. try {
  46. $properties = [
  47. 'LOADDTD' => @$reader->getParserProperty(\XMLReader::LOADDTD),
  48. 'DEFAULTATTRS' => @$reader->getParserProperty(\XMLReader::DEFAULTATTRS),
  49. 'VALIDATE' => @$reader->getParserProperty(\XMLReader::VALIDATE),
  50. 'SUBST_ENTITIES' => @$reader->getParserProperty(\XMLReader::SUBST_ENTITIES),
  51. ];
  52. } catch (\Error) {
  53. $properties = [
  54. 'LOADDTD' => false,
  55. 'DEFAULTATTRS' => false,
  56. 'VALIDATE' => false,
  57. 'SUBST_ENTITIES' => false,
  58. ];
  59. }
  60. $props = Caster::PREFIX_VIRTUAL.'parserProperties';
  61. $info = [
  62. 'localName' => $reader->localName,
  63. 'prefix' => $reader->prefix,
  64. 'nodeType' => new ConstStub(self::NODE_TYPES[$reader->nodeType], $reader->nodeType),
  65. 'depth' => $reader->depth,
  66. 'isDefault' => $reader->isDefault,
  67. 'isEmptyElement' => \XMLReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
  68. 'xmlLang' => $reader->xmlLang,
  69. 'attributeCount' => $reader->attributeCount,
  70. 'value' => $reader->value,
  71. 'namespaceURI' => $reader->namespaceURI,
  72. 'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
  73. $props => $properties,
  74. ];
  75. if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, [], $count)) {
  76. $info[$props] = new EnumStub($info[$props]);
  77. $info[$props]->cut = $count;
  78. }
  79. $a = Caster::filter($a, Caster::EXCLUDE_UNINITIALIZED, [], $count);
  80. $info = Caster::filter($info, Caster::EXCLUDE_EMPTY, [], $count);
  81. // +2 because hasValue and hasAttributes are always filtered
  82. $stub->cut += $count + 2;
  83. return $a + $info;
  84. }
  85. }