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 Ds\Collection; |
| 15 | use Ds\Map; |
| 16 | use Ds\Pair; |
| 17 | use Symfony\Component\VarDumper\Cloner\Stub; |
| 18 | |
| 19 | /** |
| 20 | * Casts Ds extension classes to array representation. |
| 21 | * |
| 22 | * @author Jáchym Toušek <enumag@gmail.com> |
| 23 | * |
| 24 | * @final |
| 25 | */ |
| 26 | class DsCaster |
| 27 | { |
| 28 | public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array |
| 29 | { |
| 30 | $a[Caster::PREFIX_VIRTUAL.'count'] = $c->count(); |
| 31 | $a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity(); |
| 32 | |
| 33 | if (!$c instanceof Map) { |
| 34 | $a += $c->toArray(); |
| 35 | } |
| 36 | |
| 37 | return $a; |
| 38 | } |
| 39 | |
| 40 | public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array |
| 41 | { |
| 42 | foreach ($c as $k => $v) { |
| 43 | $a[] = new DsPairStub($k, $v); |
| 44 | } |
| 45 | |
| 46 | return $a; |
| 47 | } |
| 48 | |
| 49 | public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array |
| 50 | { |
| 51 | foreach ($c->toArray() as $k => $v) { |
| 52 | $a[Caster::PREFIX_VIRTUAL.$k] = $v; |
| 53 | } |
| 54 | |
| 55 | return $a; |
| 56 | } |
| 57 | |
| 58 | public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array |
| 59 | { |
| 60 | if ($isNested) { |
| 61 | $stub->class = Pair::class; |
| 62 | $stub->value = null; |
| 63 | $stub->handle = 0; |
| 64 | |
| 65 | $a = $c->value; |
| 66 | } |
| 67 | |
| 68 | return $a; |
| 69 | } |
| 70 | } |