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@17: use Symfony\Component\Serializer\Encoder\DecoderInterface; Chris@0: use Symfony\Component\Serializer\Encoder\EncoderInterface; Chris@17: use Symfony\Component\Serializer\Exception\LogicException; Chris@14: use Symfony\Component\Serializer\Exception\NotEncodableValueException; Chris@14: use Symfony\Component\Serializer\Exception\NotNormalizableValueException; Chris@0: use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; Chris@17: use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; Chris@0: use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 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@17: * $serializer->serialize($obj, 'xml') Chris@17: * $serializer->decode($data, 'xml') Chris@17: * $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@17: protected $normalizers = []; 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@17: protected $normalizerCache = []; 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@17: protected $denormalizerCache = []; Chris@0: Chris@17: public function __construct(array $normalizers = [], array $encoders = []) 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@17: $decoders = []; Chris@17: $realEncoders = []; 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@17: final public function serialize($data, $format, array $context = []) Chris@0: { Chris@14: if (!$this->supportsEncoding($format, $context)) { Chris@14: throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format)); Chris@0: } Chris@0: Chris@14: if ($this->encoder->needsNormalization($format, $context)) { 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@17: final public function deserialize($data, $type, $format, array $context = []) Chris@0: { Chris@14: if (!$this->supportsDecoding($format, $context)) { Chris@14: throw new NotEncodableValueException(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@17: public function normalize($data, $format = null, array $context = []) Chris@0: { Chris@0: // If a normalizer supports the given data, use it Chris@14: if ($normalizer = $this->getNormalizer($data, $format, $context)) { 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@14: if (\is_array($data) || $data instanceof \Traversable) { Chris@17: $normalized = []; 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@14: 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@14: throw new NotNormalizableValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', \get_class($data))); Chris@0: } Chris@0: Chris@14: throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true))); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: * Chris@14: * @throws NotNormalizableValueException Chris@14: */ Chris@17: public function denormalize($data, $type, $format = null, array $context = []) Chris@14: { Chris@14: if (!$this->normalizers) { Chris@14: throw new LogicException('You must register at least one normalizer to be able to denormalize objects.'); Chris@14: } Chris@14: Chris@14: if ($normalizer = $this->getDenormalizer($data, $type, $format, $context)) { Chris@14: return $normalizer->denormalize($data, $type, $format, $context); Chris@14: } Chris@14: Chris@14: throw new NotNormalizableValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $type)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsNormalization($data, $format = null/*, array $context = []*/) Chris@0: { Chris@14: if (\func_num_args() > 2) { Chris@14: $context = \func_get_arg(2); Chris@14: } else { Chris@14: if (__CLASS__ !== \get_class($this)) { Chris@14: $r = new \ReflectionMethod($this, __FUNCTION__); Chris@14: if (__CLASS__ !== $r->getDeclaringClass()->getName()) { Chris@17: @trigger_error(sprintf('The "%s()" method will have a third `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: } Chris@14: Chris@17: $context = []; Chris@14: } Chris@14: Chris@14: return null !== $this->getNormalizer($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/) Chris@0: { Chris@14: if (\func_num_args() > 3) { Chris@14: $context = \func_get_arg(3); Chris@14: } else { Chris@14: if (__CLASS__ !== \get_class($this)) { Chris@14: $r = new \ReflectionMethod($this, __FUNCTION__); Chris@14: if (__CLASS__ !== $r->getDeclaringClass()->getName()) { Chris@17: @trigger_error(sprintf('The "%s()" method will have a fourth `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: } Chris@0: Chris@17: $context = []; Chris@14: } Chris@14: Chris@14: return null !== $this->getDenormalizer($data, $type, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a matching normalizer. Chris@0: * Chris@14: * @param mixed $data Data to get the serializer for Chris@14: * @param string $format Format name, present to give the option to normalizers to act differently based on formats Chris@14: * @param array $context Options available to the normalizer Chris@0: * Chris@0: * @return NormalizerInterface|null Chris@0: */ Chris@14: private function getNormalizer($data, $format, array $context) Chris@0: { Chris@0: foreach ($this->normalizers as $normalizer) { Chris@14: if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) { Chris@0: return $normalizer; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a matching denormalizer. Chris@0: * Chris@14: * @param mixed $data Data to restore Chris@14: * @param string $class The expected class to instantiate Chris@14: * @param string $format Format name, present to give the option to normalizers to act differently based on formats Chris@14: * @param array $context Options available to the denormalizer Chris@0: * Chris@0: * @return DenormalizerInterface|null Chris@0: */ Chris@14: private function getDenormalizer($data, $class, $format, array $context) Chris@0: { Chris@0: foreach ($this->normalizers as $normalizer) { Chris@14: if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format, $context)) { Chris@0: return $normalizer; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: final public function encode($data, $format, array $context = []) Chris@0: { Chris@0: return $this->encoder->encode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: final public function decode($data, $format, array $context = []) Chris@0: { Chris@0: return $this->decoder->decode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@14: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsEncoding($format/*, array $context = []*/) Chris@0: { Chris@14: if (\func_num_args() > 1) { Chris@14: $context = \func_get_arg(1); Chris@14: } else { Chris@14: if (__CLASS__ !== \get_class($this)) { Chris@14: $r = new \ReflectionMethod($this, __FUNCTION__); Chris@14: if (__CLASS__ !== $r->getDeclaringClass()->getName()) { Chris@17: @trigger_error(sprintf('The "%s()" method will have a second `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: } Chris@14: Chris@17: $context = []; Chris@0: } Chris@0: Chris@14: return $this->encoder->supportsEncoding($format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsDecoding($format/*, array $context = []*/) Chris@0: { Chris@14: if (\func_num_args() > 1) { Chris@14: $context = \func_get_arg(1); Chris@14: } else { Chris@14: if (__CLASS__ !== \get_class($this)) { Chris@14: $r = new \ReflectionMethod($this, __FUNCTION__); Chris@14: if (__CLASS__ !== $r->getDeclaringClass()->getName()) { Chris@17: @trigger_error(sprintf('The "%s()" method will have a second `$context = []` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: } Chris@0: Chris@17: $context = []; Chris@14: } Chris@14: Chris@14: return $this->decoder->supportsDecoding($format, $context); Chris@0: } Chris@0: }