|
@@ -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
|
|
@@ -68,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);
|
|
|
}
|
|
@@ -86,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);
|
|
|
}
|
|
|
}
|
|
@@ -107,22 +106,22 @@ class Serializer implements SerializerInterface
|
|
|
if (!$this->isStructuredType($data)) {
|
|
|
return $data;
|
|
|
}
|
|
|
- if (is_array($data)) {
|
|
|
- foreach ($data as $key => $val) {
|
|
|
- $data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
|
|
|
- }
|
|
|
- return $data;
|
|
|
- }
|
|
|
if ($data instanceof Traversable) {
|
|
|
$normalized = array();
|
|
|
foreach ($data as $key => $val) {
|
|
|
- $normalized[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
|
|
|
+ $normalized[$key] = $this->normalize($val, $format);
|
|
|
}
|
|
|
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));
|
|
|
}
|
|
|
|