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 1c21c78

Browse filesBrowse files
Guillaume Pédelagrabefabpot
Guillaume Pédelagrabe
authored andcommitted
UidNormalizer.
1 parent be6146c commit 1c21c78
Copy full SHA for 1c21c78

File tree

4 files changed

+177
-0
lines changed
Filter options

4 files changed

+177
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CHANGELOG
2828
* Made `BrowserKitAssertionsTrait` report the original error message in case of a failure
2929
* Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration.
3030
* Deprecated `session.attribute_bag` service and `session.flash_bag` service.
31+
* Added `UidNormalizer` to the framework serializer.
3132

3233
5.0.0
3334
-----

‎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
@@ -13,6 +13,7 @@ CHANGELOG
1313
* added support for `\stdClass` to `ObjectNormalizer`
1414
* added the ability to ignore properties using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Ignore`)
1515
* added an option to serialize constraint violations payloads (e.g. severity)
16+
* added `UidNormalizer`
1617

1718
5.0.0
1819
-----
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Serializer\Normalizer;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
use Symfony\Component\Serializer\Exception\LogicException;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
17+
use Symfony\Component\Uid\AbstractUid;
18+
use Symfony\Component\Uid\Ulid;
19+
use Symfony\Component\Uid\Uuid;
20+
21+
final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*
26+
* @throws InvalidArgumentException
27+
*/
28+
public function normalize($object, string $format = null, array $context = [])
29+
{
30+
if (!$object instanceof AbstractUid) {
31+
throw new InvalidArgumentException('The object must be an instance of "\Symfony\Component\Uid\AbstractUid".');
32+
}
33+
34+
return (string) $object;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function supportsNormalization($data, string $format = null)
41+
{
42+
return $data instanceof AbstractUid;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*
48+
* @throws NotNormalizableValueException
49+
*/
50+
public function denormalize($data, string $type, string $format = null, array $context = [])
51+
{
52+
if (!class_exists(AbstractUid::class)) {
53+
throw new LogicException('You cannot use the "Symfony\Component\Serializer\Normalizer\UidNormalizer" as the Symfony Uid Component is not installed. Try running "composer require symfony/uid".');
54+
}
55+
56+
try {
57+
$uid = Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data);
58+
} catch (\InvalidArgumentException $exception) {
59+
throw new NotNormalizableValueException('The data is not a valid '.$type.' string representation.');
60+
}
61+
62+
return $uid;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function supportsDenormalization($data, string $type, string $format = null)
69+
{
70+
return is_a($type, AbstractUid::class, true);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function hasCacheableSupportsMethod(): bool
77+
{
78+
return __CLASS__ === static::class;
79+
}
80+
}
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
7+
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
8+
use Symfony\Component\Uid\AbstractUid;
9+
use Symfony\Component\Uid\Ulid;
10+
use Symfony\Component\Uid\Uuid;
11+
use Symfony\Component\Uid\UuidV1;
12+
use Symfony\Component\Uid\UuidV3;
13+
use Symfony\Component\Uid\UuidV4;
14+
use Symfony\Component\Uid\UuidV5;
15+
use Symfony\Component\Uid\UuidV6;
16+
17+
class UidNormalizerTest extends TestCase
18+
{
19+
/**
20+
* @var UidNormalizer
21+
*/
22+
private $normalizer;
23+
24+
protected function setUp(): void
25+
{
26+
$this->normalizer = new UidNormalizer();
27+
}
28+
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+
42+
public function testSupportsNormalization()
43+
{
44+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
45+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v3(Uuid::v1(), 'foo')));
46+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v4()));
47+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v5(Uuid::v1(), 'foo')));
48+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v6()));
49+
$this->assertTrue($this->normalizer->supportsNormalization(new Ulid()));
50+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
51+
}
52+
53+
/**
54+
* @dataProvider dataProvider
55+
*/
56+
public function testNormalize($uuidString, $class)
57+
{
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+
}
63+
}
64+
65+
public function testNormalizeForNonUid()
66+
{
67+
$this->expectException(InvalidArgumentException::class);
68+
$this->normalizer->normalize(new \stdClass());
69+
}
70+
71+
/**
72+
* @dataProvider dataProvider
73+
*/
74+
public function testSupportsDenormalization($uuidString, $class)
75+
{
76+
$this->assertTrue($this->normalizer->supportsDenormalization($uuidString, $class));
77+
}
78+
79+
public function testSupportsDenormalizationForNonUid()
80+
{
81+
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class));
82+
}
83+
84+
/**
85+
* @dataProvider dataProvider
86+
*/
87+
public function testDenormalize($uuidString, $class)
88+
{
89+
if (Ulid::class === $class) {
90+
$this->assertEquals(new Ulid($uuidString), $this->normalizer->denormalize($uuidString, $class));
91+
} else {
92+
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
93+
}
94+
}
95+
}

0 commit comments

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