Quellcode durchsuchen

[Serializer] Properly check for the DecoderInterface

Jordi Boggiano vor 14 Jahren
Ursprung
Commit
8263b09b5c

+ 1 - 1
src/Symfony/Component/Serializer/Encoder/DecoderInterface.php

@@ -14,7 +14,7 @@ use Symfony\Component\Serializer\SerializerInterface;
  */
  */
 
 
 /**
 /**
- * Defines the interface of encoders
+ * Defines the interface of encoders that are able to decode their own format
  *
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
  */

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

@@ -4,6 +4,7 @@ namespace Symfony\Component\Serializer;
 
 
 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
 use Symfony\Component\Serializer\Encoder\EncoderInterface;
 use Symfony\Component\Serializer\Encoder\EncoderInterface;
+use Symfony\Component\Serializer\Encoder\DecoderInterface;
 
 
 /*
 /*
  * This file is part of the Symfony framework.
  * This file is part of the Symfony framework.
@@ -149,8 +150,8 @@ class Serializer implements SerializerInterface
      */
      */
     public function decode($data, $format)
     public function decode($data, $format)
     {
     {
-        if (!$this->hasEncoder($format)) {
-            throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format');
+        if (!$this->hasDecoder($format)) {
+            throw new \UnexpectedValueException('No encoder registered can decode the '.$format.' format');
         }
         }
         return $this->encoders[$format]->decode($data, $format);
         return $this->encoders[$format]->decode($data, $format);
     }
     }
@@ -213,6 +214,14 @@ class Serializer implements SerializerInterface
         return isset($this->encoders[$format]);
         return isset($this->encoders[$format]);
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    public function hasDecoder($format)
+    {
+        return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface;
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */