No Description

SplCaster.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $class = $stub->class;
  22. $flags = $c->getFlags();
  23. $b = array(
  24. "\0~\0flag::STD_PROP_LIST" => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  25. "\0~\0flag::ARRAY_AS_PROPS" => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  26. "\0~\0iteratorClass" => $c->getIteratorClass(),
  27. "\0~\0storage" => $c->getArrayCopy(),
  28. );
  29. if ($class === 'ArrayObject') {
  30. $a = $b;
  31. } else {
  32. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  33. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  34. if ($a = (array) $c) {
  35. $class = new \ReflectionClass($class);
  36. foreach ($a as $k => $p) {
  37. if (!isset($k[0]) || ("\0" !== $k[0] && !$class->hasProperty($k))) {
  38. unset($a[$k]);
  39. $a["\0+\0".$k] = $p;
  40. }
  41. }
  42. }
  43. $c->setFlags($flags);
  44. }
  45. $a += $b;
  46. }
  47. return $a;
  48. }
  49. public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
  50. {
  51. $a += array(
  52. "\0~\0heap" => iterator_to_array(clone $c),
  53. );
  54. return $a;
  55. }
  56. public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
  57. {
  58. $mode = $c->getIteratorMode();
  59. $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
  60. $a += array(
  61. "\0~\0mode" => 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),
  62. "\0~\0dllist" => iterator_to_array($c),
  63. );
  64. $c->setIteratorMode($mode);
  65. return $a;
  66. }
  67. public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
  68. {
  69. $a += array(
  70. "\0~\0storage" => $c->toArray(),
  71. );
  72. return $a;
  73. }
  74. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
  75. {
  76. $storage = array();
  77. unset($a["\0+\0\0gcdata"]); // Don't hit https://bugs.php.net/65967
  78. foreach ($c as $obj) {
  79. $storage[spl_object_hash($obj)] = array(
  80. 'object' => $obj,
  81. 'info' => $c->getInfo(),
  82. );
  83. }
  84. $a += array(
  85. "\0~\0storage" => $storage,
  86. );
  87. return $a;
  88. }
  89. }