Przeglądaj źródła

[Serializer] Split decoder/encoder maps

Jordi Boggiano 14 lat temu
rodzic
commit
99c67134fe

+ 39 - 3
src/Symfony/Component/Serializer/Serializer.php

@@ -32,6 +32,7 @@ class Serializer implements SerializerInterface
 {
     private $normalizers = array();
     private $encoders = array();
+    private $decoders = array();
     protected $normalizerCache = array();
     protected $denormalizerCache = array();
 
@@ -149,9 +150,9 @@ class Serializer implements SerializerInterface
     public function decode($data, $format)
     {
         if (!$this->hasDecoder($format)) {
-            throw new \UnexpectedValueException('No encoder registered can decode the '.$format.' format');
+            throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
         }
-        return $this->encoders[$format]->decode($data, $format);
+        return $this->decoders[$format]->decode($data, $format);
     }
 
     /**
@@ -192,6 +193,17 @@ class Serializer implements SerializerInterface
         }
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function setDecoder($format, DecoderInterface $decoder)
+    {
+        $this->decoders[$format] = $decoder;
+        if ($decoder instanceof SerializerAwareInterface) {
+            $decoder->setSerializer($this);
+        }
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -200,6 +212,14 @@ class Serializer implements SerializerInterface
         return $this->encoders;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getDecoders()
+    {
+        return $this->decoders;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -208,6 +228,14 @@ class Serializer implements SerializerInterface
         return $this->encoders[$format];
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getDecoder($format)
+    {
+        return $this->decoders[$format];
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -221,7 +249,7 @@ class Serializer implements SerializerInterface
      */
     public function hasDecoder($format)
     {
-        return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface;
+        return isset($this->decoders[$format]);
     }
 
     /**
@@ -231,4 +259,12 @@ class Serializer implements SerializerInterface
     {
         unset($this->encoders[$format]);
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function removeDecoder($format)
+    {
+        unset($this->decoders[$format]);
+    }
 }

+ 1 - 1
tests/Symfony/Tests/Component/Serializer/SerializerTest.php

@@ -65,7 +65,7 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
 
     public function testDecode()
     {
-        $this->serializer->setEncoder('json', new JsonEncoder());
+        $this->serializer->setDecoder('json', new JsonEncoder());
         $data = array('foo', array(5, 3));
         $result = $this->serializer->decode(json_encode($data), 'json');
         $this->assertEquals($data, $result);