Pārlūkot izejas kodu

Merge branch 'serializerLight' of http://github.com/schmittjoh/symfony into serializer

Jordi Boggiano 14 gadi atpakaļ
vecāks
revīzija
d6afe63d2a

+ 20 - 2
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}
      */

+ 16 - 60
src/Symfony/Component/Serializer/SerializerInterface.php

@@ -32,34 +32,34 @@ interface SerializerInterface
     function serialize($data, $format);
 
     /**
-     * Normalizes any data into a set of arrays/scalars
+     * Deserializes data into the given type.
      *
-     * @param mixed $data data to normalize
-     * @param string $format format name, present to give the option to normalizers to act differently based on formats
-     * @return array|scalar
-     * @api
+     * @param mixed $data
+     * @param string $type
+     * @param string $format
      */
-    function normalize($data, $format);
+    function deserialize($data, $type, $format);
 
     /**
-     * Normalizes an object into a set of arrays/scalars
+     * Normalizes any data into a set of arrays/scalars
      *
-     * @param object $object object to normalize
+     * @param mixed $data data 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
+     * @api
      */
-    function normalizeObject($object, $format, $properties = null);
+    function normalize($data, $format = null);
 
     /**
-     * Denormalizes data back into an object of the given class
+     * Denormalizes data into the given type.
      *
-     * @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
+     * @param mixed $data
+     * @param string $type
+     * @param string $format
+     *
+     * @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);
 }