Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b88e6f

Browse filesBrowse files
committed
[Serializer][UidNormalizer] Add normalization formats
1 parent 9e47d90 commit 6b88e6f
Copy full SHA for 6b88e6f

File tree

3 files changed

+135
-21
lines changed
Filter options

3 files changed

+135
-21
lines changed

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
5.2.0
1011
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php
+31-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php
+103-20Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer;
44

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

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

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+
52101
/**
53-
* @dataProvider dataProvider
102+
* @dataProvider normalizeProvider
54103
*/
55-
public function testNormalize($uuidString, $class)
104+
public function testNormalize(string $expected, AbstractUid $uid, ?string $uidFormat)
56105
{
57-
if (Ulid::class === $class) {
58-
$this->assertEquals($uuidString, $this->normalizer->normalize(Ulid::fromString($uuidString)));
59-
} else {
60-
$this->assertEquals($uuidString, $this->normalizer->normalize(Uuid::fromString($uuidString)));
61-
}
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+
];
62122
}
63123

64124
/**
@@ -85,4 +145,27 @@ public function testDenormalize($uuidString, $class)
85145
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
86146
}
87147
}
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+
}
88171
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.