Browse Source

put the custom normalizers first

Lukas Kahwe Smith 14 years ago
parent
commit
854f8c8c70

+ 5 - 5
Serializer/Serializer.php

@@ -73,10 +73,6 @@ class Serializer implements SerializerInterface
      */
     public final function normalize($data, $format = null)
     {
-        if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
-            return $this->nativePhpTypeNormalizer->normalize($data, $format);
-        }
-
         if ($this->customObjectNormalizers) {
             foreach ($this->customObjectNormalizers as $normalizer) {
                 if ($normalizer->supportsNormalization($data, $format)) {
@@ -85,6 +81,10 @@ class Serializer implements SerializerInterface
             }
         }
 
+        if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
+            return $this->nativePhpTypeNormalizer->normalize($data, $format);
+        }
+
         return $this->defaultObjectNormalizer->normalize($data, $format);
     }
 
@@ -152,4 +152,4 @@ class Serializer implements SerializerInterface
 
         return $this->encoderMap[$format];
     }
-}
+}

+ 72 - 0
Tests/Fixtures/AuthorList.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+/**
+ * An array-acting object that holds many author instances.
+ */
+class AuthorList implements \IteratorAggregate, \Countable, \ArrayAccess
+{
+    protected $authors = array();
+
+    /**
+     * @param Author $author
+     */
+    public function add(Author $author)
+    {
+        $this->authors[] = $author;
+    }
+
+    /**
+     * @see IteratorAggregate
+     */
+    public function getIterator()
+    {
+        return new \ArrayIterator($this->authors);
+    }
+
+    /**
+     * @see Countable
+     */
+    public function count()
+    {
+        return count($this->authors);
+    }
+
+    /**
+     * @see ArrayAccess
+     */
+    public function offsetExists($offset)
+    {
+        return isset($this->authors[$offset]);
+    }
+
+    /**
+     * @see ArrayAccess
+     */
+    public function offsetGet($offset)
+    {
+        return isset($this->authors[$offset]) ? $this->authors[$offset] : null;
+    }
+
+    /**
+     * @see ArrayAccess
+     */
+    public function offsetSet($offset, $value)
+    {
+        if (null === $offset) {
+            $this->authors[] = $value;
+        } else {
+            $this->authors[$offset] = $value;
+        }
+    }
+
+    /**
+     * @see ArrayAccess
+     */
+    public function offsetUnset($offset)
+    {
+        unset($this->authors[$offset]);
+    }
+    
+}

+ 40 - 0
Tests/Fixtures/NoopNormalizer.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Serializer\Normalizer\NormalizerInterface;
+
+class NoopNormalizer implements NormalizerInterface
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function normalize($object, $format = null)
+    {
+        return array();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function denormalize($data, $class, $format = null)
+    {
+        throw new \BadMethodCallException('Not supported');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function supportsNormalization($data, $format = null)
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function supportsDenormalization($data, $type, $format = null)
+    {
+        return false;
+    }
+}

+ 12 - 1
Tests/Serializer/SerializerTest.php

@@ -10,7 +10,9 @@ use JMS\SerializerBundle\Serializer\Exclusion\AllExclusionStrategy;
 use JMS\SerializerBundle\Serializer\Normalizer\PropertyBasedNormalizer;
 use JMS\SerializerBundle\Tests\Fixtures\Comment;
 use JMS\SerializerBundle\Tests\Fixtures\Author;
+use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
 use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
+use JMS\SerializerBundle\Tests\Fixtures\NoopNormalizer;
 use JMS\SerializerBundle\Serializer\Normalizer\ArrayCollectionNormalizer;
 use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
 use JMS\SerializerBundle\Serializer\SerializerFactory;
@@ -43,6 +45,15 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
             ),
             'comments' => array(),
         ), $normalized);
+
+        $noop = new NoopNormalizer();
+        $serializer = $this->getSerializer(null, null, array($noop));
+
+        $list = new AuthorList();
+        $list->add(new Author('Bar'));
+        $normalized = $serializer->normalize($list);
+
+        $this->assertEquals(array(), $normalized);
     }
 
     public function testDenormalize()
@@ -106,4 +117,4 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
             $encoders
         );
     }
-}
+}