Move normalizer cache to getNormalier()/getDenormalizer(), use those in normalizeObject()/denormalizeObject()
This commit is contained in:
parent
5a4885edb4
commit
e7389aa24c
@ -17,7 +17,6 @@ use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
|||||||
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
|
||||||
use Symfony\Component\Serializer\Exception\LogicException;
|
use Symfony\Component\Serializer\Exception\LogicException;
|
||||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
@ -144,13 +143,7 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
|
|||||||
*/
|
*/
|
||||||
public function supportsNormalization($data, $format = null)
|
public function supportsNormalization($data, $format = null)
|
||||||
{
|
{
|
||||||
try {
|
return (bool) $this->getNormalizer($data, $format);
|
||||||
$this->getNormalizer($data, $format);
|
|
||||||
} catch (RuntimeException $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -158,41 +151,60 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
|
|||||||
*/
|
*/
|
||||||
public function supportsDenormalization($data, $type, $format = null)
|
public function supportsDenormalization($data, $type, $format = null)
|
||||||
{
|
{
|
||||||
try {
|
return (bool) $this->getDenormalizer($data, $type, $format);
|
||||||
$this->getDenormalizer($data, $type, $format = null);
|
|
||||||
} catch (RuntimeException $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* Returns a matching normalizer.
|
||||||
|
*
|
||||||
|
* @param object $data The object to get the serializer for
|
||||||
|
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||||
|
*
|
||||||
|
* @return NormalizerInterface|null
|
||||||
*/
|
*/
|
||||||
private function getNormalizer($data, $format = null)
|
private function getNormalizer($data, $format)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$class = get_class($data);
|
||||||
|
if (isset($this->normalizerCache[$class][$format])) {
|
||||||
|
return $this->normalizerCache[$class][$format];
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->normalizers as $normalizer) {
|
foreach ($this->normalizers as $normalizer) {
|
||||||
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
|
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
|
||||||
|
$this->normalizerCache[$class][$format] = $normalizer;
|
||||||
|
|
||||||
return $normalizer;
|
return $normalizer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format));
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* Returns a matching denormalizer.
|
||||||
|
*
|
||||||
|
* @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 DenormalizerInterface|null
|
||||||
*/
|
*/
|
||||||
private function getDenormalizer($data, $type, $format = null)
|
private function getDenormalizer($data, $class, $format)
|
||||||
{
|
{
|
||||||
|
if (isset($this->denormalizerCache[$class][$format])) {
|
||||||
|
return $this->denormalizerCache[$class][$format];
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->normalizers as $normalizer) {
|
foreach ($this->normalizers as $normalizer) {
|
||||||
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format)) {
|
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
|
||||||
|
$this->denormalizerCache[$class][$format] = $normalizer;
|
||||||
|
|
||||||
return $normalizer;
|
return $normalizer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format));
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -223,27 +235,16 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
|
|||||||
* @throws LogicException
|
* @throws LogicException
|
||||||
* @throws UnexpectedValueException
|
* @throws UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
private function normalizeObject($object, $format = null, array $context = array())
|
private function normalizeObject($object, $format, array $context = array())
|
||||||
{
|
{
|
||||||
if (!$this->normalizers) {
|
if (!$this->normalizers) {
|
||||||
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
|
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$class = get_class($object);
|
if ($normalizer = $this->getNormalizer($object, $format)) {
|
||||||
if (isset($this->normalizerCache[$class][$format])) {
|
return $normalizer->normalize($object, $format, $context);
|
||||||
return $this->normalizerCache[$class][$format]->normalize($object, $format, $context);
|
|
||||||
}
|
}
|
||||||
|
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object)));
|
||||||
foreach ($this->normalizers as $normalizer) {
|
|
||||||
if ($normalizer instanceof NormalizerInterface
|
|
||||||
&& $normalizer->supportsNormalization($object, $format)) {
|
|
||||||
$this->normalizerCache[$class][$format] = $normalizer;
|
|
||||||
|
|
||||||
return $normalizer->normalize($object, $format, $context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', $class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -259,25 +260,15 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
|
|||||||
* @throws LogicException
|
* @throws LogicException
|
||||||
* @throws UnexpectedValueException
|
* @throws UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
private function denormalizeObject($data, $class, $format = null, array $context = array())
|
private function denormalizeObject($data, $class, $format, array $context = array())
|
||||||
{
|
{
|
||||||
if (!$this->normalizers) {
|
if (!$this->normalizers) {
|
||||||
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->denormalizerCache[$class][$format])) {
|
if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
|
||||||
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format, $context);
|
return $normalizer->denormalize($data, $class, $format, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->normalizers as $normalizer) {
|
|
||||||
if ($normalizer instanceof DenormalizerInterface
|
|
||||||
&& $normalizer->supportsDenormalization($data, $class, $format)) {
|
|
||||||
$this->denormalizerCache[$class][$format] = $normalizer;
|
|
||||||
|
|
||||||
return $normalizer->denormalize($data, $class, $format, $context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
|
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user