Skip to content

[VarDumper] Add support for adding more default casters to AbstractCloner::addDefaultCasters() #60480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarDumper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add support for adding more default casters to `AbstractCloner::addDefaultCasters()`

7.3
---

Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ public function __construct(?array $casters = null)
* Maps resources or objects types to a callback.
* Types are in the key, with a callable caster for value.
* Resource types are to be prefixed with a `:`,
* see e.g. static::$defaultCasters.
* see e.g. self::$defaultCasters.
*
* @param callable[] $casters A map of casters
* @param array<string, callable> $casters A map of casters
*/
public function addCasters(array $casters): void
{
Expand All @@ -265,6 +265,21 @@ public function addCasters(array $casters): void
}
}

/**
* Adds default casters for resources and objects.
*
* Maps resources or objects types to a callback.
* Types are in the key, with a callable caster for value.
* Resource types are to be prefixed with a `:`,
* see e.g. self::$defaultCasters.
*
* @param array<string, callable> $casters A map of casters
*/
public static function addDefaultCasters(array $casters): void
{
self::$defaultCasters = [...self::$defaultCasters, ...$casters];
}

/**
* Sets the maximum number of items to clone past the minimum depth in nested structures.
*/
Expand Down
64 changes: 63 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
namespace Symfony\Component\VarDumper\Tests\Cloner;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\DateCaster;
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Tests\Fixtures\Php74;
use Symfony\Component\VarDumper\Tests\Fixtures\Php81Enums;

Expand All @@ -21,6 +25,64 @@
*/
class VarClonerTest extends TestCase
{
public function testAddCaster()
{
$o1 = new class() {
public string $p1 = 'p1';
};
$o2 = new class() {
public string $p2 = 'p2';
};

AbstractCloner::addDefaultCasters([
$o1::class => function ($obj, $array) {
$array['p1'] = 123;

return $array;
},
// Test we can override the default casters
\DateTimeInterface::class => function (\DateTimeInterface $obj, $array, Stub $stub, bool $isNested, int $filter) {
$array = DateCaster::castDateTime($obj, $array, $stub, $isNested, $filter);
$array['foo'] = 'bar';

return $array;
},
]);
$cloner = new VarCloner();
$cloner->addCasters([
$o2::class => function ($obj, $array) {
$array['p2'] = 456;

return $array;
},
]);

$dumper = new CliDumper('php://output');
$dumper->setColors(false);

ob_start();
$dumper->dump($cloner->cloneVar([$o1, $o2, new \DateTime('Mon Jan 4 15:26:20 2010 +0100')]));
$out = ob_get_clean();
$out = preg_replace('/[ \t]+$/m', '', $out);
$this->assertStringMatchesFormat(
<<<EOTXT
array:3 [
0 => class@anonymous {#%d
+p1: 123
}
1 => class@anonymous {#%d
+p2: 456
}
2 => DateTime @1262615180 {#%d
date: 2010-01-04 15:26:20.0 +01:00
+foo: "bar"
}
]
EOTXT,
$out
);
}

public function testMaxIntBoundary()
{
$data = [\PHP_INT_MAX => 123];
Expand Down Expand Up @@ -427,7 +489,7 @@ public function testCaster()
[attr] => Array
(
[file] => %a%eVarClonerTest.php
[line] => 22
[line] => 26
)

)
Expand Down
Loading