Skip to content

Commit

Permalink
[Serializer] NormalizableInterface now takes a Serializer and make su…
Browse files Browse the repository at this point in the history
…re the is always optional
  • Loading branch information
Seldaek committed May 9, 2011
1 parent 3b88608 commit f8447aa
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 33 deletions.
Expand Up @@ -16,14 +16,14 @@
/**
* @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);
}

/**
Expand All @@ -32,7 +32,7 @@ public function normalize($object, $format)
public function denormalize($data, $class, $format = null)
{
$object = new $class;
$object->denormalize($this, $data, $format);
$object->denormalize($this->serializer, $data, $format);
return $object;
}

Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\SerializerInterface;

/*
* This file is part of the Symfony framework.
*
Expand All @@ -27,27 +29,25 @@ 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.
*
* 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);
}
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -59,7 +59,7 @@ public function deserialize($data, $type, $format) {
/**
* {@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.');
Expand Down Expand Up @@ -102,7 +102,7 @@ public function denormalizeObject($data, $class, $format = null)
*/
public function normalize($data, $format = null)
{
if (null === $value || is_scalar($value)) {
if (null === $data || is_scalar($data)) {
return $data;
}
if ($data instanceof Traversable) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Symfony/Tests/Component/Serializer/Fixtures/Dummy.php
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -22,7 +22,7 @@ public function normalize(NormalizerInterface $normalizer, $format, $properties
);
}

public function denormalize(NormalizerInterface $normalizer, $data, $format = null)
public function denormalize(SerializerInterface $serializer, $data, $format = null)
{
$this->foo = $data['foo'];
$this->bar = $data['bar'];
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -6,6 +6,7 @@

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.
Expand All @@ -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()
Expand Down
Expand Up @@ -33,17 +33,6 @@ public function testNormalize()
);
}

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(
Expand Down

0 comments on commit f8447aa

Please sign in to comment.