You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.7 KiB

2 years ago
  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. /**
  16. * @final
  17. */
  18. class SymfonyCaster
  19. {
  20. private const REQUEST_GETTERS = [
  21. 'pathInfo' => 'getPathInfo',
  22. 'requestUri' => 'getRequestUri',
  23. 'baseUrl' => 'getBaseUrl',
  24. 'basePath' => 'getBasePath',
  25. 'method' => 'getMethod',
  26. 'format' => 'getRequestFormat',
  27. ];
  28. public static function castRequest(Request $request, array $a, Stub $stub, bool $isNested)
  29. {
  30. $clone = null;
  31. foreach (self::REQUEST_GETTERS as $prop => $getter) {
  32. $key = Caster::PREFIX_PROTECTED.$prop;
  33. if (\array_key_exists($key, $a) && null === $a[$key]) {
  34. if (null === $clone) {
  35. $clone = clone $request;
  36. }
  37. $a[Caster::PREFIX_VIRTUAL.$prop] = $clone->{$getter}();
  38. }
  39. }
  40. return $a;
  41. }
  42. public static function castHttpClient($client, array $a, Stub $stub, bool $isNested)
  43. {
  44. $multiKey = sprintf("\0%s\0multi", \get_class($client));
  45. if (isset($a[$multiKey])) {
  46. $a[$multiKey] = new CutStub($a[$multiKey]);
  47. }
  48. return $a;
  49. }
  50. public static function castHttpClientResponse($response, array $a, Stub $stub, bool $isNested)
  51. {
  52. $stub->cut += \count($a);
  53. $a = [];
  54. foreach ($response->getInfo() as $k => $v) {
  55. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  56. }
  57. return $a;
  58. }
  59. public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested)
  60. {
  61. $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $uuid->toBase58();
  62. $a[Caster::PREFIX_VIRTUAL.'toBase32'] = $uuid->toBase32();
  63. // symfony/uid >= 5.3
  64. if (method_exists($uuid, 'getDateTime')) {
  65. $a[Caster::PREFIX_VIRTUAL.'time'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C');
  66. }
  67. return $a;
  68. }
  69. public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested)
  70. {
  71. $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $ulid->toBase58();
  72. $a[Caster::PREFIX_VIRTUAL.'toRfc4122'] = $ulid->toRfc4122();
  73. // symfony/uid >= 5.3
  74. if (method_exists($ulid, 'getDateTime')) {
  75. $a[Caster::PREFIX_VIRTUAL.'time'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C');
  76. }
  77. return $a;
  78. }
  79. }