8000 [VarDumper] Add casters for Symfony UUIDs and ULIDs · symfony/symfony@1b2a812 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b2a812

Browse files
committed
[VarDumper] Add casters for Symfony UUIDs and ULIDs
1 parent 9ccd0ad commit 1b2a812

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
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+
5.4
5+
---
6+
7+
* Add casters for Symfony's UUIDs and ULIDs
8+
49
5.2.0
510
-----
611

src/Symfony/Component/VarDumper/Caster/SymfonyCaster.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\VarDumper\Caster;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Uid\Ulid;
16+
use Symfony\Component\Uid\Uuid;
1517
use Symfony\Component\VarDumper\Cloner\Stub;
1618

1719
/**
@@ -66,4 +68,30 @@ public static function castHttpClientResponse($response, array $a, Stub $stub, b
6668

6769
return $a;
6870
}
71+
72+
public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested)
73+
{
74+
$a[Caster::PREFIX_VIRTUAL.'base 58'] = $uuid->toBase58();
75+
$a[Caster::PREFIX_VIRTUAL.'base 32'] = $uuid->toBase32();
76+
77+
// symfony/uid >= 5.3
78+
if (method_exists($uuid, 'getDateTime')) {
79+
$a[Caster::PREFIX_VIRTUAL.'timestamp'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u');
80+
}
81+
82+
return $a;
83+
}
84+
85+
public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested)
86+
{
87+
$a[Caster::PREFIX_VIRTUAL.'base 58'] = $ulid->toBase58();
88+
$a[Caster::PREFIX_VIRTUAL.'RFC 4122'] = $ulid->toRfc4122();
89+
90+
// symfony/uid >= 5.3
91+
if (method_exists($ulid, 'getDateTime')) {
92+
$a[Caster::PREFIX_VIRTUAL.'timestamp'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v');
93+
}
94+
95+
return $a;
96+
}
6997
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ abstract class AbstractCloner implements ClonerInterface
8686
'Symfony\Component\HttpClient\Response\CurlResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
8787
'Symfony\Component\HttpClient\Response\NativeResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
8888
'Symfony\Component\HttpFoundation\Request' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castRequest'],
89+
'Symfony\Component\Uid\Ulid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUlid'],
90+
'Symfony\Component\Uid\Uuid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUuid'],
8991
'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castThrowingCasterException'],
9092
'Symfony\Component\VarDumper\Caster\TraceStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castTraceStub'],
9193
'Symfony\Component\VarDumper\Caster\FrameStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFrameStub'],
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Ulid;
16+
use Symfony\Component\Uid\UuidV4;
17+
use Symfony\Component\Uid\UuidV6;
18+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
19+
20+
final class SymfonyCasterTest extends TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
public function testCastUuid()
25+
{
26+
$uuid = new UuidV4('83a9db35-3c8c-4040-b3c1-02eccc00b419');
27+
$expectedDump = <<<EODUMP
28+
Symfony\Component\Uid\UuidV4 {
29+
#uid: "83a9db35-3c8c-4040-b3c1-02eccc00b419"
30+
base 58: "HFzAAuYvev42cCjwqpnKqz"
31+
base 32: "43N7DKAF4C810B7G82XK601D0S"
32+
}
33+
EODUMP;
34+
$this->assertDumpEquals($expectedDump, $uuid);
35+
36+
$uuid = new UuidV6('1ebc50e9-8a23-6704-ad6f-59afd5cda7e5');
37+
if (method_exists($uuid, 'getDateTime')) {
38+
$expectedDump = <<<EODUMP
39+
Symfony\Component\Uid\UuidV6 {
40+
#uid: "1ebc50e9-8a23-6704-ad6f-59afd5cda7e5"
41+
base 58: "4o8c5m6v4L8h5teww36JDa"
42+
base 32: "0YQH8EK2H3CW2ATVTSNZAWV9Z5"
43+
timestamp: "2021-06-04 08:26:44.591386"
44+
}
45+
EODUMP;
46+
} else {
47+
$expectedDump = <<<EODUMP
48+
Symfony\Component\Uid\UuidV6 {
49+
#uid: "1ebc50e9-8a23-6704-ad6f-59afd5cda7e5"
50+
base 58: "4o8c5m6v4L8h5teww36JDa"
51+
base 32: "0YQH8EK2H3CW2ATVTSNZAWV9Z5"
52+
}
53+
EODUMP;
54+
}
55+
56+
$this->assertDumpEquals($expectedDump, $uuid);
57+
}
58+
59+
public function testCastUlid()
60+
{
61+
$ulid = new Ulid('01F7B252SZQGTSQGYSGACASAW6');
62+
if (method_exists($ulid, 'getDateTime')) {
63+
$expectedDump = <<<EODUMP
64+
Symfony\Component\Uid\Ulid {
65+
#uid: "01F7B252SZQGTSQGYSGACASAW6"
66+
base 58: "1Ba6pJPFWDwghSKFVvfQ1B"
67+
RFC 4122: "0179d622-8b3f-bc35-9bc3-d98298acab86"
68+
timestamp: "2021-06-04 08:27:38.687"
69+
}
70+
EODUMP;
71+
} else {
72+
$expectedDump = <<<EODUMP
73+
Symfony\Component\Uid\Ulid {
74+
#uid: "01F7B252SZQGTSQGYSGACASAW6"
75+
base 58: "1Ba6pJPFWDwghSKFVvfQ1B"
76+
RFC 4122: "0179d622-8b3f-bc35-9bc3-d98298acab86"
77+
}
78+
EODUMP;
79+
}
80+
81+
$this->assertDumpEquals($expectedDump, $ulid);
82+
}
83+
}

src/Symfony/Component/VarDumper/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ext-iconv": "*",
2525
"symfony/console": "^4.4|^5.0|^6.0",
2626
"symfony/process": "^4.4|^5.0|^6.0",
27+
"symfony/uid": "^5.0|^6.0",
2728
"twig/twig": "^2.13|^3.0.4"
2829
},
2930
"conflict": {

0 commit comments

Comments
 (0)
0