diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index b329cf1542334..799517fcd72b0 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +7.1 +--- + +* Let `UnwrappingDenormalizer` implement `DenormalizerAwareInterface` instead of `SerializerAwareInterface` +* Deprecate method `UnwrappingDenormalizer::setSerializer()`, use `UnwrappingDenormalizer::setDenormalizer()` instead + 7.0 --- diff --git a/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php index 51397961c7df6..041b5cba4cdce 100644 --- a/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php @@ -14,15 +14,14 @@ use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\Serializer\Exception\LogicException; -use Symfony\Component\Serializer\SerializerAwareInterface; -use Symfony\Component\Serializer\SerializerAwareTrait; +use Symfony\Component\Serializer\SerializerInterface; /** * @author Eduard Bulava */ -final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface +final class UnwrappingDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface { - use SerializerAwareTrait; + use DenormalizerAwareTrait; public const UNWRAP_PATH = 'unwrap_path'; @@ -51,15 +50,25 @@ public function denormalize(mixed $data, string $type, string $format = null, ar $data = $this->propertyAccessor->getValue($data, $propertyPath); } - if (!$this->serializer instanceof DenormalizerInterface) { - throw new LogicException('Cannot unwrap path because the injected serializer is not a denormalizer.'); - } - - return $this->serializer->denormalize($data, $type, $format, $context); + return $this->denormalizer->denormalize($data, $type, $format, $context); } public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool { return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']); } + + /** + * @deprecated Since symfony/serializer 7.1: The "setSerializer()" method is deprecated, use "setDenormalizer()" instead. + */ + public function setSerializer(SerializerInterface $serializer): void + { + trigger_deprecation('symfony/serializer', '7.1', 'The "%s()" method is deprecated, use "setDenormalizer()" instead.', __METHOD__); + + if (!$serializer instanceof DenormalizerInterface) { + throw new LogicException(sprintf('Cannot set denormalizer because the injected serializer does not implement the "%s".', DenormalizerInterface::class)); + } + + $this->setDenormalizer($serializer); + } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/UnwrappinDenormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/UnwrappinDenormalizerTest.php index 59ddd6da65aad..ab21fb3d0aa93 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/UnwrappinDenormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/UnwrappinDenormalizerTest.php @@ -13,8 +13,8 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; -use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy; /** @@ -23,13 +23,13 @@ class UnwrappinDenormalizerTest extends TestCase { private UnwrappingDenormalizer $denormalizer; - private MockObject&Serializer $serializer; + private MockObject&DenormalizerInterface $baseDenormalizer; protected function setUp(): void { - $this->serializer = $this->createMock(Serializer::class); + $this->baseDenormalizer = $this->createMock(DenormalizerInterface::class); $this->denormalizer = new UnwrappingDenormalizer(); - $this->denormalizer->setSerializer($this->serializer); + $this->denormalizer->setDenormalizer($this->baseDenormalizer); } public function testSupportsNormalization() @@ -46,7 +46,7 @@ public function testDenormalize() $expected->bar = 'bar'; $expected->setFoo('foo'); - $this->serializer->expects($this->exactly(1)) + $this->baseDenormalizer->expects($this->exactly(1)) ->method('denormalize') ->with(['foo' => 'foo', 'bar' => 'bar', 'baz' => true]) ->willReturn($expected); @@ -65,7 +65,7 @@ public function testDenormalize() public function testDenormalizeInvalidPath() { - $this->serializer->expects($this->exactly(1)) + $this->baseDenormalizer->expects($this->exactly(1)) ->method('denormalize') ->with(null) ->willReturn(new ObjectDummy()); diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 627bfccaf3061..9f5607b4378aa 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -17,7 +17,8 @@ ], "require": { "php": ">=8.2", - "symfony/polyfill-ctype": "~1.8" + "symfony/polyfill-ctype": "~1.8", + "symfony/deprecation-contracts": "^2.5|^3" }, "require-dev": { "phpdocumentor/reflection-docblock": "^3.2|^4.0|^5.0",