Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\VarDumper\Caster; |
| 13 | |
| 14 | use Symfony\Component\HttpFoundation\Request; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 15 | use Symfony\Component\Uid\Ulid; |
| 16 | use Symfony\Component\Uid\Uuid; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 17 | use Symfony\Component\VarDumper\Cloner\Stub; |
| 18 | |
| 19 | /** |
| 20 | * @final |
| 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 | |
| 33 | public static function castRequest(Request $request, array $a, Stub $stub, bool $isNested) |
| 34 | { |
| 35 | $clone = null; |
| 36 | |
| 37 | foreach (self::REQUEST_GETTERS as $prop => $getter) { |
| 38 | $key = Caster::PREFIX_PROTECTED.$prop; |
| 39 | if (\array_key_exists($key, $a) && null === $a[$key]) { |
| 40 | if (null === $clone) { |
| 41 | $clone = clone $request; |
| 42 | } |
| 43 | $a[Caster::PREFIX_VIRTUAL.$prop] = $clone->{$getter}(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return $a; |
| 48 | } |
| 49 | |
| 50 | public static function castHttpClient($client, array $a, Stub $stub, bool $isNested) |
| 51 | { |
| 52 | $multiKey = sprintf("\0%s\0multi", \get_class($client)); |
| 53 | if (isset($a[$multiKey])) { |
| 54 | $a[$multiKey] = new CutStub($a[$multiKey]); |
| 55 | } |
| 56 | |
| 57 | return $a; |
| 58 | } |
| 59 | |
| 60 | public static function castHttpClientResponse($response, array $a, Stub $stub, bool $isNested) |
| 61 | { |
| 62 | $stub->cut += \count($a); |
| 63 | $a = []; |
| 64 | |
| 65 | foreach ($response->getInfo() as $k => $v) { |
| 66 | $a[Caster::PREFIX_VIRTUAL.$k] = $v; |
| 67 | } |
| 68 | |
| 69 | return $a; |
| 70 | } |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 71 | |
| 72 | public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested) |
| 73 | { |
| 74 | $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $uuid->toBase58(); |
| 75 | $a[Caster::PREFIX_VIRTUAL.'toBase32'] = $uuid->toBase32(); |
| 76 | |
| 77 | // symfony/uid >= 5.3 |
| 78 | if (method_exists($uuid, 'getDateTime')) { |
| 79 | $a[Caster::PREFIX_VIRTUAL.'time'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C'); |
| 80 | } |
| 81 | |
| 82 | return $a; |
| 83 | } |
| 84 | |
| 85 | public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested) |
| 86 | { |
| 87 | $a[Caster::PREFIX_VIRTUAL.'toBase58'] = $ulid->toBase58(); |
| 88 | $a[Caster::PREFIX_VIRTUAL.'toRfc4122'] = $ulid->toRfc4122(); |
| 89 | |
| 90 | // symfony/uid >= 5.3 |
| 91 | if (method_exists($ulid, 'getDateTime')) { |
| 92 | $a[Caster::PREFIX_VIRTUAL.'time'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C'); |
| 93 | } |
| 94 | |
| 95 | return $a; |
| 96 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 97 | } |