Skip to content

Commit 632335f

Browse files
committed
Add support for adding more default castors to AbstractCloner::addDefaultCasters()
1 parent 9ec163b commit 632335f

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Add support for adding more default casters to `AbstractCloner::addDefaultCasters()`
8+
49
7.3
510
---
611

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ public function __construct(?array $casters = null)
254254
* Maps resources or objects types to a callback.
255255
* Types are in the key, with a callable caster for value.
256256
* Resource types are to be prefixed with a `:`,
257-
* see e.g. static::$defaultCasters.
257+
* see e.g. self::$defaultCasters.
258258
*
259-
* @param callable[] $casters A map of casters
259+
* @param array<string, callable> $casters A map of casters
260260
*/
261261
public function addCasters(array $casters): void
262262
{
@@ -265,6 +265,21 @@ public function addCasters(array $casters): void
265265
}
266266
}
267267

268+
/**
269+
* Adds default casters for resources and objects.
270+
*
271+
* Maps resources or objects types to a callback.
272+
* Types are in the key, with a callable caster for value.
273+
* Resource types are to be prefixed with a `:`,
274+
* see e.g. self::$defaultCasters.
275+
*
276+
* @param array<string, callable> $casters A map of casters
277+
*/
278+
public static function addDefaultCasters(array $casters): void
279+
{
280+
self::$defaultCasters = [...self::$defaultCasters, ...$casters];
281+
}
282+
268283
/**
269284
* Sets the maximum number of items to clone past the minimum depth in nested structures.
270285
*/

src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
namespace Symfony\Component\VarDumper\Tests\Cloner;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\DateCaster;
16+
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
17+
use Symfony\Component\VarDumper\Cloner\Stub;
1518
use Symfony\Component\VarDumper\Cloner\VarCloner;
19+
use Symfony\Component\VarDumper\Dumper\CliDumper;
1620
use Symfony\Component\VarDumper\Tests\Fixtures\Php74;
1721
use Symfony\Component\VarDumper\Tests\Fixtures\Php81Enums;
1822

@@ -21,6 +25,64 @@
2125
*/
2226
class VarClonerTest extends TestCase
2327
{
28+
public function testAddCaster()
29+
{
30+
$o1 = new class() {
31+
public string $p1 = 'p1';
32+
};
33+
$o2 = new class() {
34+
public string $p2 = 'p2';
35+
};
36+
37+
AbstractCloner::addDefaultCasters([
38+
$o1::class => function ($obj, $array) {
39+
$array['p1'] = 123;
40+
41+
return $array;
42+
},
43+
// Test we can override the default casters
44+
\DateTimeInterface::class => function (\DateTimeInterface $obj, $array, Stub $stub, bool $isNested, int $filter) {
45+
$array = DateCaster::castDateTime($obj, $array, $stub, $isNested, $filter);
46+
$array['foo'] = 'bar';
47+
48+
return $array;
49+
},
50+
]);
51+
$cloner = new VarCloner();
52+
$cloner->addCasters([
53+
$o2::class => function ($obj, $array) {
54+
$array['p2'] = 456;
55+
56+
return $array;
57+
},
58+
]);
59+
60+
$dumper = new CliDumper('php://output');
61+
$dumper->setColors(false);
62+
63+
ob_start();
64+
$dumper->dump($cloner->cloneVar([$o1, $o2, new \DateTime('Mon Jan 4 15:26:20 2010 +0100')]));
65+
$out = ob_get_clean();
66+
$out = preg_replace('/[ \t]+$/m', '', $out);
67+
$this->assertStringMatchesFormat(
68+
<<<EOTXT
69+
array:3 [
70+
0 => class@anonymous {#%d
71+
+p1: 123
72+
}
73+
1 => class@anonymous {#%d
74+
+p2: 456
75+
}
76+
2 => DateTime @1262615180 {#%d
77+
date: 2010-01-04 15:26:20.0 +01:00
78+
+foo: "bar"
79+
}
80+
]
81+
EOTXT,
82+
$out
83+
);
84+
}
85+
2486
public function testMaxIntBoundary()
2587
{
2688
$data = [\PHP_INT_MAX => 123];
@@ -427,7 +489,7 @@ public function testCaster()
427489
[attr] => Array
428490
(
429491
[file] => %a%eVarClonerTest.php
430-
[line] => 22
492+
[line] => 26
431493
)
432494
433495
)

0 commit comments

Comments
 (0)