Skip to content

Commit

Permalink
[VarDumper] Add casters for Symfony UUIDs and ULIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Jun 11, 2021
1 parent 173cf39 commit 2e6661f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add casters for Symfony's UUIDs and ULIDs

5.2.0
-----

Expand Down
28 changes: 28 additions & 0 deletions Caster/SymfonyCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\VarDumper\Caster;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\VarDumper\Cloner\Stub;

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

return $a;
}

public static function castUuid(Uuid $uuid, array $a, Stub $stub, bool $isNested)
{
$a[Caster::PREFIX_VIRTUAL.'toBase58'] = $uuid->toBase58();
$a[Caster::PREFIX_VIRTUAL.'toBase32'] = $uuid->toBase32();

// symfony/uid >= 5.3
if (method_exists($uuid, 'getDateTime')) {
$a[Caster::PREFIX_VIRTUAL.'time'] = $uuid->getDateTime()->format('Y-m-d H:i:s.u \U\T\C');
}

return $a;
}

public static function castUlid(Ulid $ulid, array $a, Stub $stub, bool $isNested)
{
$a[Caster::PREFIX_VIRTUAL.'toBase58'] = $ulid->toBase58();
$a[Caster::PREFIX_VIRTUAL.'toRfc4122'] = $ulid->toRfc4122();

// symfony/uid >= 5.3
if (method_exists($ulid, 'getDateTime')) {
$a[Caster::PREFIX_VIRTUAL.'time'] = $ulid->getDateTime()->format('Y-m-d H:i:s.v \U\T\C');
}

return $a;
}
}
2 changes: 2 additions & 0 deletions Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ abstract class AbstractCloner implements ClonerInterface
'Symfony\Component\HttpClient\Response\CurlResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
'Symfony\Component\HttpClient\Response\NativeResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
'Symfony\Component\HttpFoundation\Request' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castRequest'],
'Symfony\Component\Uid\Ulid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUlid'],
'Symfony\Component\Uid\Uuid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUuid'],
'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castThrowingCasterException'],
'Symfony\Component\VarDumper\Caster\TraceStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castTraceStub'],
'Symfony\Component\VarDumper\Caster\FrameStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFrameStub'],
Expand Down
83 changes: 83 additions & 0 deletions Tests/Caster/SymfonyCasterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Caster;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV6;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

final class SymfonyCasterTest extends TestCase
{
use VarDumperTestTrait;

public function testCastUuid()
{
$uuid = new UuidV4('83a9db35-3c8c-4040-b3c1-02eccc00b419');
$expectedDump = <<<EODUMP
Symfony\Component\Uid\UuidV4 {
#uid: "83a9db35-3c8c-4040-b3c1-02eccc00b419"
toBase58: "HFzAAuYvev42cCjwqpnKqz"
toBase32: "43N7DKAF4C810B7G82XK601D0S"
}
EODUMP;
$this->assertDumpEquals($expectedDump, $uuid);

$uuid = new UuidV6('1ebc50e9-8a23-6704-ad6f-59afd5cda7e5');
if (method_exists($uuid, 'getDateTime')) {
$expectedDump = <<<EODUMP
Symfony\Component\Uid\UuidV6 {
#uid: "1ebc50e9-8a23-6704-ad6f-59afd5cda7e5"
toBase58: "4o8c5m6v4L8h5teww36JDa"
toBase32: "0YQH8EK2H3CW2ATVTSNZAWV9Z5"
time: "2021-06-04 08:26:44.591386 UTC"
}
EODUMP;
} else {
$expectedDump = <<<EODUMP
Symfony\Component\Uid\UuidV6 {
#uid: "1ebc50e9-8a23-6704-ad6f-59afd5cda7e5"
toBase58: "4o8c5m6v4L8h5teww36JDa"
toBase32: "0YQH8EK2H3CW2ATVTSNZAWV9Z5"
}
EODUMP;
}

$this->assertDumpEquals($expectedDump, $uuid);
}

public function testCastUlid()
{
$ulid = new Ulid('01F7B252SZQGTSQGYSGACASAW6');
if (method_exists($ulid, 'getDateTime')) {
$expectedDump = <<<EODUMP
Symfony\Component\Uid\Ulid {
#uid: "01F7B252SZQGTSQGYSGACASAW6"
toBase58: "1Ba6pJPFWDwghSKFVvfQ1B"
toRfc4122: "0179d622-8b3f-bc35-9bc3-d98298acab86"
time: "2021-06-04 08:27:38.687 UTC"
}
EODUMP;
} else {
$expectedDump = <<<EODUMP
Symfony\Component\Uid\Ulid {
#uid: "01F7B252SZQGTSQGYSGACASAW6"
toBase58: "1Ba6pJPFWDwghSKFVvfQ1B"
toRfc4122: "0179d622-8b3f-bc35-9bc3-d98298acab86"
}
EODUMP;
}

$this->assertDumpEquals($expectedDump, $ulid);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ext-iconv": "*",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/process": "^4.4|^5.0|^6.0",
"symfony/uid": "^5.1|^6.0",
"twig/twig": "^2.13|^3.0.4"
},
"conflict": {
Expand Down

0 comments on commit 2e6661f

Please sign in to comment.