<?php
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;
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;
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface {
protected $encoder;
protected $decoder;
protected $normalizers = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();
public function __construct(array $normalizers = array(), array $encoders = array()) {
foreach ($normalizers as $normalizer) {
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer
->setSerializer($this);
}
}
$this->normalizers = $normalizers;
$decoders = array();
$realEncoders = array();
foreach ($encoders as $encoder) {
if ($encoder instanceof SerializerAwareInterface) {
$encoder
->setSerializer($this);
}
if ($encoder instanceof DecoderInterface) {
$decoders[] = $encoder;
}
if ($encoder instanceof EncoderInterface) {
$realEncoders[] = $encoder;
}
}
$this->encoder = new ChainEncoder($realEncoders);
$this->decoder = new ChainDecoder($decoders);
}
public final function serialize($data, $format, array $context = array()) {
if (!$this
->supportsEncoding($format)) {
throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
}
if ($this->encoder
->needsNormalization($format)) {
$data = $this
->normalize($data, $format, $context);
}
return $this
->encode($data, $format, $context);
}
public final function deserialize($data, $type, $format, array $context = array()) {
if (!$this
->supportsDecoding($format)) {
throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
}
$data = $this
->decode($data, $format, $context);
return $this
->denormalize($data, $type, $format, $context);
}
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);
}
if ($data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
$normalized[$key] = $this
->normalize($val, $format, $context);
}
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);
}
return $data;
}
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
}
public function denormalize($data, $type, $format = null, array $context = array()) {
return $this
->denormalizeObject($data, $type, $format, $context);
}
public function supportsNormalization($data, $format = null) {
try {
$this
->getNormalizer($data, $format);
} catch (RuntimeException $e) {
return false;
}
return true;
}
public function supportsDenormalization($data, $type, $format = null) {
try {
$this
->getDenormalizer($data, $type, $format = null);
} catch (RuntimeException $e) {
return false;
}
return true;
}
private function getNormalizer($data, $format = null) {
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface && $normalizer
->supportsNormalization($data, $format)) {
return $normalizer;
}
}
throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format));
}
private function getDenormalizer($data, $type, $format = null) {
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof DenormalizerInterface && $normalizer
->supportsDenormalization($data, $type, $format)) {
return $normalizer;
}
}
throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format));
}
public final function encode($data, $format, array $context = array()) {
return $this->encoder
->encode($data, $format, $context);
}
public final function decode($data, $format, array $context = array()) {
return $this->decoder
->decode($data, $format, $context);
}
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);
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface && $normalizer
->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
return $normalizer
->normalize($object, $format, $context);
}
}
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', $class));
}
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;
return $normalizer
->denormalize($data, $class, $format, $context);
}
}
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
}
public function supportsEncoding($format) {
return $this->encoder
->supportsEncoding($format);
}
public function supportsDecoding($format) {
return $this->decoder
->supportsDecoding($format);
}
}