Skip to content

Commit f456eec

Browse files
committed
[Serializer][UidNormalizer] Add normalization formats
1 parent 29c7bfa commit f456eec

File tree

3 files changed

+139
-22
lines changed

3 files changed

+139
-22
lines changed

src/Symfony/Component/Serializer/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.3.0
5+
-----
6+
7+
* added normalization formats to `UidNormalizer`
8+
49
5.2.0
510
-----
611

src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,49 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\Exception\LogicException;
1415
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1516
use Symfony\Component\Uid\AbstractUid;
1617
use Symfony\Component\Uid\Ulid;
1718
use Symfony\Component\Uid\Uuid;
1819

1920
final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2021
{
22+
public const NORMALIZATION_FORMAT_KEY = 'uid_normalization_format';
23+
24+
public const NORMALIZATION_FORMAT_CANONICAL = 'canonical';
25+
public const NORMALIZATION_FORMAT_BASE_58 = 'base_58';
26+
public const NORMALIZATION_FORMAT_BASE_32 = 'base_32';
27+
public const NORMALIZATION_FORMAT_RFC_4122 = 'rfc_4122';
28+
29+
private $defaultContext = [
30+
self::NORMALIZATION_FORMAT_KEY => self::NORMALIZATION_FORMAT_CANONICAL,
31+
];
32+
33+
public function __construct(array $defaultContext = [])
34+
{
35+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
36+
}
37+
2138
/**
2239
* {@inheritdoc}
40+
*
41+
* @param AbstractUid $object
2342
*/
2443
public function normalize($object, string $format = null, array $context = [])
2544
{
26-
return (string) $object;
45+
switch ($context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY]) {
46+
case self::NORMALIZATION_FORMAT_CANONICAL:
47+
return (string) $object;
48+
case self::NORMALIZATION_FORMAT_BASE_58:
49+
return $object->toBase58();
50+
case self::NORMALIZATION_FORMAT_BASE_32:
51+
return $object->toBase32();
52+
case self::NORMALIZATION_FORMAT_RFC_4122:
53+
return $object->toRfc4122();
54+
}
55+
56+
throw new LogicException(sprintf('The "%s" format is not valid.', $context[self::NORMALIZATION_FORMAT_KEY] ?? $this->defaultContext[self::NORMALIZATION_FORMAT_KEY]));
2757
}
2858

