From 632335fe2fc7768ed11c57d5d8ed16a7761b1878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 19 May 2025 16:40:10 +0200 Subject: [PATCH] Add support for adding more default castors to `AbstractCloner::addDefaultCasters()` --- src/Symfony/Component/VarDumper/CHANGELOG.md | 5 ++ .../VarDumper/Cloner/AbstractCloner.php | 19 +++++- .../VarDumper/Tests/Cloner/VarClonerTest.php | 64 ++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/VarDumper/CHANGELOG.md b/src/Symfony/Component/VarDumper/CHANGELOG.md index bb63a98547184..b35a762412bad 100644 --- a/src/Symfony/Component/VarDumper/CHANGELOG.md +++ b/src/Symfony/Component/VarDumper/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Add support for adding more default casters to `AbstractCloner::addDefaultCasters()` + 7.3 --- diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 9038d2c04e8a5..28ade8329d825 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -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 $casters A map of casters */ public function addCasters(array $casters): void { @@ -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 $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. */ diff --git a/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php b/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php index f678385891d03..f6dfefdd67e6d 100644 --- a/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php @@ -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; @@ -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( + << 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]; @@ -427,7 +489,7 @@ public function testCaster() [attr] => Array ( [file] => %a%eVarClonerTest.php - [line] => 22 + [line] => 26 ) )