You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.2 KiB

2 years ago
  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. try {
  44. $properties = [
  45. 'LOADDTD' => @$reader->getParserProperty(\XMLReader::LOADDTD),
  46. 'DEFAULTATTRS' => @$reader->getParserProperty(\XMLReader::DEFAULTATTRS),
  47. 'VALIDATE' => @$reader->getParserProperty(\XMLReader::VALIDATE),
  48. 'SUBST_ENTITIES' => @$reader->getParserProperty(\XMLReader::SUBST_ENTITIES),
  49. ];
  50. } catch (\Error $e) {
  51. $properties = [
  52. 'LOADDTD' => false,
  53. 'DEFAULTATTRS' => false,
  54. 'VALIDATE' => false,
  55. 'SUBST_ENTITIES' => false,
  56. ];
  57. }
  58. $props = Caster::PREFIX_VIRTUAL.'parserProperties';
  59. $info = [
  60. 'localName' => $reader->localName,
  61. 'prefix' => $reader->prefix,
  62. 'nodeType' => new ConstStub(self::NODE_TYPES[$reader->nodeType], $reader->nodeType),
  63. 'depth' => $reader->depth,
  64. 'isDefault' => $reader->isDefault,
  65. 'isEmptyElement' => \XMLReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
  66. 'xmlLang' => $reader->xmlLang,
  67. 'attributeCount' => $reader->attributeCount,
  68. 'value' => $reader->value,
  69. 'namespaceURI' => $reader->namespaceURI,
  70. 'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
  71. $props => $properties,
  72. ];
  73. if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, [], $count)) {
  74. $info[$props] = new EnumStub($info[$props]);
  75. $info[$props]->cut = $count;
  76. }
  77. $info = Caster::filter($info, Caster::EXCLUDE_EMPTY, [], $count);
  78. // +2 because hasValue and hasAttributes are always filtered
  79. $stub->cut += $count + 2;
  80. return $a + $info;
  81. }
  82. }