Update based on feedback

This commit is contained in:
Tomas 2020-09-11 06:38:29 +03:00 committed by Fabien Potencier
parent 1c21c78d25
commit d6a899395b
4 changed files with 7 additions and 13 deletions

View File

@ -28,7 +28,6 @@ CHANGELOG
* Made `BrowserKitAssertionsTrait` report the original error message in case of a failure * 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. * 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. * Deprecated `session.attribute_bag` service and `session.flash_bag` service.
* Added `UidNormalizer` to the framework serializer.
5.0.0 5.0.0
----- -----

View File

@ -44,6 +44,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer; use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
@ -106,6 +107,9 @@ return static function (ContainerConfigurator $container) {
->args([service('serializer.property_accessor')]) ->args([service('serializer.property_accessor')])
->tag('serializer.normalizer', ['priority' => 1000]) ->tag('serializer.normalizer', ['priority' => 1000])
->set('serializer.normalizer.uid', UidNormalizer::class)
->tag('serializer.normalizer', ['priority' => -915])
->set('serializer.normalizer.object', ObjectNormalizer::class) ->set('serializer.normalizer.object', ObjectNormalizer::class)
->args([ ->args([
service('serializer.mapping.class_metadata_factory'), service('serializer.mapping.class_metadata_factory'),

View File

@ -5,6 +5,7 @@ CHANGELOG
----- -----
* added `CompiledClassMetadataFactory` and `ClassMetadataFactoryCompiler` for faster metadata loading. * added `CompiledClassMetadataFactory` and `ClassMetadataFactoryCompiler` for faster metadata loading.
* added `UidNormalizer`
5.1.0 5.1.0
----- -----
@ -13,7 +14,6 @@ CHANGELOG
* added support for `\stdClass` to `ObjectNormalizer` * added support for `\stdClass` to `ObjectNormalizer`
* added the ability to ignore properties using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Ignore`) * 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 an option to serialize constraint violations payloads (e.g. severity)
* added `UidNormalizer`
5.0.0 5.0.0
----- -----

View File

@ -12,7 +12,6 @@
namespace Symfony\Component\Serializer\Normalizer; namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Uid\AbstractUid; use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid; use Symfony\Component\Uid\Ulid;
@ -22,13 +21,11 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface,
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @throws InvalidArgumentException
*/ */
public function normalize($object, string $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!$object instanceof AbstractUid) { 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; return (string) $object;
@ -44,19 +41,13 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface,
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @throws NotNormalizableValueException
*/ */
public function denormalize($data, string $type, string $format = null, array $context = []) 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 { try {
$uid = Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data); $uid = Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data);
} catch (\InvalidArgumentException $exception) { } 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; return $uid;