Skip to content

Commit 03859f3

Browse files
Merge branch '7.4' into 8.0
* 7.4: - [Console] Table counts wrong column width when using colspan and `setColumnMaxWidth()` [Console] Table counts wrong number of padding symbols in `renderCell()` method when cell contain unicode variant selector [Cache] Fix using a `ChainAdapter` as an adapter for a pool [SecurityBundle] Don't break `security.http_utils` service decoration [Serializer] Fix collect_denormalization_errors flag in defaultContext [TypeInfo] Fix handling `ConstFetchNode` [VarDumper] Avoid deprecated call in PgSqlCaster [DependencyInjection] Add argument `$target` to `ContainerBuilder::registerAliasForArgument()` [BrowserKit] Add `isFirstPage()` and `isLastPage()` methods to History Fix command option mode (InputOption::VALUE_REQUIRED) use an EOL-agnostic approach to parse class uses [Uid] Add microsecond precision to UUIDv7 and optimize on x64 [Uid] Improve entropy of the increment for UUIDv7 [FrameworkBundle] Add `ControllerHelper`; the helpers from AbstractController as a standalone service Add support for adding more default castors to `AbstractCloner::addDefaultCasters()` [HttpKernel] Fix `#[MapUploadedFile]` handling for optional file uploads
2 parents 77079f4 + db6726c commit 03859f3

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

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

Caster/PgSqlCaster.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\VarDumper\Cloner\Stub;
1515

1616
/**
17-
* Casts pqsql resources to array representation.
17+
* Casts pgsql resources to array representation.
1818
*
1919
* @author Nicolas Grekas <[email protected]>
2020
*
@@ -135,9 +135,9 @@ public static function castResult($result, array $a, Stub $stub, bool $isNested)
135135
'name' => pg_field_name($result, $i),
136136
'table' => \sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)),
137137
'type' => \sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)),
138-
'nullable' => (bool) pg_field_is_null($result, $i),
138+
'nullable' => (bool) pg_field_is_null($result, null, $i),
139139
'storage' => pg_field_size($result, $i).' bytes',
140-
'display' => pg_field_prtlen($result, $i).' chars',
140+
'display' => pg_field_prtlen($result, null, $i).' chars',
141141
];
142142
if (' (OID: )' === $field['table']) {
143143
$field['table'] = null;

Cloner/AbstractCloner.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ public function __construct(?array $casters = null)
247247
/**
248248
* Adds casters for resources and objects.
249249
*
250-
* Maps resources or objects types to a callback.
251-
* Types are in the key, with a callable caster for value.
252-
* Resource types are to be prefixed with a `:`,
253-
* see e.g. static::$defaultCasters.
250+
* Maps resources or object types to a callback.
251+
* Use types as keys and callable casters as values.
252+
* Prefix types with `::`,
253+
* see e.g. self::$defaultCasters.
254254
*
255-
* @param callable[] $casters A map of casters
255+
* @param array<string, callable> $casters A map of casters
256256
*/
257257
public function addCasters(array $casters): void
258258
{
@@ -261,6 +261,21 @@ public function addCasters(array $casters): void
261261
}
262262
}
263263

264+
/**
265+
* Adds default casters for resources and objects.
266+
*
267+
* Maps resources or object types to a callback.
268+
* Use types as keys and callable casters as values.
269+
* Prefix types with `::`,
270+
* see e.g. self::$defaultCasters.
271+
*
272+
* @param array<string, callable> $casters A map of casters
273+
*/
274+
public static function addDefaultCasters(array $casters): void
275+
{
276+
self::$defaultCasters = [...self::$defaultCasters, ...$casters];
277+
}
278+
264279
/**
265280
* Sets the maximum number of items to clone past the minimum depth in nested structures.
266281
*/

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)