暂无描述

DsCaster.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * @final since Symfony 4.4
  21. */
  22. class DsCaster
  23. {
  24. public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array
  25. {
  26. $a[Caster::PREFIX_VIRTUAL.'count'] = $c->count();
  27. $a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity();
  28. if (!$c instanceof Map) {
  29. $a += $c->toArray();
  30. }
  31. return $a;
  32. }
  33. public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array
  34. {
  35. foreach ($c as $k => $v) {
  36. $a[] = new DsPairStub($k, $v);
  37. }
  38. return $a;
  39. }
  40. public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array
  41. {
  42. foreach ($c->toArray() as $k => $v) {
  43. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  44. }
  45. return $a;
  46. }
  47. public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array
  48. {
  49. if ($isNested) {
  50. $stub->class = Pair::class;
  51. $stub->value = null;
  52. $stub->handle = 0;
  53. $a = $c->value;
  54. }
  55. return $a;
  56. }
  57. }