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

224 lines
6.1 KiB
PHP
Raw Normal View History

<?php
namespace Symfony\Component\Serializer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
/*
* 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
{
2011-04-03 15:24:25 +01:00
private $normalizers = array();
private $encoders = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();
2011-04-03 14:14:07 +01:00
/**
* @param mixed $value value to test
* @return Boolean whether the type is a structured type (array + objects)
*/
public function isStructuredType($value)
2011-01-30 09:10:27 +00:00
{
2011-04-03 14:14:07 +01:00
return null !== $value && !is_scalar($value);
2011-01-30 09:10:27 +00:00
}
/**
* {@inheritdoc}
*/
public function serialize($data, $format)
{
return $this->encode($this->normalize($data, $format), $format);
}
/**
* {@inheritDoc}
*/
public function deserialize($data, $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}
*/
public function normalize($data, $format = null)
{
if (!$this->isStructuredType($data)) {
return $data;
}
if ($data instanceof Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
$normalized[$key] = $this->normalize($val, $format);
}
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);
}
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)
{
if (!$this->hasEncoder($format)) {
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
}
return $this->encoders[$format]->encode($data, $format);
}
/**
* {@inheritdoc}
*/
public function decode($data, $format)
{
if (!$this->hasEncoder($format)) {
throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format');
}
return $this->encoders[$format]->decode($data, $format);
}
/**
* {@inheritdoc}
*/
public function addNormalizer(NormalizerInterface $normalizer)
{
$this->normalizers[] = $normalizer;
$normalizer->setSerializer($this);
}
/**
* {@inheritdoc}
*/
public function getNormalizers()
{
return $this->normalizers;
}
/**
* {@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;
$encoder->setSerializer($this);
}
/**
* {@inheritdoc}
*/
public function getEncoders()
{
return $this->encoders;
}
/**
* {@inheritdoc}
*/
public function getEncoder($format)
{
return $this->encoders[$format];
}
/**
* {@inheritdoc}
*/
public function hasEncoder($format)
{
return isset($this->encoders[$format]);
}
/**
* {@inheritdoc}
*/
public function removeEncoder($format)
{
unset($this->encoders[$format]);
}
}