DsCaster.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Ds\Collection;
  12. use Ds\Map;
  13. use Ds\Pair;
  14. use Symfony\Component\VarDumper\Cloner\Stub;
  15. /**
  16. * Casts Ds extension classes to array representation.
  17. *
  18. * @author Jáchym Toušek <enumag@gmail.com>
  19. */
  20. class DsCaster
  21. {
  22. public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array
  23. {
  24. $a[Caster::PREFIX_VIRTUAL.'count'] = $c->count();
  25. $a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity();
  26. if (!$c instanceof Map) {
  27. $a += $c->toArray();
  28. }
  29. return $a;
  30. }
  31. public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array
  32. {
  33. foreach ($c as $k => $v) {
  34. $a[] = new DsPairStub($k, $v);
  35. }
  36. return $a;
  37. }
  38. public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array
  39. {
  40. foreach ($c->toArray() as $k => $v) {
  41. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  42. }
  43. return $a;
  44. }
  45. public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array
  46. {
  47. if ($isNested) {
  48. $stub->class = Pair::class;
  49. $stub->value = null;
  50. $stub->handle = 0;
  51. $a = $c->value;
  52. }
  53. return $a;
  54. }
  55. }