SymfonyCaster.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\HttpFoundation\Request;
  12. use Symfony\Component\Uid\TimeBasedUidInterface;
  13. use Symfony\Component\Uid\Ulid;
  14. use Symfony\Component\Uid\Uuid;
  15. use Symfony\Component\VarDumper\Cloner\Stub;
  16. use Symfony\Component\VarExporter\Internal\LazyObjectState;
  17. /**
  18. * @final
  19. *
  20. * @internal
  21. */
  22. class SymfonyCaster
  23. {
  24. private const REQUEST_GETTERS = [
  25. 'pathInfo' => 'getPathInfo',
  26. 'requestUri' => 'getRequestUri',
  27. 'baseUrl' => 'getBaseUrl',
  28. 'basePath' => 'getBasePath',
  29. 'method' => 'getMethod',
  30. 'format' => 'getRequestFormat',
  31. ];
  32. public static function castRequest(Request $request, array $a, Stub $stub, bool $isNested): array
  33. {
  34. $clone = null;
  35. foreach (self::REQUEST_GETTERS as $prop => $getter) {
  36. $key = Caster::PREFIX_PROTECTED.$prop;
  37. if (\array_key_exists($key, $a) && null === $a[$key]) {
  38. $clone ??= clone $request;
  39. $a[Caster::PREFIX_VIRTUAL.$prop] = $clone->{$getter}();
  40. }
  41. }
  42. return $a;
  43. }
  44. public static function castHttpClient($client, array $a, Stub $stub, bool $isNested): array
  45. {
  46. $multiKey = \sprintf("\0%s\0multi", $client::class);
  47. if (isset($a[$multiKey]) && !$a[$multiKey] instanceof Stub) {
  48. $a[$multiKey] = new CutStub($a[$multiKey]);
  49. }
  50. return $a;
  51. }
  52. public static function castHttpClientResponse($response, array $a, Stub $stub, bool $isNested): array
  53. {
  54. $stub->cut += \count($a);
  55. $a = [];
  56. foreach ($response->getInfo() as $k => $v) {
  57. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  58. }
  59. return $a;
  60. }
  61. public static function castLazyObjectState($state, array $a, Stub $stub, bool $isNested): array
  62. {
  63. if (!$isNested) {
  64. return $a;
  65. }
  66. $stub->cut += \count($a) - 1;
  67. $instance = $a['realInstance'] ?? null;
  68. if (isset($a['status'])) { // forward-compat with Symfony 8
  69. $a = ['status' => new ConstStub(match ($a['status']) {
  70. LazyObjectState::STATUS_INITIALIZED_FULL => 'INITIALIZED_FULL',
  71. LazyObjectState::STATUS_INITIALIZED_PARTIAL => 'INITIALIZED_PARTIAL',
  72. LazyObjectState::STATUS_UNINITIALIZED_FULL => 'UNINITIALIZED_FULL',
  73. LazyObjectState::STATUS_UNINITIALIZED_PARTIAL => 'UNINITIALIZED_PARTIAL',
  74. }, $a['status'])];
  75. }
  76. if ($instance) {
  77. $a['realInstance'] = $instance;
  78. --$stub->cut;
  79. }
  80. return $a;
  81. }
  82. public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested): array
  83. {
  84. $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $uuid->toBase58();
  85. $a[Caster::PREFIX_VIRTUAL.'toBase32'] = $uuid->toBase32();
  86. if ($uuid instanceof TimeBasedUidInterface) {
  87. $a[Caster::PREFIX_VIRTUAL.'time'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C');
  88. }
  89. return $a;
  90. }
  91. public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested): array
  92. {
  93. $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $ulid->toBase58();
  94. $a[Caster::PREFIX_VIRTUAL.'toRfc4122'] = $ulid->toRfc4122();
  95. if ($ulid instanceof TimeBasedUidInterface) {
  96. $a[Caster::PREFIX_VIRTUAL.'time'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C');
  97. }
  98. return $a;
  99. }
  100. }