8000 Moved the NormalizationAwareInterface check to the ChainEncoder · nathanlon/symfony@12bdec3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12bdec3

Browse files
committed
Moved the NormalizationAwareInterface check to the ChainEncoder
This allows nesting a ChainEncoder inside another one without breaking the check.
1 parent 28e137c commit 12bdec3

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/Symfony/Component/Serializer/Encoder/ChainEncoder.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Encoder;
1313

1414
use Symfony\Component\Serializer\Encoder\EncoderInterface;
15+
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
1516
use Symfony\Component\Serializer\Exception\RuntimeException;
1617

1718
/**
@@ -53,6 +54,28 @@ public function supportsEncoding($format)
5354
return true;
5455
}
5556

57+
/**
58+
* Checks whether the normalization is needed for the given format.
59+
*
60+
* @param string $format
61+
*
62+
* @return Boolean
63+
*/
64+
public function needsNormalization($format)
65+
{
66+
$encoder = $this->getEncoder($format);
67+
68+
if (!$encoder instanceof NormalizationAwareInterface) {
69+
return true;
70+
}
71+
72+
if ($encoder instanceof self) {
73+
return $encoder->needsNormalization($format);
74+
}
75+
76+
return false;
77+
}
78+
5679
/**
5780
* Gets the encoder supporting the format.
5881
*
@@ -61,7 +84,7 @@ public function supportsEncoding($format)
6184
* @return EncoderInterface
6285
* @throws RuntimeException if no encoder is found
6386
*/
64-
public function getEncoder($format)
87+
private function getEncoder($format)
6588
{
6689
if (isset($this->encoderByFormat[$format])
6790
&& isset($this->encoders[$this->encoderByFormat[$format]])

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Serializer\Encoder\ChainEncoder;
1616
use Symfony\Component\Serializer\Encoder\EncoderInterface;
1717
use Symfony\Component\Serializer\Encoder\DecoderInterface;
18-
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
1918
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2019
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2120
use Symfony\Component\Serializer\Exception\RuntimeException;
@@ -41,11 +40,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
4140
protected $encoder;
4241
protected $decoder;
4342
protected $normalizers = array();
44-
protected $encoders = array();
4543
protected $normalizerCache = array();
4644
protected $denormalizerCache = array();
47-
protected $encoderByFormat = array();
48-
protected $decoderByFormat = array();
4945

5046
public function __construct(array $normalizers = array(), array $encoders = array())
5147
{
@@ -82,9 +78,7 @@ final public function serialize($data, $format)
8278
throw new UnexpectedValueException('Serialization for the format '.$format.' is not supported');
8379
}
8480

85-
$encoder = $this->encoder->getEncoder($format);
86-
87-
if (!$encoder instanceof NormalizationAwareInterface) {
81+
if ($this->encoder->needsNormalization($format)) {
8882
$data = $this->normalize($data, $format);
8983
}
9084

0 commit comments

Comments
 (0)
0