8000 [VarDumper] Add EnumStub for dumping virtual collections with casters · symfony/symfony@8a07117 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a07117

Browse files
[VarDumper] Add EnumStub for dumping virtual collections with casters
1 parent bcb2ff6 commit 8a07117

File tree

7 files changed

+50
-5
lines changed

7 files changed

+50
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
* Represents an enumeration of values.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class EnumStub extends Stub
22+
{
23+
public function __construct(array $values)
24+
{
25+
$this->value = $values;
26+
}
27+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
8282
$a += array(
8383
$prefix.'inTransaction' => method_exists($c, 'inTransaction'),
8484
$prefix.'errorInfo' => $c->errorInfo(),
85-
$prefix.'attributes' => $attr,
85+
$prefix.'attributes' => new EnumStub($attr),
8686
);
8787

8888
if ($a[$prefix.'inTransaction']) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ public static function castResult($result, array $a, Stub $stub, $isNested)
126126
$fields = pg_num_fields($result);
127127

128128
for ($i = 0; $i < $fields; ++$i) {
129-
$field = array(
129+
$field = new EnumStub(array(
130130
'name' => pg_field_name($result, $i),
131131
'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)),
132132
'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)),
133133
'nullable' => (bool) pg_field_is_null($result, $i),
134134
'storage' => pg_field_size($result, $i).' bytes',
135135
'display' => pg_field_prtlen($result, $i).' chars',
136-
);
136+
));
137137
if (' (OID: )' === $field['table']) {
138138
$field['table'] = null;
139139
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,19 @@ public static function cutInternals($obj, array $a, Stub $stub, $isNested)
4848

4949
return $a;
5050
}
51+
52+
public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
53+
{
54+
if ($isNested) {
55+
$stub->class = '';
56+
$stub->handle = 0;
57+
58+
$a = array();
59+
foreach ($c->value as $k => $v) {
60+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
61+
}
62+
}
63+
64+
return $a;
65+
}
5166
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class AbstractCloner implements ClonerInterface
2525
'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
2626
'Symfony\Component\VarDumper\Caster\CutArrayStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castCutArray',
2727
'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
28+
'Symfony\Component\VarDumper\Caster\EnumStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castEnum',
2829

2930
'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
3031
'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass',

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
247247
$class = $this->utf8Encode($class);
248248
}
249249
if (Cursor::HASH_OBJECT === $type) {
250-
$prefix = 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
250+
$prefix = $class && 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
251251
} elseif (Cursor::HASH_RESOURCE === $type) {
252252
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
253253
} else {

src/Symfony/Component/VarDumper/Tests/Caster/PdoCasterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public function testCastPdo()
2929
$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array('PDOStatement', array($pdo)));
3030

3131
$cast = PdoCaster::castPdo($pdo, array(), new Stub(), false);
32-
$attr = $cast["\0~\0attributes"];
3332

33+
$this->assertInstanceOf('Symfony\Component\VarDumper\Caster\EnumStub', $cast["\0~\0attributes"]);
34+
35+
$attr = $cast["\0~\0attributes"] = $cast["\0~\0attributes"]->value;
3436
$this->assertInstanceOf('Symfony\Component\VarDumper\Caster\ConstStub', $attr['CASE']);
3537
$this->assertSame('NATURAL', $attr['CASE']->class);
3638
$this->assertSame('BOTH', $attr['DEFAULT_FETCH_MODE']->class);

0 commit comments

Comments
 (0)
0