SymfonyCaster.php 3.7 KB

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