Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer; Chris@0: Chris@0: use Symfony\Component\Serializer\Encoder\ChainDecoder; Chris@0: use Symfony\Component\Serializer\Encoder\ChainEncoder; Chris@0: use Symfony\Component\Serializer\Encoder\EncoderInterface; Chris@0: use Symfony\Component\Serializer\Encoder\DecoderInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\NormalizerInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; Chris@0: use Symfony\Component\Serializer\Exception\LogicException; Chris@0: use Symfony\Component\Serializer\Exception\UnexpectedValueException; Chris@0: Chris@0: /** Chris@0: * Serializer serializes and deserializes data. Chris@0: * Chris@0: * objects are turned into arrays by normalizers. Chris@0: * arrays are turned into various output formats by encoders. Chris@0: * Chris@0: * $serializer->serialize($obj, 'xml') Chris@0: * $serializer->decode($data, 'xml') Chris@0: * $serializer->denormalize($data, 'Class', 'xml') Chris@0: * Chris@0: * @author Jordi Boggiano Chris@0: * @author Johannes M. Schmitt Chris@0: * @author Lukas Kahwe Smith Chris@0: * @author Kévin Dunglas Chris@0: */ Chris@0: class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface Chris@0: { Chris@0: /** Chris@0: * @var Encoder\ChainEncoder Chris@0: */ Chris@0: protected $encoder; Chris@0: Chris@0: /** Chris@0: * @var Encoder\ChainDecoder Chris@0: */ Chris@0: protected $decoder; Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: protected $normalizers = array(); Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: * Chris@0: * @deprecated since 3.1 will be removed in 4.0 Chris@0: */ Chris@0: protected $normalizerCache = array(); Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: * Chris@0: * @deprecated since 3.1 will be removed in 4.0 Chris@0: */ Chris@0: protected $denormalizerCache = array(); Chris@0: Chris@0: public function __construct(array $normalizers = array(), array $encoders = array()) Chris@0: { Chris@0: foreach ($normalizers as $normalizer) { Chris@0: if ($normalizer instanceof SerializerAwareInterface) { Chris@0: $normalizer->setSerializer($this); Chris@0: } Chris@0: Chris@0: if ($normalizer instanceof DenormalizerAwareInterface) { Chris@0: $normalizer->setDenormalizer($this); Chris@0: } Chris@0: Chris@0: if ($normalizer instanceof NormalizerAwareInterface) { Chris@0: $normalizer->setNormalizer($this); Chris@0: } Chris@0: } Chris@0: $this->normalizers = $normalizers; Chris@0: Chris@0: $decoders = array(); Chris@0: $realEncoders = array(); Chris@0: foreach ($encoders as $encoder) { Chris@0: if ($encoder instanceof SerializerAwareInterface) { Chris@0: $encoder->setSerializer($this); Chris@0: } Chris@0: if ($encoder instanceof DecoderInterface) { Chris@0: $decoders[] = $encoder; Chris@0: } Chris@0: if ($encoder instanceof EncoderInterface) { Chris@0: $realEncoders[] = $encoder; Chris@0: } Chris@0: } Chris@0: $this->encoder = new ChainEncoder($realEncoders); Chris@0: $this->decoder = new ChainDecoder($decoders); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: final public function serialize($data, $format, array $context = array()) Chris@0: { Chris@0: if (!$this->supportsEncoding($format)) { Chris@0: throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format)); Chris@0: } Chris@0: Chris@0: if ($this->encoder->needsNormalization($format)) { Chris@0: $data = $this->normalize($data, $format, $context); Chris@0: } Chris@0: Chris@0: return $this->encode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: final public function deserialize($data, $type, $format, array $context = array()) Chris@0: { Chris@0: if (!$this->supportsDecoding($format)) { Chris@0: throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format)); Chris@0: } Chris@0: Chris@0: $data = $this->decode($data, $format, $context); Chris@0: Chris@0: return $this->denormalize($data, $type, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function normalize($data, $format = null, array $context = array()) Chris@0: { Chris@0: // If a normalizer supports the given data, use it Chris@0: if ($normalizer = $this->getNormalizer($data, $format)) { Chris@0: return $normalizer->normalize($data, $format, $context); Chris@0: } Chris@0: Chris@0: if (null === $data || is_scalar($data)) { Chris@0: return $data; Chris@0: } Chris@0: Chris@0: if (is_array($data) || $data instanceof \Traversable) { Chris@0: $normalized = array(); Chris@0: foreach ($data as $key => $val) { Chris@0: $normalized[$key] = $this->normalize($val, $format, $context); Chris@0: } Chris@0: Chris@0: return $normalized; Chris@0: } Chris@0: Chris@0: if (is_object($data)) { Chris@0: if (!$this->normalizers) { Chris@0: throw new LogicException('You must register at least one normalizer to be able to normalize objects.'); Chris@0: } Chris@0: Chris@0: throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data))); Chris@0: } Chris@0: Chris@0: throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true))); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function denormalize($data, $type, $format = null, array $context = array()) Chris@0: { Chris@0: return $this->denormalizeObject($data, $type, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsNormalization($data, $format = null) Chris@0: { Chris@0: return null !== $this->getNormalizer($data, $format); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsDenormalization($data, $type, $format = null) Chris@0: { Chris@0: return null !== $this->getDenormalizer($data, $type, $format); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a matching normalizer. Chris@0: * Chris@0: * @param mixed $data Data to get the serializer for Chris@0: * @param string $format format name, present to give the option to normalizers to act differently based on formats Chris@0: * Chris@0: * @return NormalizerInterface|null Chris@0: */ Chris@0: private function getNormalizer($data, $format) Chris@0: { Chris@0: foreach ($this->normalizers as $normalizer) { Chris@0: if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) { Chris@0: return $normalizer; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a matching denormalizer. Chris@0: * Chris@0: * @param mixed $data data to restore Chris@0: * @param string $class the expected class to instantiate Chris@0: * @param string $format format name, present to give the option to normalizers to act differently based on formats Chris@0: * Chris@0: * @return DenormalizerInterface|null Chris@0: */ Chris@0: private function getDenormalizer($data, $class, $format) Chris@0: { Chris@0: foreach ($this->normalizers as $normalizer) { Chris@0: if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) { Chris@0: return $normalizer; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: final public function encode($data, $format, array $context = array()) Chris@0: { Chris@0: return $this->encoder->encode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: final public function decode($data, $format, array $context = array()) Chris@0: { Chris@0: return $this->decoder->decode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Denormalizes data back into an object of the given class. Chris@0: * Chris@0: * @param mixed $data data to restore Chris@0: * @param string $class the expected class to instantiate Chris@0: * @param string $format format name, present to give the option to normalizers to act differently based on formats Chris@0: * @param array $context The context data for this particular denormalization Chris@0: * Chris@0: * @return object Chris@0: * Chris@0: * @throws LogicException Chris@0: * @throws UnexpectedValueException Chris@0: */ Chris@0: private function denormalizeObject($data, $class, $format, array $context = array()) Chris@0: { Chris@0: if (!$this->normalizers) { Chris@0: throw new LogicException('You must register at least one normalizer to be able to denormalize objects.'); Chris@0: } Chris@0: Chris@0: if ($normalizer = $this->getDenormalizer($data, $class, $format)) { Chris@0: return $normalizer->denormalize($data, $class, $format, $context); Chris@0: } Chris@0: Chris@0: throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsEncoding($format) Chris@0: { Chris@0: return $this->encoder->supportsEncoding($format); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supportsDecoding($format) Chris@0: { Chris@0: return $this->decoder->supportsDecoding($format); Chris@0: } Chris@0: }