From ec1199eda7f756a4782d8bfc7bf94f039875365f Mon Sep 17 00:00:00 2001 From: Johannes Schmitt Date: Fri, 6 May 2011 13:01:19 +0200 Subject: [PATCH] [Serializer] updated SerializerInterface --- .../Component/Serializer/Serializer.php | 22 +++++- .../Serializer/SerializerInterface.php | 76 ++++--------------- 2 files changed, 36 insertions(+), 62 deletions(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index f7f012c110..183e17ef0f 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -46,7 +46,14 @@ class Serializer implements SerializerInterface */ public function serialize($data, $format) { - return $this->encode($data, $format); + return $this->encode($this->normalize($data, $format), $format); + } + + /** + * {@inheritDoc} + */ + public function deserialize($data, $type, $format) { + return $this->denormalize($this->decode($data, $format), $type, $format); } /** @@ -95,8 +102,11 @@ class Serializer implements SerializerInterface /** * {@inheritdoc} */ - public function normalize($data, $format) + public function normalize($data, $format = null) { + if (!$this->isStructuredType($data)) { + return $data; + } if (is_array($data)) { foreach ($data as $key => $val) { $data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val; @@ -109,6 +119,14 @@ class Serializer implements SerializerInterface throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true)); } + /** + * {@inheritDoc} + */ + public function denormalize($data, $type, $format = null) + { + return $this->denormalizeObject($data, $type, $format); + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Serializer/SerializerInterface.php b/src/Symfony/Component/Serializer/SerializerInterface.php index 7f557221c5..262e06a893 100644 --- a/src/Symfony/Component/Serializer/SerializerInterface.php +++ b/src/Symfony/Component/Serializer/SerializerInterface.php @@ -31,6 +31,15 @@ interface SerializerInterface */ function serialize($data, $format); + /** + * Deserializes data into the given type. + * + * @param mixed $data + * @param string $type + * @param string $format + */ + function deserialize($data, $type, $format); + /** * Normalizes any data into a set of arrays/scalars * @@ -39,27 +48,18 @@ interface SerializerInterface * @return array|scalar * @api */ - function normalize($data, $format); + function normalize($data, $format = null); /** - * Normalizes an object into a set of arrays/scalars + * Denormalizes data into the given type. * - * @param object $object object to normalize - * @param string $format format name, present to give the option to normalizers to act differently based on formats - * @param array $properties a list of properties to extract, if null all properties are returned - * @return array|scalar - */ - function normalizeObject($object, $format, $properties = null); - - /** - * Denormalizes data back into an object of the given class + * @param mixed $data + * @param string $type + * @param string $format * - * @param mixed $data data to restore - * @param string $class the expected class to instantiate - * @param string $format format name, present to give the option to normalizers to act differently based on formats - * @return object + * @return mixed */ - function denormalizeObject($data, $class, $format = null); + function denormalize($data, $type, $format = null); /** * Encodes data into the given format @@ -80,48 +80,4 @@ interface SerializerInterface * @api */ function decode($data, $format); - - /** - * @param NormalizerInterface $normalizer - */ - function addNormalizer(NormalizerInterface $normalizer); - - /** - * @return array[]NormalizerInterface - */ - function getNormalizers(); - - /** - * @param NormalizerInterface $normalizer - */ - function removeNormalizer(NormalizerInterface $normalizer); - - /** - * @param string $format format name - * @param EncoderInterface $encoder - */ - function setEncoder($format, EncoderInterface $encoder); - - /** - * @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); }