-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] add UidNormalizer #36406
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
Changes from all commits
eefeb39
fcdb75c
63f5b3c
1058e1e
5edc132
abe7069
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,11 @@ | |
<tag name="serializer.normalizer" priority="1000" /> | ||
</service> | ||
|
||
<service id="serializer.normalizer.uid" class="Symfony\Component\Serializer\Normalizer\UidNormalizer"> | ||
<!-- Run before serializer.normalizer.object --> | ||
<tag name="serializer.normalizer" priority="-880" /> | ||
</service> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be moved to the new PHP config file. |
||
|
||
<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer"> | ||
<argument type="service" id="serializer.mapping.class_metadata_factory" /> | ||
<argument type="service" id="serializer.name_converter.metadata_aware" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ CHANGELOG | |
* added support for `\stdClass` to `ObjectNormalizer` | ||
* added the ability to ignore properties using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Ignore`) | ||
* added an option to serialize constraint violations payloads (e.g. severity) | ||
* added `UidNormalizer` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be moved to the 5.2.0 section. |
||
|
||
5.0.0 | ||
----- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
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 | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed |
||
*/ | ||
public function normalize($object, string $format = null, array $context = []) | ||
{ | ||
if (!$object instanceof AbstractUid) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition can be removed as the supports method garuantees that already. |
||
throw new InvalidArgumentException('The object must be an instance of "\Symfony\Component\Uid\AbstractUid".'); | ||
} | ||
|
||
return (string) $object; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, string $format = null) | ||
{ | ||
return $data instanceof AbstractUid; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws NotNormalizableValueException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed |
||
*/ | ||
public function denormalize($data, string $type, string $format = null, array $context = []) | ||
{ | ||
if (!class_exists(AbstractUid::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed |
||
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".'); | ||
} | ||
|
||
try { | ||
$uid = Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data); | ||
guillbdx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (\InvalidArgumentException $exception) { | ||
throw new NotNormalizableValueException('The data is not a valid '.$type.' string representation.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use sprintf here. |
||
} | ||
|
||
return $uid; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, string $type, string $format = null) | ||
{ | ||
return is_a($type, AbstractUid::class, true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return __CLASS__ === static::class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Normalizer\UidNormalizer; | ||
use Symfony\Component\Uid\AbstractUid; | ||
use Symfony\Component\Uid\Ulid; | ||
use Symfony\Component\Uid\Uuid; | ||
use Symfony\Component\Uid\UuidV1; | ||
use Symfony\Component\Uid\UuidV3; | ||
use Symfony\Component\Uid\UuidV4; | ||
use Symfony\Component\Uid\UuidV5; | ||
use Symfony\Component\Uid\UuidV6; | ||
|
||
class UidNormalizerTest extends TestCase | ||
{ | ||
/** | ||
* @var UidNormalizer | ||
*/ | ||
private $normalizer; | ||
|
||
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())); | ||
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v3(Uuid::v1(), 'foo'))); | ||
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v4())); | ||
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v5(Uuid::v1(), 'foo'))); | ||
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v6())); | ||
$this->assertTrue($this->normalizer->supportsNormalization(new Ulid())); | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testNormalize($uuidString, $class) | ||
{ | ||
if (Ulid::class === $class) { | ||
$this->assertEquals($uuidString, $this->normalizer->normalize(Ulid::fromString($uuidString))); | ||
} else { | ||
$this->assertEquals($uuidString, $this->normalizer->normalize(Uuid::fromString($uuidString))); | ||
} | ||
} | ||
|
||
public function testNormalizeForNonUid() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->normalizer->normalize(new \stdClass()); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testSupportsDenormalization($uuidString, $class) | ||
{ | ||
$this->assertTrue($this->normalizer->supportsDenormalization($uuidString, $class)); | ||
} | ||
|
||
public function testSupportsDenormalizationForNonUid() | ||
{ | ||
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class)); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testDenormalize($uuidString, $class) | ||
{ | ||
if (Ulid::class === $class) { | ||
$this->assertEquals(new Ulid($uuidString), $this->normalizer->denormalize($uuidString, $class)); | ||
} else { | ||
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed