Moved the NormalizationAwareInterface check to the ChainEncoder

This allows nesting a ChainEncoder inside another one without breaking
the check.
This commit is contained in:
Christophe Coevoet 2012-07-14 11:05:55 +02:00
parent 28e137c920
commit 12bdec3cd2
2 changed files with 25 additions and 8 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
/**
@ -53,6 +54,28 @@ class ChainEncoder implements EncoderInterface
return true;
}
/**
* Checks whether the normalization is needed for the given format.
*
* @param string $format
*
* @return Boolean
*/
public function needsNormalization($format)
{
$encoder = $this->getEncoder($format);
if (!$encoder instanceof NormalizationAwareInterface) {
return true;
}
if ($encoder instanceof self) {
return $encoder->needsNormalization($format);
}
return false;
}
/**
* Gets the encoder supporting the format.
*
@ -61,7 +84,7 @@ class ChainEncoder implements EncoderInterface
* @return EncoderInterface
* @throws RuntimeException if no encoder is found
*/
public function getEncoder($format)
private function getEncoder($format)
{
if (isset($this->encoderByFormat[$format])
&& isset($this->encoders[$this->encoderByFormat[$format]])

View File

@ -15,7 +15,6 @@ use Symfony\Component\Serializer\Encoder\ChainDecoder;
use Symfony\Component\Serializer\Encoder\ChainEncoder;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
@ -41,11 +40,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
protected $encoder;
protected $decoder;
protected $normalizers = array();
protected $encoders = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();
protected $encoderByFormat = array();
protected $decoderByFormat = array();
public function __construct(array $normalizers = array(), array $encoders = array())
{
@ -82,9 +78,7 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
throw new UnexpectedValueException('Serialization for the format '.$format.' is not supported');
}
$encoder = $this->encoder->getEncoder($format);
if (!$encoder instanceof NormalizationAwareInterface) {
if ($this->encoder->needsNormalization($format)) {
$data = $this->normalize($data, $format);
}