浏览代码

[Serializer] Added hasEncoder and getEncoder to the SerializerInterface

Jordi Boggiano 14 年之前
父节点
当前提交
08f8b223ff

+ 27 - 2
src/Symfony/Component/Serializer/Serializer.php

@@ -105,7 +105,7 @@ class Serializer implements SerializerInterface
      */
     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 @@ class Serializer implements SerializerInterface
      */
     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 @@ class Serializer implements SerializerInterface
         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]);

+ 32 - 0
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 @@ interface SerializerInterface
      * @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);
 }