From 1c21c78d25a8bb8d1ecb1f728b92d470b4c35a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20P=C3=A9delagrabe?= Date: Thu, 9 Apr 2020 18:25:57 +0200 Subject: [PATCH 1/2] UidNormalizer. --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + src/Symfony/Component/Serializer/CHANGELOG.md | 1 + .../Serializer/Normalizer/UidNormalizer.php | 80 ++++++++++++++++ .../Tests/Normalizer/UidNormalizerTest.php | 95 +++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php create mode 100644 src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index f046415bb9..a93baaed07 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -28,6 +28,7 @@ CHANGELOG * Made `BrowserKitAssertionsTrait` report the original error message in case of a failure * Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration. * Deprecated `session.attribute_bag` service and `session.flash_bag` service. + * Added `UidNormalizer` to the framework serializer. 5.0.0 ----- diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index ea826ba6c7..8cbfdbe393 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -13,6 +13,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` 5.0.0 ----- diff --git a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php new file mode 100644 index 0000000000..b75ca0c436 --- /dev/null +++ b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php @@ -0,0 +1,80 @@ + + * + * 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 + */ + public function normalize($object, string $format = null, array $context = []) + { + if (!$object instanceof AbstractUid) { + 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 + */ + public function denormalize($data, string $type, string $format = null, array $context = []) + { + if (!class_exists(AbstractUid::class)) { + 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); + } catch (\InvalidArgumentException $exception) { + throw new NotNormalizableValueException('The data is not a valid '.$type.' string representation.'); + } + + 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; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php new file mode 100644 index 0000000000..17d3d07c75 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php @@ -0,0 +1,95 @@ +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)); + } + } +} From d6a899395b667f8e516db64c2174eee8911cb448 Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 11 Sep 2020 06:38:29 +0300 Subject: [PATCH 2/2] Update based on feedback --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 1 - .../FrameworkBundle/Resources/config/serializer.php | 4 ++++ src/Symfony/Component/Serializer/CHANGELOG.md | 2 +- .../Serializer/Normalizer/UidNormalizer.php | 13 ++----------- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index a93baaed07..f046415bb9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -28,7 +28,6 @@ CHANGELOG * Made `BrowserKitAssertionsTrait` report the original error message in case of a failure * Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration. * Deprecated `session.attribute_bag` service and `session.flash_bag` service. - * Added `UidNormalizer` to the framework serializer. 5.0.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php index a0c5be34b9..5f02d765f9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php @@ -44,6 +44,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ProblemNormalizer; use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; +use Symfony\Component\Serializer\Normalizer\UidNormalizer; use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; @@ -106,6 +107,9 @@ return static function (ContainerConfigurator $container) { ->args([service('serializer.property_accessor')]) ->tag('serializer.normalizer', ['priority' => 1000]) + ->set('serializer.normalizer.uid', UidNormalizer::class) + ->tag('serializer.normalizer', ['priority' => -915]) + ->set('serializer.normalizer.object', ObjectNormalizer::class) ->args([ service('serializer.mapping.class_metadata_factory'), diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 8cbfdbe393..e5915f7af9 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * added `CompiledClassMetadataFactory` and `ClassMetadataFactoryCompiler` for faster metadata loading. +* added `UidNormalizer` 5.1.0 ----- @@ -13,7 +14,6 @@ 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` 5.0.0 ----- diff --git a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php index b75ca0c436..7ab8978fec 100644 --- a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php @@ -12,7 +12,6 @@ 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; @@ -22,13 +21,11 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, { /** * {@inheritdoc} - * - * @throws InvalidArgumentException */ public function normalize($object, string $format = null, array $context = []) { if (!$object instanceof AbstractUid) { - throw new InvalidArgumentException('The object must be an instance of "\Symfony\Component\Uid\AbstractUid".'); + throw new InvalidArgumentException('The object must be an instance of "Symfony\Component\Uid\AbstractUid".'); } return (string) $object; @@ -44,19 +41,13 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, /** * {@inheritdoc} - * - * @throws NotNormalizableValueException */ public function denormalize($data, string $type, string $format = null, array $context = []) { - if (!class_exists(AbstractUid::class)) { - 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); } catch (\InvalidArgumentException $exception) { - throw new NotNormalizableValueException('The data is not a valid '.$type.' string representation.'); + throw new NotNormalizableValueException(sprintf('The data is not a valid "%s" string representation.', $type)); } return $uid;