From 08f8b223ff4d7e05248e0f83b17a306c5c10fe50 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 25 Jan 2011 10:40:22 +0100 Subject: [PATCH] [Serializer] Added hasEncoder and getEncoder to the SerializerInterface --- .../Component/Serializer/Serializer.php | 29 +++++++++++++++-- .../Serializer/SerializerInterface.php | 32 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 6b510a850850..0147d8755b8b 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -105,7 +105,7 @@ public function normalize($data, $format) */ public function encode($data, $format) { - if (!isset($this->encoders[$format])) { + if (!$this->hasEncoder($format)) { throw new \UnexpectedValueException('No encoder registered for the '.$format.' format'); } return $this->encoders[$format]->encode($data, $format); @@ -116,7 +116,7 @@ public function encode($data, $format) */ public function decode($data, $format) { - if (!isset($this->encoders[$format])) { + if (!$this->hasEncoder($format)) { throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format'); } return $this->encoders[$format]->decode($data, $format); @@ -138,17 +138,42 @@ public function removeNormalizer(NormalizerInterface $normalizer) unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]); } + /** + * {@inheritdoc} + */ public function addEncoder($format, EncoderInterface $encoder) { $this->encoders[$format] = $encoder; $encoder->setSerializer($this); } + /** + * {@inheritdoc} + */ public function getEncoders() { return $this->encoders; } + /** + * {@inheritdoc} + */ + public function getEncoder($format) + { + return $this->encoders[$format]; + } + + /** + * {@inheritdoc} + */ + public function hasEncoder($format) + { + return isset($this->encoders[$format]); + } + + /** + * {@inheritdoc} + */ public function removeEncoder($format) { unset($this->encoders[$format]); diff --git a/src/Symfony/Component/Serializer/SerializerInterface.php b/src/Symfony/Component/Serializer/SerializerInterface.php index af08a1b11daa..df6f3617d786 100644 --- a/src/Symfony/Component/Serializer/SerializerInterface.php +++ b/src/Symfony/Component/Serializer/SerializerInterface.php @@ -2,6 +2,8 @@ namespace Symfony\Component\Serializer; +use Symfony\Component\Serializer\Encoder\EncoderInterface; + /* * This file is part of the Symfony framework. * @@ -73,4 +75,34 @@ function encode($data, $format); * @return mixed */ function decode($data, $format); + + /** + * @param string $format format name + * @param EncoderInterface $encoder + */ + function addEncoder($format, EncoderInterface $encoder); + + /** + * @param string $format format name + * @return EncoderInterface + */ + function getEncoders(); + + /** + * @return array[]EncoderInterface + */ + function getEncoder($format); + + /** + * Checks whether the serializer has an encoder registered for the given format + * + * @param string $format format name + * @return Boolean + */ + function hasEncoder($format); + + /** + * @param string $format format name + */ + function removeEncoder($format); }