소스 검색

[Serializer] NormalizableInterface now takes a Serializer and make sure the is always optional

Jordi Boggiano 14 년 전
부모
커밋
f8447aa74c

+ 4 - 4
src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php

@@ -16,14 +16,14 @@ use Symfony\Component\Serializer\SerializerInterface;
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class CustomNormalizer implements NormalizerInterface
+class CustomNormalizer extends SerializerAwareNormalizer
 {
     /**
      * {@inheritdoc}
      */
-    public function normalize($object, $format)
+    public function normalize($object, $format = null)
     {
-        return $object->normalize($this, $format);
+        return $object->normalize($this->serializer, $format);
     }
 
     /**
@@ -32,7 +32,7 @@ class CustomNormalizer implements NormalizerInterface
     public function denormalize($data, $class, $format = null)
     {
         $object = new $class;
-        $object->denormalize($this, $data, $format);
+        $object->denormalize($this->serializer, $data, $format);
         return $object;
     }
 

+ 1 - 1
src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

@@ -38,7 +38,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer
     /**
      * {@inheritdoc}
      */
-    public function normalize($object, $format)
+    public function normalize($object, $format = null)
     {
         $reflectionObject = new \ReflectionObject($object);
         $reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);

+ 8 - 8
src/Symfony/Component/Serializer/Normalizer/NormalizableInterface.php

@@ -2,6 +2,8 @@
 
 namespace Symfony\Component\Serializer\Normalizer;
 
+use Symfony\Component\Serializer\SerializerInterface;
+
 /*
  * This file is part of the Symfony framework.
  *
@@ -27,14 +29,13 @@ interface NormalizableInterface
      * It is important to understand that the normalize() call should normalize
      * recursively all child objects of the implementor.
      *
-     * @param NormalizerInterface $normalizer The normalizer is given so that you
-     *   can use it to normalize objects contained within this object, eventually
-     *   grabbing the serializer from it to access other normalizers.
+     * @param SerializerInterface $serializer The serializer is given so that you
+     *   can use it to normalize objects contained within this object.
      * @param string|null $format The format is optionally given to be able to normalize differently
      *   based on different output formats.
      * @return array|scalar
      */
-    function normalize(NormalizerInterface $normalizer, $format);
+    function normalize(SerializerInterface $serializer, $format = null);
 
     /**
      * Denormalizes the object back from an array of scalars|arrays.
@@ -42,12 +43,11 @@ interface NormalizableInterface
      * It is important to understand that the normalize() call should denormalize
      * recursively all child objects of the implementor.
      *
-     * @param NormalizerInterface $normalizer The normalizer is given so that you
-     *   can use it to denormalize objects contained within this object, eventually
-     *   grabbing the serializer from it to access other normalizers.
+     * @param SerializerInterface $serializer The serializer is given so that you
+     *   can use it to denormalize objects contained within this object.
      * @param array|scalar $data The data from which to re-create the object.
      * @param string|null $format The format is optionally given to be able to denormalize differently
      *   based on different input formats.
      */
-    function denormalize(NormalizerInterface $normalizer, $data, $format = null);
+    function denormalize(SerializerInterface $serializer, $data, $format = null);
 }

+ 1 - 1
src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php

@@ -28,7 +28,7 @@ interface NormalizerInterface
      * @return array|scalar
      * @api
      */
-    function normalize($object, $format);
+    function normalize($object, $format = null);
 
     /**
      * Denormalizes data back into an object of the given class

+ 2 - 2
src/Symfony/Component/Serializer/Serializer.php

@@ -59,7 +59,7 @@ class Serializer implements SerializerInterface
     /**
      * {@inheritdoc}
      */
-    public function normalizeObject($object, $format)
+    public function normalizeObject($object, $format = null)
     {
         if (!$this->normalizers) {
             throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
@@ -102,7 +102,7 @@ class Serializer implements SerializerInterface
      */
     public function normalize($data, $format = null)
     {
-        if (null === $value || is_scalar($value)) {
+        if (null === $data || is_scalar($data)) {
             return $data;
         }
         if ($data instanceof Traversable) {

+ 3 - 3
tests/Symfony/Tests/Component/Serializer/Fixtures/Dummy.php

@@ -3,7 +3,7 @@
 namespace Symfony\Tests\Component\Serializer\Fixtures;
 
 use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
-use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
+use Symfony\Component\Serializer\SerializerInterface;
 
 class Dummy implements NormalizableInterface
 {
@@ -12,7 +12,7 @@ class Dummy implements NormalizableInterface
     public $baz;
     public $qux;
 
-    public function normalize(NormalizerInterface $normalizer, $format, $properties = null)
+    public function normalize(SerializerInterface $serializer, $format = null)
     {
         return array(
             'foo' => $this->foo,
@@ -22,7 +22,7 @@ class Dummy implements NormalizableInterface
         );
     }
 
-    public function denormalize(NormalizerInterface $normalizer, $data, $format = null)
+    public function denormalize(SerializerInterface $serializer, $data, $format = null)
     {
         $this->foo = $data['foo'];
         $this->bar = $data['bar'];

+ 3 - 3
tests/Symfony/Tests/Component/Serializer/Fixtures/ScalarDummy.php

@@ -3,19 +3,19 @@
 namespace Symfony\Tests\Component\Serializer\Fixtures;
 
 use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
-use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
+use Symfony\Component\Serializer\SerializerInterface;
 
 class ScalarDummy implements NormalizableInterface
 {
     public $foo;
     public $xmlFoo;
 
-    public function normalize(NormalizerInterface $normalizer, $format, $properties = null)
+    public function normalize(SerializerInterface $serializer, $format = null)
     {
         return $format === 'xml' ? $this->xmlFoo : $this->foo;
     }
 
-    public function denormalize(NormalizerInterface $normalizer, $data, $format = null)
+    public function denormalize(SerializerInterface $serializer, $data, $format = null)
     {
         if ($format === 'xml') {
             $this->xmlFoo = $data;

+ 2 - 0
tests/Symfony/Tests/Component/Serializer/Normalizer/CustomNormalizerTest.php

@@ -6,6 +6,7 @@ require_once __DIR__.'/../Fixtures/ScalarDummy.php';
 
 use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
+use Symfony\Component\Serializer\Serializer;
 
 /*
  * This file is part of the Symfony framework.
@@ -21,6 +22,7 @@ class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
     public function setUp()
     {
         $this->normalizer = new CustomNormalizer;
+        $this->normalizer->setSerializer(new Serializer);
     }
 
     public function testSerialize()

+ 0 - 11
tests/Symfony/Tests/Component/Serializer/Normalizer/GetSetMethodNormalizerTest.php

@@ -33,17 +33,6 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testNormalizeRestricted()
-    {
-        $obj = new GetSetDummy;
-        $obj->setFoo('foo');
-        $obj->setBar('bar');
-        $this->assertEquals(
-            array('foo' => 'foo'),
-            $this->normalizer->normalize($obj, 'any', array('foo'))
-        );
-    }
-
     public function testDenormalize()
     {
         $obj = $this->normalizer->denormalize(