2959
/**

src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer;
44

55
use PHPUnit\Framework\TestCase;
6-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
6+
use Symfony\Component\Serializer\Exception\LogicException;
77
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
88
use Symfony\Component\Uid\AbstractUid;
99
use Symfony\Component\Uid\Ulid;
@@ -26,19 +26,6 @@ protected function setUp(): void
2626
$this->normalizer = new UidNormalizer();
2727
}
2828

29-
public function dataProvider()
30-
{
31-
return [
32-
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
33-
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
34-
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
35-
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
36-
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
37-
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
38-
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
39-
];
40-
}
41-
4229
public function testSupportsNormalization()
4330
{
4431
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
@@ -50,16 +37,88 @@ public function testSupportsNormalization()
5037
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
5138
}
5239

40+
public function normalizeProvider()
41+
{
42+
$uidFormats = [null, 'canonical', 'base_58', 'base_32', 'rfc_4122'];
43+
$data = [
44+
[
45+
UuidV1::fromString('9b7541de-6f87-11ea-ab3c-9da9a81562fc'),
46+
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
47+
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
48+
'LCQS8f2p5SDSiAt9V7ZYnF',
49+
'4VEN0XWVW727NAPF4XN6M1ARQW',
50+
'9b7541de-6f87-11ea-ab3c-9da9a81562fc',
51+
],
52+
[
53+
UuidV3::fromString('e576629b-ff34-3642-9c08-1f5219f0d45b'),
54+
'e576629b-ff34-3642-9c08-1f5219f0d45b',
55+
'e576629b-ff34-3642-9c08-1f5219f0d45b',
56+
'VLRwe3qfi66uUAE3mYQ4Dp',
57+
'75ESH9QZSM6S19R20ZA8CZ1N2V',
58+
'e576629b-ff34-3642-9c08-1f5219f0d45b',
59+
],
60+
[
61+
UuidV4::fromString('4126dbc1-488e-4f6e-aadd-775dcbac482e'),
62+
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
63+
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
64+
'93d88pS3fdrDXNR2XxU9nu',
65+
'214VDW2J4E9XQANQBQBQ5TRJ1E',
66+
'4126dbc1-488e-4f6e-aadd-775dcbac482e',
67+
],
68+
[
69+
UuidV5::fromString('18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22'),
70+
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
71+
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
72+
'44epMFQYZ9byVSGis5dofo',
73+
'0RSQSX7TGVBCHTKHA0NF8E5QS2',
74+
'18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22',
75+
],
76+
[
77+
UuidV6::fromString('1ea6ecef-eb9a-66fe-b62b-957b45f17e43'),
78+
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
79+
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
80+
'4nXtvo2iuyYefrqTMhvogn',
81+
'0YMVPEZTWTCVZBCAWNFD2Z2ZJ3',
82+
'1ea6ecef-eb9a-66fe-b62b-957b45f17e43',
83+
],
84+
[
85+
Ulid::fromString('01E4BYF64YZ97MDV6RH0HAMN6X'),
86+
'01E4BYF64YZ97MDV6RH0HAMN6X',
87+
'01E4BYF64YZ97MDV6RH0HAMN6X',
88+
'1BKuy2YWf8Yf9vSkA2wDpg',
89+
'01E4BYF64YZ97MDV6RH0HAMN6X',
90+
'017117e7-989e-fa4f-46ec-d88822aa54dd',
91+
],
92+
];
93+
94+
foreach ($uidFormats as $i => $uidFormat) {
95+
foreach ($data as $uidClass => $row) {
96+
yield [$row[$i + 1], $row[0], $uidFormat];
97+
}
98+
}
99+
}
100+
53101
/**
54-
* @dataProvider dataProvider
102+
* @dataProvider normalizeProvider
55103
*/
56-
public function testNormalize($uuidString, $class)
104+
public function testNormalize(string $expected, AbstractUid $uid, ?string $uidFormat)
57105
{
58-
if (Ulid::class === $class) {
59-
$this->assertEquals($uuidString, $this->normalizer->normalize(Ulid::fromString($uuidString)));
60-
} else {
61-
$this->assertEquals($uuidString, $this->normalizer->normalize(Uuid::fromString($uuidString)));
62-
}
106+
$this->assertSame($expected, $this->normalizer->normalize($uid, null, null !== $uidFormat ? [
107+
'uid_normalization_format' => $uidFormat,
108+
] : []));
109+
}
110+
111+
public function dataProvider()
112+
{
113+
return [
114+
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
115+
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
116+
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
117+
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
118+
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
119+
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
120+
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
121+
];
63122
}
64123

65124
/**
@@ -86,4 +145,27 @@ public function testDenormalize($uuidString, $class)
86145
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
87146
}
88147
}
148+
149+
public function testNormalizeWithNormalizationFormatPassedInConstructor()
150+
{
151+
$uidNormalizer = new UidNormalizer([
152+
'uid_normalization_format' => 'rfc_4122',
153+
]);
154+
$ulid = Ulid::fromString('01ETWV01C0GYQ5N92ZK7QRGB10');
155+
156+
$this->assertSame('0176b9b0-0580-87ae-5aa4-5f99ef882c20', $uidNormalizer->normalize($ulid));
157+
$this->assertSame('01ETWV01C0GYQ5N92ZK7QRGB10', $uidNormalizer->normalize($ulid, null, [
158+
'uid_normalization_format' => 'canonical',
159+
]));
160+
}
161+
162+
public function testNormalizeWithNormalizationFormatNotValid()
163+
{
164+
$this->expectException(LogicException::class);
165+
$this->expectDeprecationMessage('The "ccc" format is not valid.');
166+
167+
$this->normalizer->normalize(new Ulid(), null, [
168+
'uid_normalization_format' => 'ccc',
169+
]);
170+
}
89171
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy