Skip to content

Commit

Permalink
[VarDumper] Allow preserving a subset of cut arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 22, 2015
1 parent 449b18c commit 208ca94
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
30 changes: 30 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/CutArrayStub.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

/**
* Represents a cut array.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CutArrayStub extends CutStub
{
public $preservedSubset;

public function __construct(array $value, array $preservedKeys)
{
parent::__construct($value);

$this->preservedSubset = array_intersect_key($value, array_flip($preservedKeys));
$this->cut -= count($this->preservedSubset);
}
}
13 changes: 1 addition & 12 deletions src/Symfony/Component/VarDumper/Caster/SplCaster.php
Expand Up @@ -160,18 +160,7 @@ public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $
}

if (isset($a[$prefix.'fstat'])) {
$fstat = $a[$prefix.'fstat'];
$fstat = array(
'dev' => $fstat['dev'],
'ino' => $fstat['ino'],
'nlink' => $fstat['nlink'],
'rdev' => $fstat['rdev'],
'blksize' => $fstat['blksize'],
'blocks' => $fstat['blocks'],
'…' => '…'.(count($fstat) - 6),
);

$a[$prefix.'fstat'] = $fstat;
$a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], array('dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks'));
}

return $a;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/StubCaster.php
Expand Up @@ -33,6 +33,11 @@ public static function castStub(Stub $c, array $a, Stub $stub, $isNested)
}
}

public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, $isNested)
{
return $isNested ? $c->preservedSubset : $a;
}

public static function cutInternals($obj, array $a, Stub $stub, $isNested)
{
if ($isNested) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -23,6 +23,7 @@ abstract class AbstractCloner implements ClonerInterface
{
public static $defaultCasters = array(
'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
'Symfony\Component\VarDumper\Caster\CutArrayStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castCutArray',
'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',

'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
Expand Down
Expand Up @@ -106,14 +106,14 @@ public function testCastFileObject()
]
flags: DROP_NEW_LINE|SKIP_EMPTY
maxLineLen: 0
fstat: array:7 [
fstat: array:26 [
"dev" => %d
"ino" => %d
"nlink" => %d
"rdev" => 0
"blksize" => %d
"blocks" => %d
"…" => "…20"
…20
]
eof: false
key: 0
Expand Down

0 comments on commit 208ca94

Please sign in to comment.