Skip to content

Commit

Permalink
merged Seldaek/serializer_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 11, 2011
2 parents cdf6858 + e88f2a9 commit 1097b59
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 153 deletions.
2 changes: 1 addition & 1 deletion Encoder/DecoderInterface.php
Expand Up @@ -14,7 +14,7 @@
*/

/**
* Defines the interface of encoders
* Defines the interface of encoders that are able to decode their own format
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
Expand Down
5 changes: 1 addition & 4 deletions Encoder/JsonEncoder.php
Expand Up @@ -18,16 +18,13 @@
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class JsonEncoder extends AbstractEncoder implements DecoderInterface
class JsonEncoder implements EncoderInterface, DecoderInterface
{
/**
* {@inheritdoc}
*/
public function encode($data, $format)
{
if ($this->serializer->isStructuredType($data)) {
$data = $this->serializer->normalize($data, $format);
}
return json_encode($data);
}

Expand Down
26 changes: 26 additions & 0 deletions Encoder/NormalizationAwareInterface.php
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\SerializerInterface;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Defines the interface of encoders that will normalize data themselves
*
* Implementing this interface essentially just tells the Serializer that the
* data should not be pre-normalized before being passed to this Encoder.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
interface NormalizationAwareInterface
{
}
12 changes: 2 additions & 10 deletions Encoder/AbstractEncoder.php → Encoder/SerializerAwareEncoder.php
Expand Up @@ -15,11 +15,11 @@
*/

/**
* Abstract Encoder implementation
* SerializerAware Encoder implementation
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
abstract class AbstractEncoder implements SerializerAwareInterface, EncoderInterface
abstract class SerializerAwareEncoder implements SerializerAwareInterface, EncoderInterface
{
protected $serializer;

Expand All @@ -30,12 +30,4 @@ public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* {@inheritdoc}
*/
public function getSerializer()
{
return $this->serializer;
}
}
22 changes: 11 additions & 11 deletions Encoder/XmlEncoder.php
Expand Up @@ -20,7 +20,7 @@
* @author John Wards <jwards@whiteoctober.co.uk>
* @author Fabian Vogler <fabian@equivalence.ch>
*/
class XmlEncoder extends AbstractEncoder implements DecoderInterface
class XmlEncoder extends SerializerAwareEncoder implements DecoderInterface, NormalizationAwareInterface
{
private $dom;
private $format;
Expand All @@ -38,7 +38,7 @@ public function encode($data, $format)
$this->dom = new \DOMDocument();
$this->format = $format;

if ($this->serializer->isStructuredType($data)) {
if (null !== $data && !is_scalar($data)) {
$root = $this->dom->createElement($this->rootNodeName);
$this->dom->appendChild($root);
$this->buildXml($root, $data);
Expand Down Expand Up @@ -248,16 +248,16 @@ private function buildXml($parentNode, $data)
}
if (is_object($data)) {
$data = $this->serializer->normalizeObject($data, $this->format);
if (!$this->serializer->isStructuredType($data)) {
// top level data object is normalized into a scalar
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root->removeChild($parentNode);
return $this->appendNode($root, $data, $this->rootNodeName);
}
return $this->appendNode($parentNode, $data, 'data');
if (null !== $data && !is_scalar($data)) {
return $this->buildXml($parentNode, $data);
}
// top level data object was normalized into a scalar
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root->removeChild($parentNode);
return $this->appendNode($root, $data, $this->rootNodeName);
}
return $this->buildXml($parentNode, $data);
return $this->appendNode($parentNode, $data, 'data');
}
throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
}
Expand Down
8 changes: 4 additions & 4 deletions Normalizer/CustomNormalizer.php
Expand Up @@ -16,14 +16,14 @@
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class CustomNormalizer extends AbstractNormalizer
class CustomNormalizer extends SerializerAwareNormalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format, $properties = null)
public function normalize($object, $format = null)
{
return $object->normalize($this, $format, $properties);
return $object->normalize($this->serializer, $format);
}

/**
Expand All @@ -32,7 +32,7 @@ public function normalize($object, $format, $properties = null)
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
18 changes: 7 additions & 11 deletions Normalizer/GetSetMethodNormalizer.php
Expand Up @@ -33,15 +33,13 @@
*
* @author Nils Adermann <naderman@naderman.de>
*/
class GetSetMethodNormalizer extends AbstractNormalizer
class GetSetMethodNormalizer extends SerializerAwareNormalizer
{
/**
* {@inheritdoc}
*/
public function normalize($object, $format, $properties = null)
public function normalize($object, $format = null)
{
$propertyMap = (null === $properties) ? null : array_flip(array_map('strtolower', $properties));

$reflectionObject = new \ReflectionObject($object);
$reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);

Expand All @@ -50,14 +48,12 @@ public function normalize($object, $format, $properties = null)
if ($this->isGetMethod($method)) {
$attributeName = strtolower(substr($method->getName(), 3));

if (null === $propertyMap || isset($propertyMap[$attributeName])) {
$attributeValue = $method->invoke($object);
if ($this->serializer->isStructuredType($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format);
}

$attributes[$attributeName] = $attributeValue;
$attributeValue = $method->invoke($object);
if (null !== $attributeValue && !is_scalar($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format);
}

$attributes[$attributeName] = $attributeValue;
}
}

Expand Down
18 changes: 8 additions & 10 deletions Normalizer/NormalizableInterface.php
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,29 +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.
* @param array|null $properties If provided, this is a (subset) list of
* properties that should be exported from the object.
* @return array|scalar
*/
function normalize(NormalizerInterface $normalizer, $format, $properties = null);
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);
}
19 changes: 1 addition & 18 deletions Normalizer/NormalizerInterface.php
Expand Up @@ -25,11 +25,10 @@ interface NormalizerInterface
*
* @param object $object object to normalize
* @param string $format format the normalization result will be encoded as
* @param array $properties a list of properties to extract, if null all properties are returned
* @return array|scalar
* @api
*/
function normalize($object, $format, $properties = null);
function normalize($object, $format = null);

/**
* Denormalizes data back into an object of the given class
Expand Down Expand Up @@ -62,20 +61,4 @@ function supportsNormalization($data, $format = null);
* @api
*/
function supportsDenormalization($data, $type, $format = null);

/**
* Sets the owning Serializer object
*
* @param SerializerInterface $serializer
* @api
*/
function setSerializer(SerializerInterface $serializer);

/**
* Gets the owning Serializer object
*
* @return SerializerInterface
* @api
*/
function getSerializer();
}
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;

/*
* This file is part of the Symfony framework.
Expand All @@ -14,11 +15,11 @@
*/

/**
* Abstract Normalizer implementation
* SerializerAware Normalizer implementation
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
abstract class AbstractNormalizer implements NormalizerInterface
abstract class SerializerAwareNormalizer implements SerializerAwareInterface, NormalizerInterface
{
protected $serializer;

Expand All @@ -29,12 +30,4 @@ public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* {@inheritdoc}
*/
public function getSerializer()
{
return $this->serializer;
}
}

0 comments on commit 1097b59

Please sign in to comment.