10000 Add support for adding more default castors to `AbstractCloner::addDe… · symfony/symfony@ea86493 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea86493

Browse files
committed
Add support for adding more default castors to AbstractCloner::addDefaultCasters()
1 parent c11519a commit ea86493

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
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 castors to `AbstractCloner::addDefaultCasters()`
8+
49
7.3
510
---
611

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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. static::$defaultCasters.
275+
*
276+
* @param callable[] $casters A map of casters
277+
*/
278+
public static function addDefaultCasters(array $casters): void
279+
{
280+
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: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\VarDumper\Tests\Cloner;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
1516
use Symfony\Component\VarDumper\Cloner\VarCloner;
17+
use Symfony\Component\VarDumper\Dumper\CliDumper;
1618
use Symfony\Component\VarDumper\Tests\Fixtures\Php74;
1719
use Symfony\Component\VarDumper\Tests\Fixtures\Php81Enums;
1820

@@ -21,6 +23,53 @@
2123
*/
2224
class VarClonerTest extends TestCase
2325
{
26+
public function testAddCaster()
27+
{
28+
$o1 = new class() {
29+
public string $p1 = 'p1';
30+
};
31+
$o2 = new class() {
32+
public string $p2 = 'p2';
33+
};
34+
35+
AbstractCloner::addDefaultCasters([
36+
$o1::class => function ($obj, $array) {
37+
$array['p1'] = 123;
38+
39+
return $array;
40+
},
41+
]);
42+
$cloner = new VarCloner();
43+
$cloner->addCasters([
44+
$o2::class => function ($obj, $array) {
45+
$array['p2'] = 456;
46+
47+
return $array;
48+
},
49+
]);
50+
51+
$dumper = new CliDumper('php://output');
52+
$dumper->setColors(false);
53+
54+
ob_start();
55+
$dumper->dump($cloner->cloneVar([$o1, $o2]));
56+
$out = ob_get_clean();
57+
$out = preg_replace('/[ \t]+$/m', '', $out);
58+
$this->assertStringMatchesFormat(
59+
<<<EOTXT
60+
array:2 [
61+
0 => class@anonymous {#%d
62+
+p1: 123
63+
}
64+
1 => class@anonymous {#%d
65+
+p2: 456
66+
}
67+
]
68+
EOTXT,
69+
$out
70+
);
71+
}
72+
2473
public function testMaxIntBoundary()
2574
{
2675
$data = [\PHP_INT_MAX => 123];
@@ -427,7 +476,7 @@ public function testCaster()
427476
[attr] => Array
428477
(
429478
[file] => %a%eVarClonerTest.php
430-
[line] => 22
479+
[line] => 24
431480
)
432481
433482
)

0 commit comments

Comments
 (0)
0