diff --git a/Encoder/DecoderInterface.php b/Encoder/DecoderInterface.php index fb67f2969..f109753d6 100644 --- a/Encoder/DecoderInterface.php +++ b/Encoder/DecoderInterface.php @@ -13,7 +13,7 @@ */ /** - * Defines the interface of encoders that are able to decode their own format + * Defines the interface of decoders * * @author Jordi Boggiano */ @@ -22,9 +22,17 @@ interface DecoderInterface /** * Decodes a string into PHP data * - * @param string $data data to decode - * @param string $format format to decode from + * @param scalar $data data to decode + * @param string $format format name * @return mixed */ function decode($data, $format); + + /** + * Checks whether the serializer can decode from given format + * + * @param string $format format name + * @return Boolean + */ + function supportsDecoding($format); } diff --git a/Encoder/EncoderInterface.php b/Encoder/EncoderInterface.php index 11ce30fb4..7d63e7cae 100644 --- a/Encoder/EncoderInterface.php +++ b/Encoder/EncoderInterface.php @@ -2,7 +2,6 @@ namespace Symfony\Component\Serializer\Encoder; - /* * This file is part of the Symfony framework. * @@ -20,11 +19,19 @@ interface EncoderInterface { /** - * Encodes data into a string + * Encodes data into the given format * * @param mixed $data data to encode - * @param string $format format to encode to - * @return string + * @param string $format format name + * @return scalar */ function encode($data, $format); + + /** + * Checks whether the serializer can encode to given format + * + * @param string $format format name + * @return Boolean + */ + function supportsEncoding($format); } diff --git a/Encoder/JsonEncoder.php b/Encoder/JsonEncoder.php index 2e874eb61..620d749e7 100644 --- a/Encoder/JsonEncoder.php +++ b/Encoder/JsonEncoder.php @@ -34,4 +34,26 @@ public function decode($data, $format) { return json_decode($data, true); } + + /** + * Checks whether the serializer can encode to given format + * + * @param string $format format name + * @return Boolean + */ + public function supportsEncoding($format) + { + return 'json' === $format; + } + + /** + * Checks whether the serializer can decode from given format + * + * @param string $format format name + * @return Boolean + */ + public function supportsDecoding($format) + { + return 'json' === $format; + } } diff --git a/Encoder/XmlEncoder.php b/Encoder/XmlEncoder.php index 7fbde8e61..3a61c1a7b 100644 --- a/Encoder/XmlEncoder.php +++ b/Encoder/XmlEncoder.php @@ -71,6 +71,28 @@ public function decode($data, $format) return $this->parseXml($xml); } + /** + * Checks whether the serializer can encode to given format + * + * @param string $format format name + * @return Boolean + */ + public function supportsEncoding($format) + { + return 'xml' === $format; + } + + /** + * Checks whether the serializer can decode from given format + * + * @param string $format format name + * @return Boolean + */ + public function supportsDecoding($format) + { + return 'xml' === $format; + } + /** * Sets the root node name * @param string $name root node name diff --git a/EncoderInterface.php b/EncoderInterface.php deleted file mode 100644 index e4d79475c..000000000 --- a/EncoderInterface.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -/** - * Defines the interface of the Encoder - * - * @author Jordi Boggiano - */ -interface EncoderInterface -{ - /** - * Encodes data into the given format - * - * @param mixed $data data to encode - * @param string $format format name - * @return array|scalar - */ - function encode($data, $format); - - /** - * Decodes a string from the given format back into PHP data - * - * @param string $data data to decode - * @param string $format format name - * @return mixed - */ - function decode($data, $format); - - /** - * Checks whether the serializer can encode to given format - * - * @param string $format format name - * @return Boolean - */ - function supportsEncoding($format); - - /** - * Checks whether the serializer can decode from given format - * - * @param string $format format name - * @return Boolean - */ - function supportsDecoding($format); - - /** - * Get the encoder for the given format - * - * @return EncoderInterface - */ - function getEncoder($format); -} diff --git a/NormalizerInterface.php b/NormalizerInterface.php index 706bed60a..d1fbe719f 100644 --- a/NormalizerInterface.php +++ b/NormalizerInterface.php @@ -2,8 +2,6 @@ namespace Symfony\Component\Serializer; -use Symfony\Component\Serializer\Encoder\EncoderInterface; - /* * This file is part of the Symfony framework. * @@ -14,7 +12,7 @@ */ /** - * Defines the interface of the Normalizer + * Defines the interface of Normalizers * * @author Jordi Boggiano */ diff --git a/Serializer.php b/Serializer.php index 4c60942ba..8c861bd3a 100644 --- a/Serializer.php +++ b/Serializer.php @@ -32,7 +32,7 @@ * @author Johannes M. Schmitt * @author Lukas Kahwe Smith */ -class Serializer implements SerializerInterface, NormalizerInterface, EncoderInterface +class Serializer implements SerializerInterface, NormalizerInterface, EncoderInterface, DecoderInterface { protected $normalizers = array(); protected $encoders = array();