瀏覽代碼

Merge remote branch 'lsmith77/add_decoder_interface'

* lsmith77/add_decoder_interface:
  added a DecodeInterface (and SerializerAwareInterface) to make it easier to identify if an Encoder also supports decoding
Fabien Potencier 14 年之前
父節點
當前提交
f4aae27904

+ 3 - 2
src/Symfony/Component/Serializer/Encoder/AbstractEncoder.php

@@ -3,6 +3,7 @@
 namespace Symfony\Component\Serializer\Encoder;
 
 use Symfony\Component\Serializer\SerializerInterface;
+use Symfony\Component\Serializer\SerializerAwareInterface;
 
 /*
  * This file is part of the Symfony framework.
@@ -18,7 +19,7 @@ use Symfony\Component\Serializer\SerializerInterface;
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-abstract class AbstractEncoder implements EncoderInterface
+abstract class AbstractEncoder implements SerializerAwareInterface, EncoderInterface
 {
     protected $serializer;
 
@@ -37,4 +38,4 @@ abstract class AbstractEncoder implements EncoderInterface
     {
         return $this->serializer;
     }
-}
+}

+ 32 - 0
src/Symfony/Component/Serializer/Encoder/DecoderInterface.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace Symfony\Component\Serializer\Encoder;
+
+use Symfony\Component\Serializer\SerializerInterface;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Defines the interface of encoders
+ *
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+interface DecoderInterface
+{
+    /**
+     * Decodes a string into PHP data
+     *
+     * @param string $data data to decode
+     * @param string $format format to decode from
+     * @return mixed
+     * @api
+     */
+    function decode($data, $format);
+}

+ 0 - 26
src/Symfony/Component/Serializer/Encoder/EncoderInterface.php

@@ -29,30 +29,4 @@ interface EncoderInterface
      * @api
      */
     function encode($data, $format);
-
-    /**
-     * Decodes a string into PHP data
-     *
-     * @param string $data data to decode
-     * @param string $format format to decode from
-     * @return mixed
-     * @api
-     */
-    function decode($data, $format);
-
-    /**
-     * Sets the owning Serializer object
-     *
-     * @param SerializerInterface $serializer
-     * @api
-     */
-    function setSerializer(SerializerInterface $serializer);
-
-    /**
-     * Gets the owning Serializer object
-     *
-     * @return SerializerInterface
-     * @api
-     */
-    function getSerializer();
 }

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

@@ -18,7 +18,7 @@ use Symfony\Component\Serializer\SerializerInterface;
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class JsonEncoder extends AbstractEncoder
+class JsonEncoder extends AbstractEncoder implements DecoderInterface
 {
     /**
      * {@inheritdoc}

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

@@ -20,7 +20,7 @@ use Symfony\Component\Serializer\SerializerInterface;
  * @author John Wards <jwards@whiteoctober.co.uk>
  * @author Fabian Vogler <fabian@equivalence.ch>
  */
-class XmlEncoder extends AbstractEncoder
+class XmlEncoder extends AbstractEncoder implements DecoderInterface
 {
     private $dom;
     private $format;

+ 38 - 0
src/Symfony/Component/Serializer/SerializerAwareInterface.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Symfony\Component\Serializer;
+
+use Symfony\Component\Serializer\SerializerInterface;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Defines the interface of encoders
+ *
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+interface SerializerAwareInterface
+{
+    /**
+     * Sets the owning Serializer object
+     *
+     * @param SerializerInterface $serializer
+     * @api
+     */
+    function setSerializer(SerializerInterface $serializer);
+
+    /**
+     * Gets the owning Serializer object
+     *
+     * @return SerializerInterface
+     * @api
+     */
+    function getSerializer();
+}