|
@@ -31,6 +31,7 @@ class Serializer implements SerializerInterface
|
|
|
private $normalizers = array();
|
|
|
private $encoders = array();
|
|
|
protected $normalizerCache = array();
|
|
|
+ protected $denormalizerCache = array();
|
|
|
|
|
|
/**
|
|
|
* @param mixed $value value to test
|
|
@@ -46,7 +47,14 @@ class Serializer implements SerializerInterface
|
|
|
*/
|
|
|
public function serialize($data, $format)
|
|
|
{
|
|
|
- return $this->encode($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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -61,9 +69,8 @@ class Serializer implements SerializerInterface
|
|
|
if (isset($this->normalizerCache[$class][$format])) {
|
|
|
return $this->normalizerCache[$class][$format]->normalize($object, $format, $properties);
|
|
|
}
|
|
|
- $reflClass = new \ReflectionClass($class);
|
|
|
foreach ($this->normalizers as $normalizer) {
|
|
|
- if ($normalizer->supports($reflClass, $format)) {
|
|
|
+ if ($normalizer->supportsNormalization($object, $class, $format)) {
|
|
|
$this->normalizerCache[$class][$format] = $normalizer;
|
|
|
return $normalizer->normalize($object, $format, $properties);
|
|
|
}
|
|
@@ -79,13 +86,12 @@ class Serializer implements SerializerInterface
|
|
|
if (!$this->normalizers) {
|
|
|
throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
|
|
}
|
|
|
- if (isset($this->normalizerCache[$class][$format])) {
|
|
|
- return $this->normalizerCache[$class][$format]->denormalize($data, $class, $format);
|
|
|
+ if (isset($this->denormalizerCache[$class][$format])) {
|
|
|
+ return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
|
|
|
}
|
|
|
- $reflClass = new \ReflectionClass($class);
|
|
|
foreach ($this->normalizers as $normalizer) {
|
|
|
- if ($normalizer->supports($reflClass, $format)) {
|
|
|
- $this->normalizerCache[$class][$format] = $normalizer;
|
|
|
+ if ($normalizer->supportsDenormalization($class, $format)) {
|
|
|
+ $this->denormalizerCache[$class][$format] = $normalizer;
|
|
|
return $normalizer->denormalize($data, $class, $format);
|
|
|
}
|
|
|
}
|
|
@@ -95,20 +101,38 @@ class Serializer implements SerializerInterface
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
- public function normalize($data, $format)
|
|
|
+ public function normalize($data, $format = null)
|
|
|
{
|
|
|
- if (is_array($data)) {
|
|
|
+ if (!$this->isStructuredType($data)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ if ($data instanceof Traversable) {
|
|
|
+ $normalized = array();
|
|
|
foreach ($data as $key => $val) {
|
|
|
- $data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
|
|
|
+ $normalized[$key] = $this->normalize($val, $format);
|
|
|
}
|
|
|
- return $data;
|
|
|
+ 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}
|
|
|
*/
|