Skip to content

[Serializer] [UidNormalizer] Add normalization formats #39675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
* added normalization formats to `UidNormalizer`

5.2.0
-----
Expand Down
32 changes: 31 additions & 1 deletion src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,49 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
public const NORMALIZATION_FORMAT_KEY = 'uid_normalization_format';

public const NORMALIZATION_FORMAT_CANONICAL = 'canonical';
public const NORMALIZATION_FORMAT_BASE_58 = 'base_58';
public const NORMALIZATION_FORMAT_BASE_32 = 'base_32';
public const NORMALIZATION_FORMAT_RFC_4122 = 'rfc_4122';

private $defaultContext = [
self::NORMALIZATION_FORMAT_KEY => self::NORMALIZATION_FORMAT_CANONICAL,
];

public function __construct(array $defaultContext = [])
{
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}

/**
* {@inheritdoc}
*
* @param AbstractUid $object
*/
public function normalize($object, string $format = null, array $context = [])
{
return (string) $object;
switch ($context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY]) {
case self::NORMALIZATION_FORMAT_CANONICAL:
return (string) $object;
case self::NORMALIZATION_FORMAT_BASE_58:
return $object->toBase58();
case self::NORMALIZATION_FORMAT_BASE_32:
return $object->toBase32();
case self::NORMALIZATION_FORMAT_RFC_4122:
return $object->toRfc4122();
}

throw new LogicException(sprintf('The "%s" format is not valid.', $context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY]));
}

/**
Expand Down
123 changes: 103 additions & 20 deletions src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
Expand All @@ -25,19 +26,6 @@ protected function setUp(): void
$this->normalizer = new UidNormalizer();
}

public function dataProvider()
{
return [
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
];
}

public function testSupportsNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
Expand All @@ -49,16 +37,88 @@ public function testSupportsNormalization()
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}

public function normalizeProvider()
{
$uidFormats = [null, 'canonical', 'base_58', 'base_32', 'rfc_4122'];
$data = [
[
UuidV1::fromString('9b7541de-6f87-11ea-ab3c-9da9a81562fc'),
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
'LCQS8f2p5SDSiAt9V7ZYnF',
'4VEN0XWVW727NAPF4XN6M1ARQW',
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
],
[
UuidV3::fromString('e576629b-ff34-3642-9c08-1f5219f0d45b'),
'e576629b-ff34-3642-9c08-1f5219f0d45b',
'e576629b-ff34-3642-9c08-1f5219f0d45b',
'VLRwe3qfi66uUAE3mYQ4Dp',
'75ESH9QZSM6S19R20ZA8CZ1N2V',
'e576629b-ff34-3642-9c08-1f5219f0d45b',
],
[
UuidV4::fromString('4126dbc1-488e-4f6e-aadd-775dcbac482e'),
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
'93d88pS3fdrDXNR2XxU9nu',
'214VDW2J4E9XQANQBQBQ5TRJ1E',
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
],
[
UuidV5::fromString('18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22'),
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
'44epMFQYZ9byVSGis5dofo',
'0RSQSX7TGVBCHTKHA0NF8E5QS2',
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
],
[
UuidV6::fromString('1ea6ecef-eb9a-66fe-b62b-957b45f17e43'),
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
'4nXtvo2iuyYefrqTMhvogn',
'0YMVPEZTWTCVZBCAWNFD2Z2ZJ3',
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
],
[
Ulid::fromString('01E4BYF64YZ97MDV6RH0HAMN6X'),
'01E4BYF64YZ97MDV6RH0HAMN6X',
'01E4BYF64YZ97MDV6RH0HAMN6X',
'1BKuy2YWf8Yf9vSkA2wDpg',
'01E4BYF64YZ97MDV6RH0HAMN6X',
'017117e7-989e-fa4f-46ec-d88822aa54dd',
],
];

foreach ($uidFormats as $i => $uidFormat) {
foreach ($data as $uidClass => $row) {
yield [$row[$i + 1], $row[0], $uidFormat];
}
}
}

/**
* @dataProvider dataProvider
* @dataProvider normalizeProvider
*/
public function testNormalize($uuidString, $class)
public function testNormalize(string $expected, AbstractUid $uid, ?string $uidFormat)
{
if (Ulid::class === $class) {
$this->assertEquals($uuidString, $this->normalizer->normalize(Ulid::fromString($uuidString)));
} else {
$this->assertEquals($uuidString, $this->normalizer->normalize(Uuid::fromString($uuidString)));
}
$this->assertSame($expected, $this->normalizer->normalize($uid, null, null !== $uidFormat ? [
'uid_normalization_format' => $uidFormat,
] : []));
}

public function dataProvider()
{
return [
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
];
}

/**
Expand All @@ -85,4 +145,27 @@ public function testDenormalize($uuidString, $class)
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
}
}

public function testNormalizeWithNormalizationFormatPassedInConstructor()
{
$uidNormalizer = new UidNormalizer([
'uid_normalization_format' => 'rfc_4122',
]);
$ulid = Ulid::fromString('01ETWV01C0GYQ5N92ZK7QRGB10');

$this->assertSame('0176b9b0-0580-87ae-5aa4-5f99ef882c20', $uidNormalizer->normalize($ulid));
$this->assertSame('01ETWV01C0GYQ5N92ZK7QRGB10', $uidNormalizer->normalize($ulid, null, [
'uid_normalization_format' => 'canonical',
]));
}

public function testNormalizeWithNormalizationFormatNotValid()
{
$this->expectException(LogicException::class);
$this->expectDeprecationMessage('The "ccc" format is not valid.');

$this->normalizer->normalize(new Ulid(), null, [
'uid_normalization_format' => 'ccc',
]);
}
}