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.

69 lines
1.8 KiB

3 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\VarDumper\Cloner\Stub;
  13. /**
  14. * @final
  15. */
  16. class SymfonyCaster
  17. {
  18. private const REQUEST_GETTERS = [
  19. 'pathInfo' => 'getPathInfo',
  20. 'requestUri' => 'getRequestUri',
  21. 'baseUrl' => 'getBaseUrl',
  22. 'basePath' => 'getBasePath',
  23. 'method' => 'getMethod',
  24. 'format' => 'getRequestFormat',
  25. ];
  26. public static function castRequest(Request $request, array $a, Stub $stub, bool $isNested)
  27. {
  28. $clone = null;
  29. foreach (self::REQUEST_GETTERS as $prop => $getter) {
  30. $key = Caster::PREFIX_PROTECTED.$prop;
  31. if (\array_key_exists($key, $a) && null === $a[$key]) {
  32. if (null === $clone) {
  33. $clone = clone $request;
  34. }
  35. $a[Caster::PREFIX_VIRTUAL.$prop] = $clone->{$getter}();
  36. }
  37. }
  38. return $a;
  39. }
  40. public static function castHttpClient($client, array $a, Stub $stub, bool $isNested)
  41. {
  42. $multiKey = sprintf("\0%s\0multi", \get_class($client));
  43. if (isset($a[$multiKey])) {
  44. $a[$multiKey] = new CutStub($a[$multiKey]);
  45. }
  46. return $a;
  47. }
  48. public static function castHttpClientResponse($response, array $a, Stub $stub, bool $isNested)
  49. {
  50. $stub->cut += \count($a);
  51. $a = [];
  52. foreach ($response->getInfo() as $k => $v) {
  53. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  54. }
  55. return $a;
  56. }
  57. }