SymfonyCaster.php 3.5 KB

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