XmlReaderCaster.php 2.9 KB

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