2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Component\Serializer;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
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-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')
|
|
|
|
* $serializer->denormalizeObject($data, 'Class', 'xml')
|
|
|
|
*
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*/
|
|
|
|
class Serializer implements SerializerInterface
|
|
|
|
{
|
2011-04-03 15:24:25 +01:00
|
|
|
private $normalizers = array();
|
|
|
|
private $encoders = array();
|
2011-05-10 08:17:33 +01:00
|
|
|
private $decoders = array();
|
2011-04-29 22:51:37 +01:00
|
|
|
protected $normalizerCache = array();
|
2011-05-06 18:36:56 +01:00
|
|
|
protected $denormalizerCache = array();
|
2011-01-18 19:17:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function serialize($data, $format)
|
|
|
|
{
|
2011-05-10 08:18:23 +01:00
|
|
|
if (!isset($this->encoders[$format])) {
|
2011-05-08 17:16:53 +01:00
|
|
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
|
|
|
}
|
2011-05-10 08:18:23 +01:00
|
|
|
if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
|
2011-05-08 17:16:53 +01:00
|
|
|
$data = $this->normalize($data);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function deserialize($data, $type, $format) {
|
|
|
|
return $this->denormalize($this->decode($data, $format), $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-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-01-18 19:17:24 +00:00
|
|
|
throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:01:19 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function denormalize($data, $type, $format = null)
|
|
|
|
{
|
|
|
|
return $this->denormalizeObject($data, $type, $format);
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function encode($data, $format)
|
|
|
|
{
|
2011-05-10 08:18:23 +01:00
|
|
|
if (!isset($this->encoders[$format])) {
|
2011-01-18 19:17:24 +00:00
|
|
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $this->encoders[$format]->encode($data, $format);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function decode($data, $format)
|
|
|
|
{
|
2011-05-10 08:18:23 +01:00
|
|
|
if (!isset($this->decoders[$format])) {
|
2011-05-10 08:17:33 +01:00
|
|
|
throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-10 08:17:33 +01:00
|
|
|
return $this->decoders[$format]->decode($data, $format);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 14:15:53 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +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
|
|
|
|
*/
|
|
|
|
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;
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-10 08:24:19 +01:00
|
|
|
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) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param NormalizerInterface $normalizer
|
2011-04-03 14:15:53 +01:00
|
|
|
*/
|
2011-01-18 19:17:24 +00:00
|
|
|
public function addNormalizer(NormalizerInterface $normalizer)
|
|
|
|
{
|
|
|
|
$this->normalizers[] = $normalizer;
|
2011-05-08 16:54:35 +01:00
|
|
|
if ($normalizer instanceof SerializerAwareInterface) {
|
|
|
|
$normalizer->setSerializer($this);
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 14:15:53 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @return array[]NormalizerInterface
|
2011-04-03 14:15:53 +01:00
|
|
|
*/
|
2011-01-18 19:17:24 +00:00
|
|
|
public function getNormalizers()
|
|
|
|
{
|
|
|
|
return $this->normalizers;
|
|
|
|
}
|
|
|
|
|
2011-04-03 14:15:53 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @param NormalizerInterface $normalizer
|
2011-04-03 14:15:53 +01:00
|
|
|
*/
|
2011-01-18 19:17:24 +00:00
|
|
|
public function removeNormalizer(NormalizerInterface $normalizer)
|
|
|
|
{
|
|
|
|
unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
|
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @param string $format format name
|
|
|
|
* @param EncoderInterface $encoder
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
2011-01-26 03:19:02 +00:00
|
|
|
public function setEncoder($format, EncoderInterface $encoder)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
$this->encoders[$format] = $encoder;
|
2011-05-08 16:54:35 +01:00
|
|
|
if ($encoder instanceof SerializerAwareInterface) {
|
|
|
|
$encoder->setSerializer($this);
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 08:17:33 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @param string $format format name
|
|
|
|
* @param DecoderInterface $decoder
|
2011-05-10 08:17:33 +01:00
|
|
|
*/
|
|
|
|
public function setDecoder($format, DecoderInterface $decoder)
|
|
|
|
{
|
|
|
|
$this->decoders[$format] = $decoder;
|
|
|
|
if ($decoder instanceof SerializerAwareInterface) {
|
|
|
|
$decoder->setSerializer($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @return array[]EncoderInterface
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
2011-01-18 19:17:24 +00:00
|
|
|
public function getEncoders()
|
|
|
|
{
|
|
|
|
return $this->encoders;
|
|
|
|
}
|
|
|
|
|
2011-05-10 08:17:33 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @return array[]DecoderInterface
|
2011-05-10 08:17:33 +01:00
|
|
|
*/
|
|
|
|
public function getDecoders()
|
|
|
|
{
|
|
|
|
return $this->decoders;
|
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @return EncoderInterface
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
|
|
|
public function getEncoder($format)
|
|
|
|
{
|
|
|
|
return $this->encoders[$format];
|
|
|
|
}
|
|
|
|
|
2011-05-10 08:17:33 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @return DecoderInterface
|
2011-05-10 08:17:33 +01:00
|
|
|
*/
|
|
|
|
public function getDecoder($format)
|
|
|
|
{
|
|
|
|
return $this->decoders[$format];
|
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* Checks whether the serializer has an encoder registered for the given format
|
|
|
|
*
|
|
|
|
* @param string $format format name
|
|
|
|
* @return Boolean
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
|
|
|
public function hasEncoder($format)
|
|
|
|
{
|
|
|
|
return isset($this->encoders[$format]);
|
|
|
|
}
|
|
|
|
|
2011-05-08 16:49:19 +01:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* Checks whether the serializer has a decoder registered for the given format
|
|
|
|
*
|
|
|
|
* @param string $format format name
|
|
|
|
* @return Boolean
|
2011-05-08 16:49:19 +01:00
|
|
|
*/
|
|
|
|
public function hasDecoder($format)
|
|
|
|
{
|
2011-05-10 08:17:33 +01:00
|
|
|
return isset($this->decoders[$format]);
|
2011-05-08 16:49:19 +01:00
|
|
|
}
|
|
|
|
|
2011-01-25 09:40:22 +00:00
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @param string $format format name
|
2011-01-25 09:40:22 +00:00
|
|
|
*/
|
2011-01-18 19:17:24 +00:00
|
|
|
public function removeEncoder($format)
|
|
|
|
{
|
|
|
|
unset($this->encoders[$format]);
|
|
|
|
}
|
2011-05-10 08:17:33 +01:00
|
|
|
|
|
|
|
/**
|
2011-05-10 08:24:19 +01:00
|
|
|
* @param string $format format name
|
2011-05-10 08:17:33 +01:00
|
|
|
*/
|
|
|
|
public function removeDecoder($format)
|
|
|
|
{
|
|
|
|
unset($this->decoders[$format]);
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|