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

244 lines
7.3 KiB
PHP
Raw Normal View History

<?php
namespace Symfony\Component\Serializer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/*
* 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.
*/
/**
* 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
{
protected $normalizers = array();
protected $encoders = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();
/**
* {@inheritdoc}
*/
public function serialize($data, $format)
{
2011-05-10 08:18:23 +01:00
if (!isset($this->encoders[$format])) {
throw new UnexpectedValueException('No encoder registered for the '.$format.' format');
}
2011-05-10 08:18:23 +01:00
if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
$data = $this->normalize($data);
}
2011-06-08 11:12:55 +01:00
return $this->encode($data, $format);
}
/**
* {@inheritDoc}
*/
public function deserialize($data, $type, $format) {
return $this->denormalize($this->decode($data, $format), $type, $format);
}
/**
* {@inheritdoc}
*/
public function normalize($data, $format = null)
{
if (null === $data || is_scalar($data)) {
return $data;
}
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);
}
2011-06-08 11:12:55 +01:00
return $normalized;
}
if (is_object($data)) {
return $this->normalizeObject($data, $format);
}
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = $this->normalize($val, $format);
}
2011-06-08 11:12:55 +01:00
return $data;
}
throw new UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
}
/**
* {@inheritDoc}
*/
public function denormalize($data, $type, $format = null)
{
return $this->denormalizeObject($data, $type, $format);
}
/**
* {@inheritdoc}
*/
public function encode($data, $format)
{
2011-05-10 08:18:23 +01:00
if (!isset($this->encoders[$format])) {
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
}
2011-06-08 11:12:55 +01:00
return $this->encoders[$format]->encode($data, $format);
}
/**
* {@inheritdoc}
*/
public function decode($data, $format)
{
if (!isset($this->encoders[$format])) {
throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
}
2011-06-08 11:12:55 +01:00
return $this->encoders[$format]->decode($data, $format);
}
/**
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-29 09:16:42 +01:00
private 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
return $normalizer->normalize($object, $format);
}
}
throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
}
/**
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-29 09:16:42 +01:00
private 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($data, $class, $format)) {
$this->denormalizerCache[$class][$format] = $normalizer;
2011-06-08 11:12:55 +01:00
return $normalizer->denormalize($data, $class, $format);
}
}
throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
}
/**
* {@inheritdoc}
*/
public function addNormalizer(NormalizerInterface $normalizer)
{
$this->normalizers[] = $normalizer;
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($this);
}
}
/**
* {@inheritdoc}
*/
public function removeNormalizer(NormalizerInterface $normalizer)
{
unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
}
/**
* {@inheritdoc}
*/
public function setEncoder($format, EncoderInterface $encoder)
{
$this->encoders[$format] = $encoder;
if ($encoder instanceof SerializerAwareInterface) {
$encoder->setSerializer($this);
}
}
/**
* {@inheritdoc}
*/
public function getEncoder($format)
{
if (!isset($this->encoders[$format])) {
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
2011-05-28 10:19:04 +01:00
}
return $this->encoders[$format];
}
/**
* {@inheritdoc}
*/
public function supportsSerialization($format)
{
2011-05-27 18:57:57 +01:00
return isset($this->encoders[$format]);
}
/**
* {@inheritdoc}
*/
public function supportsDeserialization($format)
{
2011-05-27 18:57:57 +01:00
return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface;
}
/**
* {@inheritdoc}
*/
public function removeEncoder($format)
{
2011-05-28 10:19:04 +01:00
if (isset($this->encoders[$format])) {
unset($this->encoders[$format]);
}
}
}