Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put the custom normalizers first #10

Merged
merged 2 commits into from
Jun 7, 2011
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public function __construct(NormalizerInterface $nativePhpNormalizer, Normalizer
*/
public final function normalize($data, $format = null)
{
if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
return $this->nativePhpTypeNormalizer->normalize($data, $format);
}

if ($this->customObjectNormalizers) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an "is_object" check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

foreach ($this->customObjectNormalizers as $normalizer) {
if ($normalizer->supportsNormalization($data, $format)) {
Expand All @@ -85,6 +81,10 @@ public final function normalize($data, $format = null)
}
}

if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
return $this->nativePhpTypeNormalizer->normalize($data, $format);
}

return $this->defaultObjectNormalizer->normalize($data, $format);
}

Expand Down Expand Up @@ -152,4 +152,4 @@ protected function getEncoder($format)

return $this->encoderMap[$format];
}
}
}
72 changes: 72 additions & 0 deletions Tests/Fixtures/AuthorList.php
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions Tests/Fixtures/NoopNormalizer.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 12 additions & 1 deletion Tests/Serializer/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
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;
Expand Down Expand Up @@ -43,6 +45,15 @@ public function testNormalize()
),
'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()
Expand Down Expand Up @@ -106,4 +117,4 @@ private function getSerializer($propertyNamingStrategy = null, $encoders = null,
$encoders
);
}
}
}