2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Component\Serializer;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
2011-05-08 16:49:19 +01:00
|
|
|
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
2011-05-08 17:16:53 +01:00
|
|
|
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
|
2011-11-08 20:04:30 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
2011-05-29 09:55:08 +01:00
|
|
|
use Symfony\Component\Serializer\Exception\RuntimeException;
|
|
|
|
use Symfony\Component\Serializer\Exception\LogicException;
|
|
|
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
2011-01-18 19:17:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializer serializes and deserializes data
|
|
|
|
*
|
|
|
|
* objects are turned into arrays by normalizers
|
|
|
|
* arrays are turned into various output formats by encoders
|
|
|
|
*
|
|
|
|
* $serializer->serialize($obj, 'xml')
|
|
|
|
* $serializer->decode($data, 'xml')
|
2011-05-29 09:55:54 +01:00
|
|
|
* $serializer->denormalize($data, 'Class', 'xml')
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-05-29 09:55:54 +01:00
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
2011-11-08 20:04:30 +00:00
|
|
|
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-05-27 17:57:51 +01:00
|
|
|
protected $normalizers = array();
|
|
|
|
protected $encoders = array();
|
2011-04-29 22:51:37 +01:00
|
|
|
protected $normalizerCache = array();
|
2011-05-06 18:36:56 +01:00
|
|
|
protected $denormalizerCache = array();
|
2011-11-08 20:04:55 +00:00
|
|
|
protected $encoderByFormat = array();
|
|
|
|
protected $decoderByFormat = array();
|
2011-01-18 19:17:24 +00:00
|
|
|
|
2011-05-29 09:55:54 +01:00
|
|
|
public function __construct(array $normalizers = array(), array $encoders = array())
|
|
|
|
{
|
|
|
|
foreach ($normalizers as $normalizer) {
|
|
|
|
if ($normalizer instanceof SerializerAwareInterface) {
|
|
|
|
$normalizer->setSerializer($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->normalizers = $normalizers;
|
|
|
|
|
|
|
|
foreach ($encoders as $encoder) {
|
|
|
|
if ($encoder instanceof SerializerAwareInterface) {
|
|
|
|
$encoder->setSerializer($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->encoders = $encoders;
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-05-29 09:55:54 +01:00
|
|
|
public final function serialize($data, $format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-06-09 13:18:47 +01:00
|
|
|
if (!$this->supportsSerialization($format)) {
|
|
|
|
throw new UnexpectedValueException('Serialization for the format '.$format.' is not supported');
|
2011-05-08 17:16:53 +01:00
|
|
|
}
|
2011-06-09 13:18:47 +01:00
|
|
|
|
|
|
|
$encoder = $this->getEncoder($format);
|
|
|
|
|
2011-06-09 12:56:31 +01:00
|
|
|
if (!$encoder instanceof NormalizationAwareInterface) {
|
2011-08-12 10:01:28 +01:00
|
|
|
$data = $this->normalize($data, $format);
|
2011-05-08 17:16:53 +01:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-08 14:17:42 +01:00
|
|
|
return $this->encode($data, $format);
|
2011-05-06 12:01:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-29 09:57:30 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-06 12:01:19 +01:00
|
|
|
*/
|
2011-06-09 12:55:54 +01:00
|
|
|
public final function deserialize($data, $type, $format)
|
|
|
|
{
|
2011-06-09 13:18:47 +01:00
|
|
|
if (!$this->supportsDeserialization($format)) {
|
|
|
|
throw new UnexpectedValueException('Deserialization for the format '.$format.' is not supported');
|
|
|
|
}
|
|
|
|
|
2011-06-09 12:55:54 +01:00
|
|
|
$data = $this->decode($data, $format);
|
|
|
|
|
|
|
|
return $this->denormalize($data, $type, $format);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-05-06 12:01:19 +01:00
|
|
|
public function normalize($data, $format = null)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-05-09 12:59:54 +01:00
|
|
|
if (null === $data || is_scalar($data)) {
|
2011-05-06 12:01:19 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2011-11-02 03:19:26 +00:00
|
|
|
if (is_object($data) && $this->supportsNormalization($data, $format)) {
|
|
|
|
return $this->normalizeObject($data, $format);
|
|
|
|
}
|
2011-06-06 16:29:50 +01:00
|
|
|
if ($data instanceof \Traversable) {
|
2011-05-06 18:04:39 +01:00
|
|
|
$normalized = array();
|
|
|
|
foreach ($data as $key => $val) {
|
2011-05-06 18:36:56 +01:00
|
|
|
$normalized[$key] = $this->normalize($val, $format);
|
2011-05-06 18:04:39 +01:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-06 18:04:39 +01:00
|
|
|
return $normalized;
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
if (is_object($data)) {
|
|
|
|
return $this->normalizeObject($data, $format);
|
|
|
|
}
|
2011-05-06 18:36:56 +01:00
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $key => $val) {
|
|
|
|
$data[$key] = $this->normalize($val, $format);
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-06 18:36:56 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2011-05-29 09:55:08 +01:00
|
|
|
throw new UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-06 12:01:19 +01:00
|
|
|
/**
|
2011-05-29 09:57:30 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-06 12:01:19 +01:00
|
|
|
*/
|
|
|
|
public function denormalize($data, $type, $format = null)
|
|
|
|
{
|
|
|
|
return $this->denormalizeObject($data, $type, $format);
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-05-29 09:57:30 +01:00
|
|
|
public final function encode($data, $format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-05-29 09:58:12 +01:00
|
|
|
return $this->getEncoder($format)->encode($data, $format);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-05-29 09:57:30 +01:00
|
|
|
public final function decode($data, $format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-05-29 09:58:12 +01:00
|
|
|
return $this->getEncoder($format)->decode($data, $format);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 14:15:53 +01:00
|
|
|
/**
|
2011-05-29 09:16:42 +01:00
|
|
|
* 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
|
2011-05-10 08:24:19 +01:00
|
|
|
*/
|
2011-05-29 09:16:42 +01:00
|
|
|
private function normalizeObject($object, $format = null)
|
2011-05-10 08:24:19 +01:00
|
|
|
{
|
|
|
|
if (!$this->normalizers) {
|
2011-05-29 09:55:08 +01:00
|
|
|
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
|
2011-05-10 08:24:19 +01:00
|
|
|
}
|
2011-11-02 03:01:10 +00:00
|
|
|
|
2011-05-10 08:24:19 +01:00
|
|
|
$class = get_class($object);
|
2011-11-02 03:01:10 +00:00
|
|
|
|
|
|
|
// If normalization is supported, cached normalizer will exist
|
|
|
|
if ($this->supportsNormalization($object, $format)) {
|
2011-05-10 08:24:19 +01:00
|
|
|
return $this->normalizerCache[$class][$format]->normalize($object, $format);
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-29 09:55:08 +01:00
|
|
|
throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
|
2011-05-10 08:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-29 09:16:42 +01:00
|
|
|
* 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
|
2011-05-10 08:24:19 +01:00
|
|
|
*/
|
2011-05-29 09:16:42 +01:00
|
|
|
private function denormalizeObject($data, $class, $format = null)
|
2011-05-10 08:24:19 +01:00
|
|
|
{
|
|
|
|
if (!$this->normalizers) {
|
2011-05-29 09:55:08 +01:00
|
|
|
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
2011-05-10 08:24:19 +01:00
|
|
|
}
|
|
|
|
if (isset($this->denormalizerCache[$class][$format])) {
|
|
|
|
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
|
|
|
|
}
|
|
|
|
foreach ($this->normalizers as $normalizer) {
|
2011-05-21 13:48:27 +01:00
|
|
|
if ($normalizer->supportsDenormalization($data, $class, $format)) {
|
2011-05-10 08:24:19 +01:00
|
|
|
$this->denormalizerCache[$class][$format] = $normalizer;
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-10 08:24:19 +01:00
|
|
|
return $normalizer->denormalize($data, $class, $format);
|
|
|
|
}
|
|
|
|
}
|
2011-05-29 09:55:08 +01:00
|
|
|
throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
|
2011-05-10 08:24:19 +01:00
|
|
|
}
|
|
|
|
|
2011-11-02 03:01:10 +00:00
|
|
|
/**
|
|
|
|
* Check if normalizer cache or normalizers supports provided object, which will then be cached
|
|
|
|
*
|
|
|
|
* @param object $object Object to test for normalization support
|
|
|
|
* @param string $format Format name, needed for normalizers to pivot on
|
|
|
|
*/
|
2011-11-02 03:25:33 +00:00
|
|
|
private function supportsNormalization($object, $format)
|
2011-11-02 03:01:10 +00:00
|
|
|
{
|
|
|
|
$class = get_class($object);
|
|
|
|
|
|
|
|
if (isset($this->normalizerCache[$class][$format])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->normalizers as $normalizer) {
|
|
|
|
if ($normalizer->supportsNormalization($object, $format)) {
|
|
|
|
$this->normalizerCache[$class][$format] = $normalizer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-10 08:24:19 +01:00
|
|
|
/**
|
2011-05-27 17:57:51 +01:00
|
|
|
* {@inheritdoc}
|
2011-04-03 14:15:53 +01:00
|
|
|
*/
|
2011-05-29 09:58:32 +01:00
|
|
|
public function supportsSerialization($format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-06-09 13:18:47 +01:00
|
|
|
return $this->supportsEncoding($format);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 14:15:53 +01:00
|
|
|
/**
|
2011-05-27 17:57:51 +01:00
|
|
|
* {@inheritdoc}
|
2011-04-03 14:15:53 +01:00
|
|
|
*/
|
2011-05-29 09:58:32 +01:00
|
|
|
public function supportsDeserialization($format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-06-09 13:18:47 +01:00
|
|
|
return $this->supportsDecoding($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function supportsEncoding($format)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$encoder = $this->getEncoder($format);
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encoder instanceof EncoderInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function supportsDecoding($format)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$encoder = $this->getEncoder($format);
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encoder instanceof DecoderInterface;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-27 17:57:51 +01:00
|
|
|
* {@inheritdoc}
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
2011-11-08 20:04:55 +00:00
|
|
|
private function getEncoder($format)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-11-08 20:04:55 +00:00
|
|
|
if (isset($this->encoderByFormat[$format])
|
|
|
|
&& isset($this->encoderByFormat[$format])
|
|
|
|
) {
|
|
|
|
return $this->encoders[$this->encoderByFormat[$format]];
|
2011-05-08 16:54:35 +01:00
|
|
|
}
|
2011-05-29 09:58:32 +01:00
|
|
|
|
2011-11-08 20:04:55 +00:00
|
|
|
foreach ($this->encoders as $i => $encoder) {
|
|
|
|
if ($encoder->supportsEncoding($format)) {
|
|
|
|
$this->encoderByFormat[$format] = $i;
|
|
|
|
return $encoder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
private function getDecoder($format)
|
|
|
|
{
|
|
|
|
if (isset($this->decoderByFormat[$format])
|
|
|
|
&& isset($this->decoderByFormat[$format])
|
|
|
|
) {
|
|
|
|
return $this->encoders[$this->decoderByFormat[$format]];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->encoders as $i => $encoder) {
|
|
|
|
if ($encoder->supportsDecoding($format)) {
|
|
|
|
$this->decoderByFormat[$format] = $i;
|
|
|
|
return $encoder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
}
|