merged Seldaek/serializer_fix
This commit is contained in:
commit
9abd53c2b4
@ -134,6 +134,11 @@ beta1 to beta2
|
|||||||
* ParameterBag::getDeep() has been removed, and is replaced with a boolean flag
|
* ParameterBag::getDeep() has been removed, and is replaced with a boolean flag
|
||||||
on the ParameterBag::get() method.
|
on the ParameterBag::get() method.
|
||||||
|
|
||||||
|
* Serializer: `AbstractEncoder` & `AbstractNormalizer` were renamed to
|
||||||
|
`SerializerAwareEncoder` & `SerializerAwareNormalizer`.
|
||||||
|
|
||||||
|
* Serializer: The `$properties` argument has been dropped from all interfaces.
|
||||||
|
|
||||||
PR12 to beta1
|
PR12 to beta1
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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>
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
|
@ -18,16 +18,13 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
*
|
*
|
||||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
class JsonEncoder extends AbstractEncoder implements DecoderInterface
|
class JsonEncoder implements EncoderInterface, DecoderInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function encode($data, $format)
|
public function encode($data, $format)
|
||||||
{
|
{
|
||||||
if ($this->serializer->isStructuredType($data)) {
|
|
||||||
$data = $this->serializer->normalize($data, $format);
|
|
||||||
}
|
|
||||||
return json_encode($data);
|
return json_encode($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
src/Symfony/Component/Serializer/Encoder/NormalizationAwareInterface.php
Executable file
26
src/Symfony/Component/Serializer/Encoder/NormalizationAwareInterface.php
Executable file
@ -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
|
||||||
|
{
|
||||||
|
}
|
@ -15,11 +15,11 @@ use Symfony\Component\Serializer\SerializerAwareInterface;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract Encoder implementation
|
* SerializerAware Encoder implementation
|
||||||
*
|
*
|
||||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
abstract class AbstractEncoder implements SerializerAwareInterface, EncoderInterface
|
abstract class SerializerAwareEncoder implements SerializerAwareInterface, EncoderInterface
|
||||||
{
|
{
|
||||||
protected $serializer;
|
protected $serializer;
|
||||||
|
|
||||||
@ -30,12 +30,4 @@ abstract class AbstractEncoder implements SerializerAwareInterface, EncoderInter
|
|||||||
{
|
{
|
||||||
$this->serializer = $serializer;
|
$this->serializer = $serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getSerializer()
|
|
||||||
{
|
|
||||||
return $this->serializer;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -20,7 +20,7 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
* @author John Wards <jwards@whiteoctober.co.uk>
|
* @author John Wards <jwards@whiteoctober.co.uk>
|
||||||
* @author Fabian Vogler <fabian@equivalence.ch>
|
* @author Fabian Vogler <fabian@equivalence.ch>
|
||||||
*/
|
*/
|
||||||
class XmlEncoder extends AbstractEncoder implements DecoderInterface
|
class XmlEncoder extends SerializerAwareEncoder implements DecoderInterface, NormalizationAwareInterface
|
||||||
{
|
{
|
||||||
private $dom;
|
private $dom;
|
||||||
private $format;
|
private $format;
|
||||||
@ -38,7 +38,7 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
|
|||||||
$this->dom = new \DOMDocument();
|
$this->dom = new \DOMDocument();
|
||||||
$this->format = $format;
|
$this->format = $format;
|
||||||
|
|
||||||
if ($this->serializer->isStructuredType($data)) {
|
if (null !== $data && !is_scalar($data)) {
|
||||||
$root = $this->dom->createElement($this->rootNodeName);
|
$root = $this->dom->createElement($this->rootNodeName);
|
||||||
$this->dom->appendChild($root);
|
$this->dom->appendChild($root);
|
||||||
$this->buildXml($root, $data);
|
$this->buildXml($root, $data);
|
||||||
@ -248,16 +248,16 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
|
|||||||
}
|
}
|
||||||
if (is_object($data)) {
|
if (is_object($data)) {
|
||||||
$data = $this->serializer->normalizeObject($data, $this->format);
|
$data = $this->serializer->normalizeObject($data, $this->format);
|
||||||
if (!$this->serializer->isStructuredType($data)) {
|
if (null !== $data && !is_scalar($data)) {
|
||||||
// top level data object is normalized into a scalar
|
return $this->buildXml($parentNode, $data);
|
||||||
if (!$parentNode->parentNode->parentNode) {
|
|
||||||
$root = $parentNode->parentNode;
|
|
||||||
$root->removeChild($parentNode);
|
|
||||||
return $this->appendNode($root, $data, $this->rootNodeName);
|
|
||||||
}
|
|
||||||
return $this->appendNode($parentNode, $data, '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->appendNode($parentNode, $data, 'data');
|
||||||
}
|
}
|
||||||
throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
|
throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,14 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
/**
|
/**
|
||||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
class CustomNormalizer extends AbstractNormalizer
|
class CustomNormalizer extends SerializerAwareNormalizer
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,7 +32,7 @@ class CustomNormalizer extends AbstractNormalizer
|
|||||||
public function denormalize($data, $class, $format = null)
|
public function denormalize($data, $class, $format = null)
|
||||||
{
|
{
|
||||||
$object = new $class;
|
$object = new $class;
|
||||||
$object->denormalize($this, $data, $format);
|
$object->denormalize($this->serializer, $data, $format);
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,15 +33,13 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
*
|
*
|
||||||
* @author Nils Adermann <naderman@naderman.de>
|
* @author Nils Adermann <naderman@naderman.de>
|
||||||
*/
|
*/
|
||||||
class GetSetMethodNormalizer extends AbstractNormalizer
|
class GetSetMethodNormalizer extends SerializerAwareNormalizer
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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);
|
$reflectionObject = new \ReflectionObject($object);
|
||||||
$reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
|
$reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
|
|
||||||
@ -50,14 +48,12 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
|||||||
if ($this->isGetMethod($method)) {
|
if ($this->isGetMethod($method)) {
|
||||||
$attributeName = strtolower(substr($method->getName(), 3));
|
$attributeName = strtolower(substr($method->getName(), 3));
|
||||||
|
|
||||||
if (null === $propertyMap || isset($propertyMap[$attributeName])) {
|
$attributeValue = $method->invoke($object);
|
||||||
$attributeValue = $method->invoke($object);
|
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||||
if ($this->serializer->isStructuredType($attributeValue)) {
|
$attributeValue = $this->serializer->normalize($attributeValue, $format);
|
||||||
$attributeValue = $this->serializer->normalize($attributeValue, $format);
|
|
||||||
}
|
|
||||||
|
|
||||||
$attributes[$attributeName] = $attributeValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$attributes[$attributeName] = $attributeValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Normalizer;
|
namespace Symfony\Component\Serializer\Normalizer;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Symfony framework.
|
* This file is part of the Symfony framework.
|
||||||
*
|
*
|
||||||
@ -27,16 +29,13 @@ interface NormalizableInterface
|
|||||||
* It is important to understand that the normalize() call should normalize
|
* It is important to understand that the normalize() call should normalize
|
||||||
* recursively all child objects of the implementor.
|
* recursively all child objects of the implementor.
|
||||||
*
|
*
|
||||||
* @param NormalizerInterface $normalizer The normalizer is given so that you
|
* @param SerializerInterface $serializer The serializer is given so that you
|
||||||
* can use it to normalize objects contained within this object, eventually
|
* can use it to normalize objects contained within this object.
|
||||||
* grabbing the serializer from it to access other normalizers.
|
|
||||||
* @param string|null $format The format is optionally given to be able to normalize differently
|
* @param string|null $format The format is optionally given to be able to normalize differently
|
||||||
* based on different output formats.
|
* 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
|
* @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.
|
* Denormalizes the object back from an array of scalars|arrays.
|
||||||
@ -44,12 +43,11 @@ interface NormalizableInterface
|
|||||||
* It is important to understand that the normalize() call should denormalize
|
* It is important to understand that the normalize() call should denormalize
|
||||||
* recursively all child objects of the implementor.
|
* recursively all child objects of the implementor.
|
||||||
*
|
*
|
||||||
* @param NormalizerInterface $normalizer The normalizer is given so that you
|
* @param SerializerInterface $serializer The serializer is given so that you
|
||||||
* can use it to denormalize objects contained within this object, eventually
|
* can use it to denormalize objects contained within this object.
|
||||||
* grabbing the serializer from it to access other normalizers.
|
|
||||||
* @param array|scalar $data The data from which to re-create the 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
|
* @param string|null $format The format is optionally given to be able to denormalize differently
|
||||||
* based on different input formats.
|
* based on different input formats.
|
||||||
*/
|
*/
|
||||||
function denormalize(NormalizerInterface $normalizer, $data, $format = null);
|
function denormalize(SerializerInterface $serializer, $data, $format = null);
|
||||||
}
|
}
|
||||||
|
@ -25,11 +25,10 @@ interface NormalizerInterface
|
|||||||
*
|
*
|
||||||
* @param object $object object to normalize
|
* @param object $object object to normalize
|
||||||
* @param string $format format the normalization result will be encoded as
|
* @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
|
* @return array|scalar
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
function normalize($object, $format, $properties = null);
|
function normalize($object, $format = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Denormalizes data back into an object of the given class
|
* Denormalizes data back into an object of the given class
|
||||||
@ -62,20 +61,4 @@ interface NormalizerInterface
|
|||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
function supportsDenormalization($data, $type, $format = null);
|
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();
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Symfony\Component\Serializer\Normalizer;
|
namespace Symfony\Component\Serializer\Normalizer;
|
||||||
|
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Symfony framework.
|
* This file is part of the Symfony framework.
|
||||||
@ -14,11 +15,11 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract Normalizer implementation
|
* SerializerAware Normalizer implementation
|
||||||
*
|
*
|
||||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
abstract class AbstractNormalizer implements NormalizerInterface
|
abstract class SerializerAwareNormalizer implements SerializerAwareInterface, NormalizerInterface
|
||||||
{
|
{
|
||||||
protected $serializer;
|
protected $serializer;
|
||||||
|
|
||||||
@ -29,12 +30,4 @@ abstract class AbstractNormalizer implements NormalizerInterface
|
|||||||
{
|
{
|
||||||
$this->serializer = $serializer;
|
$this->serializer = $serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getSerializer()
|
|
||||||
{
|
|
||||||
return $this->serializer;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,6 +4,8 @@ namespace Symfony\Component\Serializer;
|
|||||||
|
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
||||||
|
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
||||||
|
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Symfony framework.
|
* This file is part of the Symfony framework.
|
||||||
@ -30,24 +32,22 @@ class Serializer implements SerializerInterface
|
|||||||
{
|
{
|
||||||
private $normalizers = array();
|
private $normalizers = array();
|
||||||
private $encoders = array();
|
private $encoders = array();
|
||||||
|
private $decoders = array();
|
||||||
protected $normalizerCache = array();
|
protected $normalizerCache = array();
|
||||||
protected $denormalizerCache = array();
|
protected $denormalizerCache = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $value value to test
|
|
||||||
* @return Boolean whether the type is a structured type (array + objects)
|
|
||||||
*/
|
|
||||||
public function isStructuredType($value)
|
|
||||||
{
|
|
||||||
return null !== $value && !is_scalar($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function serialize($data, $format)
|
public function serialize($data, $format)
|
||||||
{
|
{
|
||||||
return $this->encode($this->normalize($data, $format), $format);
|
if (!isset($this->encoders[$format])) {
|
||||||
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
||||||
|
}
|
||||||
|
if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
|
||||||
|
$data = $this->normalize($data);
|
||||||
|
}
|
||||||
|
return $this->encode($data, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,53 +57,12 @@ class Serializer implements SerializerInterface
|
|||||||
return $this->denormalize($this->decode($data, $format), $type, $format);
|
return $this->denormalize($this->decode($data, $format), $type, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function normalizeObject($object, $format, $properties = null)
|
|
||||||
{
|
|
||||||
if (!$this->normalizers) {
|
|
||||||
throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
|
|
||||||
}
|
|
||||||
$class = get_class($object);
|
|
||||||
if (isset($this->normalizerCache[$class][$format])) {
|
|
||||||
return $this->normalizerCache[$class][$format]->normalize($object, $format, $properties);
|
|
||||||
}
|
|
||||||
foreach ($this->normalizers as $normalizer) {
|
|
||||||
if ($normalizer->supportsNormalization($object, $class, $format)) {
|
|
||||||
$this->normalizerCache[$class][$format] = $normalizer;
|
|
||||||
return $normalizer->normalize($object, $format, $properties);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function denormalizeObject($data, $class, $format = null)
|
|
||||||
{
|
|
||||||
if (!$this->normalizers) {
|
|
||||||
throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
|
||||||
}
|
|
||||||
if (isset($this->denormalizerCache[$class][$format])) {
|
|
||||||
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
|
|
||||||
}
|
|
||||||
foreach ($this->normalizers as $normalizer) {
|
|
||||||
if ($normalizer->supportsDenormalization($class, $format)) {
|
|
||||||
$this->denormalizerCache[$class][$format] = $normalizer;
|
|
||||||
return $normalizer->denormalize($data, $class, $format);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function normalize($data, $format = null)
|
public function normalize($data, $format = null)
|
||||||
{
|
{
|
||||||
if (!$this->isStructuredType($data)) {
|
if (null === $data || is_scalar($data)) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
if ($data instanceof Traversable) {
|
if ($data instanceof Traversable) {
|
||||||
@ -138,7 +97,7 @@ class Serializer implements SerializerInterface
|
|||||||
*/
|
*/
|
||||||
public function encode($data, $format)
|
public function encode($data, $format)
|
||||||
{
|
{
|
||||||
if (!$this->hasEncoder($format)) {
|
if (!isset($this->encoders[$format])) {
|
||||||
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
||||||
}
|
}
|
||||||
return $this->encoders[$format]->encode($data, $format);
|
return $this->encoders[$format]->encode($data, $format);
|
||||||
@ -149,23 +108,75 @@ class Serializer implements SerializerInterface
|
|||||||
*/
|
*/
|
||||||
public function decode($data, $format)
|
public function decode($data, $format)
|
||||||
{
|
{
|
||||||
if (!$this->hasEncoder($format)) {
|
if (!isset($this->decoders[$format])) {
|
||||||
throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format');
|
throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
|
||||||
}
|
}
|
||||||
return $this->encoders[$format]->decode($data, $format);
|
return $this->decoders[$format]->decode($data, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* Normalizes an object into a set of arrays/scalars
|
||||||
|
*
|
||||||
|
* @param object $object object to normalize
|
||||||
|
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||||
|
* @return array|scalar
|
||||||
|
*/
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
$class = get_class($object);
|
||||||
|
if (isset($this->normalizerCache[$class][$format])) {
|
||||||
|
return $this->normalizerCache[$class][$format]->normalize($object, $format);
|
||||||
|
}
|
||||||
|
foreach ($this->normalizers as $normalizer) {
|
||||||
|
if ($normalizer->supportsNormalization($object, $class, $format)) {
|
||||||
|
$this->normalizerCache[$class][$format] = $normalizer;
|
||||||
|
return $normalizer->normalize($object, $format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Denormalizes data back into an object of the given class
|
||||||
|
*
|
||||||
|
* @param mixed $data data to restore
|
||||||
|
* @param string $class the expected class to instantiate
|
||||||
|
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function denormalizeObject($data, $class, $format = null)
|
||||||
|
{
|
||||||
|
if (!$this->normalizers) {
|
||||||
|
throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
||||||
|
}
|
||||||
|
if (isset($this->denormalizerCache[$class][$format])) {
|
||||||
|
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
|
||||||
|
}
|
||||||
|
foreach ($this->normalizers as $normalizer) {
|
||||||
|
if ($normalizer->supportsDenormalization($class, $format)) {
|
||||||
|
$this->denormalizerCache[$class][$format] = $normalizer;
|
||||||
|
return $normalizer->denormalize($data, $class, $format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NormalizerInterface $normalizer
|
||||||
*/
|
*/
|
||||||
public function addNormalizer(NormalizerInterface $normalizer)
|
public function addNormalizer(NormalizerInterface $normalizer)
|
||||||
{
|
{
|
||||||
$this->normalizers[] = $normalizer;
|
$this->normalizers[] = $normalizer;
|
||||||
$normalizer->setSerializer($this);
|
if ($normalizer instanceof SerializerAwareInterface) {
|
||||||
|
$normalizer->setSerializer($this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @return array[]NormalizerInterface
|
||||||
*/
|
*/
|
||||||
public function getNormalizers()
|
public function getNormalizers()
|
||||||
{
|
{
|
||||||
@ -173,7 +184,7 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @param NormalizerInterface $normalizer
|
||||||
*/
|
*/
|
||||||
public function removeNormalizer(NormalizerInterface $normalizer)
|
public function removeNormalizer(NormalizerInterface $normalizer)
|
||||||
{
|
{
|
||||||
@ -181,16 +192,31 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @param string $format format name
|
||||||
|
* @param EncoderInterface $encoder
|
||||||
*/
|
*/
|
||||||
public function setEncoder($format, EncoderInterface $encoder)
|
public function setEncoder($format, EncoderInterface $encoder)
|
||||||
{
|
{
|
||||||
$this->encoders[$format] = $encoder;
|
$this->encoders[$format] = $encoder;
|
||||||
$encoder->setSerializer($this);
|
if ($encoder instanceof SerializerAwareInterface) {
|
||||||
|
$encoder->setSerializer($this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @param string $format format name
|
||||||
|
* @param DecoderInterface $decoder
|
||||||
|
*/
|
||||||
|
public function setDecoder($format, DecoderInterface $decoder)
|
||||||
|
{
|
||||||
|
$this->decoders[$format] = $decoder;
|
||||||
|
if ($decoder instanceof SerializerAwareInterface) {
|
||||||
|
$decoder->setSerializer($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array[]EncoderInterface
|
||||||
*/
|
*/
|
||||||
public function getEncoders()
|
public function getEncoders()
|
||||||
{
|
{
|
||||||
@ -198,7 +224,15 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @return array[]DecoderInterface
|
||||||
|
*/
|
||||||
|
public function getDecoders()
|
||||||
|
{
|
||||||
|
return $this->decoders;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return EncoderInterface
|
||||||
*/
|
*/
|
||||||
public function getEncoder($format)
|
public function getEncoder($format)
|
||||||
{
|
{
|
||||||
@ -206,7 +240,18 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @return DecoderInterface
|
||||||
|
*/
|
||||||
|
public function getDecoder($format)
|
||||||
|
{
|
||||||
|
return $this->decoders[$format];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the serializer has an encoder registered for the given format
|
||||||
|
*
|
||||||
|
* @param string $format format name
|
||||||
|
* @return Boolean
|
||||||
*/
|
*/
|
||||||
public function hasEncoder($format)
|
public function hasEncoder($format)
|
||||||
{
|
{
|
||||||
@ -214,10 +259,29 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* Checks whether the serializer has a decoder registered for the given format
|
||||||
|
*
|
||||||
|
* @param string $format format name
|
||||||
|
* @return Boolean
|
||||||
|
*/
|
||||||
|
public function hasDecoder($format)
|
||||||
|
{
|
||||||
|
return isset($this->decoders[$format]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $format format name
|
||||||
*/
|
*/
|
||||||
public function removeEncoder($format)
|
public function removeEncoder($format)
|
||||||
{
|
{
|
||||||
unset($this->encoders[$format]);
|
unset($this->encoders[$format]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $format format name
|
||||||
|
*/
|
||||||
|
public function removeDecoder($format)
|
||||||
|
{
|
||||||
|
unset($this->decoders[$format]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,12 +27,4 @@ interface SerializerAwareInterface
|
|||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
function setSerializer(SerializerInterface $serializer);
|
function setSerializer(SerializerInterface $serializer);
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the owning Serializer object
|
|
||||||
*
|
|
||||||
* @return SerializerInterface
|
|
||||||
* @api
|
|
||||||
*/
|
|
||||||
function getSerializer();
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
namespace Symfony\Tests\Component\Serializer\Fixtures;
|
namespace Symfony\Tests\Component\Serializer\Fixtures;
|
||||||
|
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
class Dummy implements NormalizableInterface
|
class Dummy implements NormalizableInterface
|
||||||
{
|
{
|
||||||
@ -12,7 +12,7 @@ class Dummy implements NormalizableInterface
|
|||||||
public $baz;
|
public $baz;
|
||||||
public $qux;
|
public $qux;
|
||||||
|
|
||||||
public function normalize(NormalizerInterface $normalizer, $format, $properties = null)
|
public function normalize(SerializerInterface $serializer, $format = null)
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'foo' => $this->foo,
|
'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->foo = $data['foo'];
|
||||||
$this->bar = $data['bar'];
|
$this->bar = $data['bar'];
|
||||||
|
@ -3,19 +3,19 @@
|
|||||||
namespace Symfony\Tests\Component\Serializer\Fixtures;
|
namespace Symfony\Tests\Component\Serializer\Fixtures;
|
||||||
|
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
class ScalarDummy implements NormalizableInterface
|
class ScalarDummy implements NormalizableInterface
|
||||||
{
|
{
|
||||||
public $foo;
|
public $foo;
|
||||||
public $xmlFoo;
|
public $xmlFoo;
|
||||||
|
|
||||||
public function normalize(NormalizerInterface $normalizer, $format, $properties = null)
|
public function normalize(SerializerInterface $serializer, $format = null)
|
||||||
{
|
{
|
||||||
return $format === 'xml' ? $this->xmlFoo : $this->foo;
|
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') {
|
if ($format === 'xml') {
|
||||||
$this->xmlFoo = $data;
|
$this->xmlFoo = $data;
|
||||||
|
@ -6,6 +6,7 @@ require_once __DIR__.'/../Fixtures/ScalarDummy.php';
|
|||||||
|
|
||||||
use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
|
use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
|
||||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||||
|
use Symfony\Component\Serializer\Serializer;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Symfony framework.
|
* This file is part of the Symfony framework.
|
||||||
@ -21,7 +22,7 @@ class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->normalizer = new CustomNormalizer;
|
$this->normalizer = new CustomNormalizer;
|
||||||
$this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
|
$this->normalizer->setSerializer(new Serializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSerialize()
|
public function testSerialize()
|
||||||
|
@ -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()
|
public function testDenormalize()
|
||||||
{
|
{
|
||||||
$obj = $this->normalizer->denormalize(
|
$obj = $this->normalizer->denormalize(
|
||||||
|
@ -65,7 +65,7 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testDecode()
|
public function testDecode()
|
||||||
{
|
{
|
||||||
$this->serializer->setEncoder('json', new JsonEncoder());
|
$this->serializer->setDecoder('json', new JsonEncoder());
|
||||||
$data = array('foo', array(5, 3));
|
$data = array('foo', array(5, 3));
|
||||||
$result = $this->serializer->decode(json_encode($data), 'json');
|
$result = $this->serializer->decode(json_encode($data), 'json');
|
||||||
$this->assertEquals($data, $result);
|
$this->assertEquals($data, $result);
|
||||||
|
Reference in New Issue
Block a user