|
@@ -4,6 +4,8 @@ namespace Symfony\Component\Serializer;
|
|
|
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
|
|
|
+use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
|
|
|
+use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
|
|
|
|
|
|
/*
|
|
/*
|
|
* This file is part of the Symfony framework.
|
|
* This file is part of the Symfony framework.
|
|
@@ -30,24 +32,22 @@ class Serializer implements SerializerInterface
|
|
{
|
|
{
|
|
private $normalizers = array();
|
|
private $normalizers = array();
|
|
private $encoders = array();
|
|
private $encoders = array();
|
|
|
|
+ private $decoders = array();
|
|
protected $normalizerCache = array();
|
|
protected $normalizerCache = array();
|
|
protected $denormalizerCache = array();
|
|
protected $denormalizerCache = array();
|
|
|
|
|
|
- /**
|
|
|
|
- * @param mixed $value value to test
|
|
|
|
- * @return Boolean whether the type is a structured type (array + objects)
|
|
|
|
- */
|
|
|
|
- public function isStructuredType($value)
|
|
|
|
- {
|
|
|
|
- return null !== $value && !is_scalar($value);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* {@inheritdoc}
|
|
* {@inheritdoc}
|
|
*/
|
|
*/
|
|
public function serialize($data, $format)
|
|
public function serialize($data, $format)
|
|
{
|
|
{
|
|
- return $this->encode($this->normalize($data, $format), $format);
|
|
|
|
|
|
+ if (!isset($this->encoders[$format])) {
|
|
|
|
+ throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
|
|
|
+ }
|
|
|
|
+ if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
|
|
|
|
+ $data = $this->normalize($data);
|
|
|
|
+ }
|
|
|
|
+ return $this->encode($data, $format);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -57,53 +57,12 @@ class Serializer implements SerializerInterface
|
|
return $this->denormalize($this->decode($data, $format), $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}
|
|
* {@inheritdoc}
|
|
*/
|
|
*/
|
|
public function normalize($data, $format = null)
|
|
public function normalize($data, $format = null)
|
|
{
|
|
{
|
|
- if (!$this->isStructuredType($data)) {
|
|
|
|
|
|
+ if (null === $data || is_scalar($data)) {
|
|
return $data;
|
|
return $data;
|
|
}
|
|
}
|
|
if ($data instanceof Traversable) {
|
|
if ($data instanceof Traversable) {
|
|
@@ -138,7 +97,7 @@ class Serializer implements SerializerInterface
|
|
*/
|
|
*/
|
|
public function encode($data, $format)
|
|
public function encode($data, $format)
|
|
{
|
|
{
|
|
- if (!$this->hasEncoder($format)) {
|
|
|
|
|
|
+ if (!isset($this->encoders[$format])) {
|
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
|
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
|
|
}
|
|
}
|
|
return $this->encoders[$format]->encode($data, $format);
|
|
return $this->encoders[$format]->encode($data, $format);
|
|
@@ -149,23 +108,75 @@ class Serializer implements SerializerInterface
|
|
*/
|
|
*/
|
|
public function decode($data, $format)
|
|
public function decode($data, $format)
|
|
{
|
|
{
|
|
- if (!$this->hasEncoder($format)) {
|
|
|
|
- throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format');
|
|
|
|
|
|
+ if (!isset($this->decoders[$format])) {
|
|
|
|
+ throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
|
|
}
|
|
}
|
|
- return $this->encoders[$format]->decode($data, $format);
|
|
|
|
|
|
+ return $this->decoders[$format]->decode($data, $format);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+ public 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;
|
|
|
|
+ return $normalizer->normalize($object, $format);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+ 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.');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param NormalizerInterface $normalizer
|
|
*/
|
|
*/
|
|
public function addNormalizer(NormalizerInterface $normalizer)
|
|
public function addNormalizer(NormalizerInterface $normalizer)
|
|
{
|
|
{
|
|
$this->normalizers[] = $normalizer;
|
|
$this->normalizers[] = $normalizer;
|
|
- $normalizer->setSerializer($this);
|
|
|
|
|
|
+ if ($normalizer instanceof SerializerAwareInterface) {
|
|
|
|
+ $normalizer->setSerializer($this);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @return array[]NormalizerInterface
|
|
*/
|
|
*/
|
|
public function getNormalizers()
|
|
public function getNormalizers()
|
|
{
|
|
{
|
|
@@ -173,7 +184,7 @@ class Serializer implements SerializerInterface
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @param NormalizerInterface $normalizer
|
|
*/
|
|
*/
|
|
public function removeNormalizer(NormalizerInterface $normalizer)
|
|
public function removeNormalizer(NormalizerInterface $normalizer)
|
|
{
|
|
{
|
|
@@ -181,16 +192,31 @@ class Serializer implements SerializerInterface
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @param string $format format name
|
|
|
|
+ * @param EncoderInterface $encoder
|
|
*/
|
|
*/
|
|
public function setEncoder($format, EncoderInterface $encoder)
|
|
public function setEncoder($format, EncoderInterface $encoder)
|
|
{
|
|
{
|
|
$this->encoders[$format] = $encoder;
|
|
$this->encoders[$format] = $encoder;
|
|
- $encoder->setSerializer($this);
|
|
|
|
|
|
+ if ($encoder instanceof SerializerAwareInterface) {
|
|
|
|
+ $encoder->setSerializer($this);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @param string $format format name
|
|
|
|
+ * @param DecoderInterface $decoder
|
|
|
|
+ */
|
|
|
|
+ public function setDecoder($format, DecoderInterface $decoder)
|
|
|
|
+ {
|
|
|
|
+ $this->decoders[$format] = $decoder;
|
|
|
|
+ if ($decoder instanceof SerializerAwareInterface) {
|
|
|
|
+ $decoder->setSerializer($this);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return array[]EncoderInterface
|
|
*/
|
|
*/
|
|
public function getEncoders()
|
|
public function getEncoders()
|
|
{
|
|
{
|
|
@@ -198,7 +224,15 @@ class Serializer implements SerializerInterface
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @return array[]DecoderInterface
|
|
|
|
+ */
|
|
|
|
+ public function getDecoders()
|
|
|
|
+ {
|
|
|
|
+ return $this->decoders;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return EncoderInterface
|
|
*/
|
|
*/
|
|
public function getEncoder($format)
|
|
public function getEncoder($format)
|
|
{
|
|
{
|
|
@@ -206,7 +240,18 @@ class Serializer implements SerializerInterface
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * @return DecoderInterface
|
|
|
|
+ */
|
|
|
|
+ public function getDecoder($format)
|
|
|
|
+ {
|
|
|
|
+ return $this->decoders[$format];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Checks whether the serializer has an encoder registered for the given format
|
|
|
|
+ *
|
|
|
|
+ * @param string $format format name
|
|
|
|
+ * @return Boolean
|
|
*/
|
|
*/
|
|
public function hasEncoder($format)
|
|
public function hasEncoder($format)
|
|
{
|
|
{
|
|
@@ -214,10 +259,29 @@ class Serializer implements SerializerInterface
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * {@inheritdoc}
|
|
|
|
|
|
+ * Checks whether the serializer has a decoder registered for the given format
|
|
|
|
+ *
|
|
|
|
+ * @param string $format format name
|
|
|
|
+ * @return Boolean
|
|
|
|
+ */
|
|
|
|
+ public function hasDecoder($format)
|
|
|
|
+ {
|
|
|
|
+ return isset($this->decoders[$format]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param string $format format name
|
|
*/
|
|
*/
|
|
public function removeEncoder($format)
|
|
public function removeEncoder($format)
|
|
{
|
|
{
|
|
unset($this->encoders[$format]);
|
|
unset($this->encoders[$format]);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param string $format format name
|
|
|
|
+ */
|
|
|
|
+ public function removeDecoder($format)
|
|
|
|
+ {
|
|
|
|
+ unset($this->decoders[$format]);
|
|
|
|
+ }
|
|
}
|
|
}
|