[Serializer] deprecated normalizers and encoders who dont implement the base interfaces

This commit is contained in:
Rhodri Pugh 2018-09-21 14:11:40 +02:00 committed by Nicolas Grekas
parent 0d9154e5bb
commit cbc2be8ba8
3 changed files with 40 additions and 2 deletions

View File

@ -13,6 +13,12 @@ CHANGELOG
the format and the context in a name converter
* the `AbstractNormalizer::handleCircularReference()` method will have two new `$format`
and `$context` arguments in version 5.0, not defining them is deprecated
* deprecated creating a `Serializer` with normalizers which do not implement
either `NormalizerInterface` or `DenormalizerInterface`
* deprecated creating a `Serializer` with normalizers which do not implement
either `NormalizerInterface` or `DenormalizerInterface`
* deprecated creating a `Serializer` with encoders which do not implement
either `EncoderInterface` or `DecoderInterface`
4.1.0
-----

View File

@ -64,6 +64,10 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
private $denormalizerCache = array();
private $normalizerCache = array();
/**
* @param (NormalizerInterface|DenormalizerInterface)[] $normalizers
* @param (EncoderInterface|DecoderInterface)[] $encoders
*/
public function __construct(array $normalizers = array(), array $encoders = array())
{
foreach ($normalizers as $normalizer) {
@ -78,6 +82,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
if ($normalizer instanceof NormalizerAwareInterface) {
$normalizer->setNormalizer($this);
}
if (!($normalizer instanceof NormalizerInterface || $normalizer instanceof DenormalizerInterface)) {
@trigger_error(\sprintf('Passing normalizers ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class), E_USER_DEPRECATED);
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class));
}
}
$this->normalizers = $normalizers;
@ -93,6 +102,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
if ($encoder instanceof EncoderInterface) {
$realEncoders[] = $encoder;
}
if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) {
@trigger_error(\sprintf('Passing encoders ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($encoder), EncoderInterface::class, DecoderInterface::class), E_USER_DEPRECATED);
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), EncoderInterface::class, DecoderInterface::class));
}
}
$this->encoder = new ChainEncoder($realEncoders);
$this->decoder = new ChainDecoder($decoders);

View File

@ -55,6 +55,24 @@ class SerializerTest extends TestCase
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
}
/**
* @expectedDeprecation Passing normalizers ("stdClass") which do not implement either "Symfony\Component\Serializer\Normalizer\NormalizerInterface" or "Symfony\Component\Serializer\Normalizer\DenormalizerInterface" has been deprecated since Symfony 4.2.
* @group legacy
*/
public function testDeprecationErrorOnInvalidNormalizer()
{
new Serializer(array(new \stdClass()));
}
/**
* @expectedDeprecation Passing encoders ("stdClass") which do not implement either "Symfony\Component\Serializer\Encoder\EncoderInterface" or "Symfony\Component\Serializer\Encoder\DecoderInterface" has been deprecated since Symfony 4.2.
* @group legacy
*/
public function testDeprecationErrorOnInvalidEncoder()
{
new Serializer(array(), array(new \stdClass()));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
@ -334,7 +352,7 @@ class SerializerTest extends TestCase
public function testNormalizerAware()
{
$normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock();
$normalizerAware = $this->getMockBuilder(array(NormalizerAwareInterface::class, NormalizerInterface::class))->getMock();
$normalizerAware->expects($this->once())
->method('setNormalizer')
->with($this->isInstanceOf(NormalizerInterface::class));
@ -344,7 +362,7 @@ class SerializerTest extends TestCase
public function testDenormalizerAware()
{
$denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock();
$denormalizerAware = $this->getMockBuilder(array(DenormalizerAwareInterface::class, DenormalizerInterface::class))->getMock();
$denormalizerAware->expects($this->once())
->method('setDenormalizer')
->with($this->isInstanceOf(DenormalizerInterface::class));