This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Serializer/Serializer.php

300 lines
9.5 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer;
use Symfony\Component\Serializer\Encoder\ChainDecoder;
use Symfony\Component\Serializer\Encoder\ChainEncoder;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
2011-11-08 20:04:30 +00:00
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* 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')
*
* @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-11-08 20:04:30 +00:00
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
{
protected $encoder;
protected $decoder;
protected $normalizers = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();
2012-07-17 18:53:41 +01:00
public function __construct(array $normalizers = array(), array $encoders = array())
2011-05-29 09:55:54 +01:00
{
foreach ($normalizers as $normalizer) {
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($this);
}
}
$this->normalizers = $normalizers;
$decoders = array();
$realEncoders = array();
2011-05-29 09:55:54 +01:00
foreach ($encoders as $encoder) {
if ($encoder instanceof SerializerAwareInterface) {
$encoder->setSerializer($this);
}
if ($encoder instanceof DecoderInterface) {
$decoders[] = $encoder;
}
if ($encoder instanceof EncoderInterface) {
$realEncoders[] = $encoder;
}
2011-05-29 09:55:54 +01:00
}
$this->encoder = new ChainEncoder($realEncoders);
$this->decoder = new ChainDecoder($decoders);
2011-05-29 09:55:54 +01:00
}
/**
* {@inheritdoc}
*/
final public function serialize($data, $format, array $context = array())
{
2011-12-14 17:10:48 +00:00
if (!$this->supportsEncoding($format)) {
2013-04-10 11:24:37 +01:00
throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
}
2011-06-09 13:18:47 +01:00
if ($this->encoder->needsNormalization($format)) {
$data = $this->normalize($data, $format, $context);
}
2011-06-08 11:12:55 +01:00
return $this->encode($data, $format, $context);
}
/**
* {@inheritdoc}
*/
final public function deserialize($data, $type, $format, array $context = array())
2011-06-09 12:55:54 +01:00
{
2011-12-14 17:10:48 +00:00
if (!$this->supportsDecoding($format)) {
2013-04-10 11:24:37 +01:00
throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
2011-06-09 13:18:47 +01:00
}
$data = $this->decode($data, $format, $context);
2011-06-09 12:55:54 +01:00
return $this->denormalize($data, $type, $format, $context);
}
/**
* {@inheritdoc}
*/
public function normalize($data, $format = null, array $context = array())
{
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_object($data) && $this->supportsNormalization($data, $format)) {
return $this->normalizeObject($data, $format, $context);
}
2011-06-06 16:29:50 +01:00
if ($data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
$normalized[$key] = $this->normalize($val, $format, $context);
}
2011-06-08 11:12:55 +01:00
return $normalized;
}
if (is_object($data)) {
return $this->normalizeObject($data, $format, $context);
}
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = $this->normalize($val, $format, $context);
}
2011-06-08 11:12:55 +01:00
return $data;
}
2013-04-10 11:24:37 +01:00
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $type, $format = null, array $context = array())
{
return $this->denormalizeObject($data, $type, $format, $context);
}
2011-11-08 20:54:41 +00:00
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
try {
$this->getNormalizer($data, $format);
} catch (RuntimeException $e) {
2011-11-08 20:54:41 +00:00
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
try {
$this->getDenormalizer($data, $type, $format = null);
} catch (RuntimeException $e) {
2011-11-08 20:54:41 +00:00
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
private function getNormalizer($data, $format = null)
{
foreach ($this->normalizers as $normalizer) {
2013-05-08 09:39:40 +01:00
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
2011-11-08 20:54:41 +00:00
return $normalizer;
}
}
throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format));
}
/**
* {@inheritdoc}
*/
private function getDenormalizer($data, $type, $format = null)
{
foreach ($this->normalizers as $normalizer) {
2013-05-08 09:39:40 +01:00
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format)) {
2011-11-08 20:54:41 +00:00
return $normalizer;
}
}
throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format));
}
/**
* {@inheritdoc}
*/
final public function encode($data, $format, array $context = array())
{
return $this->encoder->encode($data, $format, $context);
}
/**
* {@inheritdoc}
*/
final public function decode($data, $format, array $context = array())
{
return $this->decoder->decode($data, $format, $context);
}
/**
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
* @param array $context The context data for this particular normalization
2012-11-16 16:06:12 +00:00
*
2011-05-29 09:16:42 +01:00
* @return array|scalar
*
* @throws LogicException
* @throws UnexpectedValueException
*/
private function normalizeObject($object, $format = null, array $context = array())
{
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, $context);
}
2011-06-08 11:12:55 +01:00
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface
&& $normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
return $normalizer->normalize($object, $format, $context);
}
}
2013-04-10 11:24:37 +01:00
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', $class));
}
/**
2011-05-29 09:16:42 +01:00
* Denormalizes data back into an object of the given class
*
2012-05-15 21:19:31 +01:00
* @param mixed $data data to restore
* @param string $class the expected class to instantiate
2011-05-29 09:16:42 +01:00
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @param array $context The context data for this particular denormalization
2012-11-16 16:06:12 +00:00
*
2011-05-29 09:16:42 +01:00
* @return object
*
* @throws LogicException
* @throws UnexpectedValueException
*/
private function denormalizeObject($data, $class, $format = null, array $context = array())
{
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, $context);
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof DenormalizerInterface
&& $normalizer->supportsDenormalization($data, $class, $format)) {
$this->denormalizerCache[$class][$format] = $normalizer;
2011-06-08 11:12:55 +01:00
return $normalizer->denormalize($data, $class, $format, $context);
}
}
2013-04-10 11:24:37 +01:00
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
}
2011-06-09 13:18:47 +01:00
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return $this->encoder->supportsEncoding($format);
2011-06-09 13:18:47 +01:00
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return $this->decoder->supportsDecoding($format);
}
}