8000 [VarDumper] fix dumping Ds maps and pairs · symfony/symfony@ee20ef6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee20ef6

Browse files
[VarDumper] fix dumping Ds maps and pairs
1 parent b4f6c34 commit ee20ef6

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

src/Symfony/Component/VarDumper/Caster/DsCaster.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111

1212
namespace Symfony\Component\VarDumper\Caster;
1313

14-
use Ds\Deque;
14+
use Ds\Collection;
1515
use Ds\Map;
16-
use Ds\PriorityQueue;
17-
use Ds\Queue;
18-
use Ds\Set;
19-
use Ds\Stack;
20-
use Ds\Vector;
16+
use Ds\Pair;
2117
use Symfony\Component\VarDumper\Cloner\Stub;
2218

2319
/**
@@ -27,23 +23,48 @@
2723
*/
2824
class DsCaster
2925
{
30-
/**
31-
* @param Set|Deque|Vector|Stack|Queue|PriorityQueue $c
32-
*/
33-
public static function castDs($c, array $a, Stub $stub, bool $isNested): array
26+
public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array
3427
{
35-
$prefix = Caster::PREFIX_VIRTUAL;
36-
$a = $c->toArray();
37-
$a[$prefix.'capacity'] = $c->capacity();
28+
$a[Caster::PREFIX_VIRTUAL.'count'] = $c->count();
29+
$a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity();
30+
31+
if (!$c instanceof Map) {
32+
foreach ($c as $k => $v) {
33+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
34+
}
35+
}
3836

3937
return $a;
4038
}
4139

4240
public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array
4341
{
44-
$prefix = Caster::PREFIX_VIRTUAL;
45-
$a = $c->pairs()->toArray();
46-
$a[$prefix.'capacity'] = $c->capacity();
42+
$i = 0;
43+
foreach ($c as $k => $v) {
44+
$a[Caster::PREFIX_VIRTUAL.$i++] = new DsPairStub($k, $v);
45+
}
46+
47+
return $a;
48+
}
49+
50+
public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array
51+
{
52+
foreach ($c->toArray() as $k => $v) {
53+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
54+
}
55+
56+
return $a;
57+
}
58+
59+
public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array
60+
{
61+
if ($isNested) {
62+
$stub->class = Pair::class;
63+
$stub->value = null;
64+
$stub->handle = 0;
65+
66+
$a = $c->value;
67+
}
4768

4869
return $a;
4970
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class DsPairStub extends Stub
20+
{
21+
public function __construct($key, $value)
22+
{
23+
$this->value = [
24+
Caster::PREFIX_VIRTUAL.'key' => $key,
25+
Caster::PREFIX_VIRTUAL.'value' => $value,
26+
];
27+
}
28+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,10 @@ abstract class AbstractCloner implements ClonerInterface
125125

126126
'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'],
127127

128-
'Ds\Set' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
129-
'Ds\Vector' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
130-
'Ds\Deque' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
131-
'Ds\Stack' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
132-
'Ds\Queue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
133-
'Ds\PriorityQueue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
128+
'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castCollection'],
134129
'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', ' 5DA3 ;castMap'],
130+
'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'],
131+
'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'],
135132

136133
':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'],
137134
':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],

0 commit comments

Comments
 (0)
0