SplCaster.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 SPL related classes to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class SplCaster
  18. {
  19. public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
  20. {
  21. $prefix = Caster::PREFIX_VIRTUAL;
  22. $class = $stub->class;
  23. $flags = $c->getFlags();
  24. $b = array(
  25. $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  26. $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  27. $prefix.'iteratorClass' => $c->getIteratorClass(),
  28. $prefix.'storage' => $c->getArrayCopy(),
  29. );
  30. if ($class === 'ArrayObject') {
  31. $a = $b;
  32. } else {
  33. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  34. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  35. $a = Caster::castObject($c, new \ReflectionClass($class));
  36. $c->setFlags($flags);
  37. }
  38. $a += $b;
  39. }
  40. return $a;
  41. }
  42. public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
  43. {
  44. $a += array(
  45. Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
  46. );
  47. return $a;
  48. }
  49. public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
  50. {
  51. $prefix = Caster::PREFIX_VIRTUAL;
  52. $mode = $c->getIteratorMode();
  53. $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
  54. $a += array(
  55. $prefix.'mode' => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
  56. $prefix.'dllist' => iterator_to_array($c),
  57. );
  58. $c->setIteratorMode($mode);
  59. return $a;
  60. }
  61. public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
  62. {
  63. $a += array(
  64. Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
  65. );
  66. return $a;
  67. }
  68. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
  69. {
  70. $storage = array();
  71. unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
  72. foreach ($c as $obj) {
  73. $storage[spl_object_hash($obj)] = array(
  74. 'object' => $obj,
  75. 'info' => $c->getInfo(),
  76. );
  77. }
  78. $a += array(
  79. Caster::PREFIX_VIRTUAL.'storage' => $storage,
  80. );
  81. return $a;
  82. }
  83. }