-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Unify usage of normalizer cache #10457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…improvements
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Exception\RuntimeException; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
|
@@ -144,41 +143,26 @@ public function denormalize($data, $type, $format = null, array $context = array | |
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
try { | ||
$this->getNormalizer($data, $format); | ||
} catch (RuntimeException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return (bool) $this->getNormalizer($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
try { | ||
$this->getDenormalizer($data, $type, $format = null); | ||
} catch (RuntimeException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return (bool) $this->getDenormalizer($data, $type, $format = null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy-paste issue?
|
||
} | ||
|
||
/** | ||
* Returns a matching normalizer. | ||
* | ||
* @param $data | ||
* @param null $format | ||
* | ||
* @return mixed | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. string|null from what I see in supportsNormalization. also description not aligned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the format is sometimes null. But I'm not sure what that would even mean? Because the normalizers AFAIK check for a specific format and passing NULL will not find any? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you could have a 'default' normalizer/denormalizer that would be called last if this was NULL, as a fallback? |
||
* | ||
* @throws Exception\LogicException | ||
* @throws Exception\UnexpectedValueException | ||
* @return NormalizerInterface|null | ||
*/ | ||
private function getNormalizer($data, $format = null) | ||
private function getNormalizer($data, $format) | ||
{ | ||
|
||
$class = get_class($data); | ||
|
@@ -194,36 +178,33 @@ private function getNormalizer($data, $format = null) | |
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format)); | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns a matching denormalizer. | ||
* | ||
* @param $data | ||
* @param $type | ||
* @param null $format | ||
* | ||
* @return mixed | ||
* @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 | ||
* | ||
* @throws Exception\LogicException | ||
* @throws Exception\UnexpectedValueException | ||
* @return DenormalizerInterface|null | ||
*/ | ||
private function getDenormalizer($data, $type, $format = null) | ||
private function getDenormalizer($data, $class, $format) | ||
{ | ||
if (isset($this->denormalizerCache[$type][$format])) { | ||
return $this->denormalizerCache[$type][$format]; | ||
if (isset($this->denormalizerCache[$class][$format])) { | ||
return $this->denormalizerCache[$class][$format]; | ||
} | ||
|
||
foreach ($this->normalizers as $normalizer) { | ||
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format)) { | ||
$this->denormalizerCache[$type][$format] = $normalizer; | ||
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) { | ||
$this->denormalizerCache[$class][$format] = $normalizer; | ||
|
||
return $normalizer; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format)); | ||
return null; | ||
} | ||
|
||
/** | ||
|
@@ -254,17 +235,16 @@ final public function decode($data, $format, array $context = array()) | |
* @throws LogicException | ||
* @throws UnexpectedValueException | ||
*/ | ||
private function normalizeObject($object, $format = null, array $context = array()) | ||
private function normalizeObject($object, $format, array $context = array()) | ||
{ | ||
if (!$this->normalizers) { | ||
throw new LogicException('You must register at least one normalizer to be able to normalize objects.'); | ||
} | ||
|
||
try { | ||
return $this->getNormalizer($object, $format)->normalize($object, $format, $context); | ||
} catch (RuntimeException $e) { | ||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object))); | ||
if ($normalizer = $this->getNormalizer($object, $format)) { | ||
return $normalizer->normalize($object, $format, $context); | ||
} | ||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object))); | ||
} | ||
|
||
/** | ||
|
@@ -280,17 +260,16 @@ private function normalizeObject($object, $format = null, array $context = array | |
* @throws LogicException | ||
* @throws UnexpectedValueException | ||
*/ | ||
private function denormalizeObject($data, $class, $format = null, array $context = array()) | ||
private function denormalizeObject($data, $class, $format, array $context = array()) | ||
{ | ||
if (!$this->normalizers) { | ||
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.'); | ||
} | ||
|
||
try { | ||
return $this->getDenormalizer($data, $class, $format)->denormalize($data, $class, $format, $context); | ||
} catch (RuntimeException $e) { | ||
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class)); | ||
if ($normalizer = $this->getDenormalizer($data, $class, $format)) { | ||
return $normalizer->denormalize($data, $class, $format, $context); | ||
} | ||
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class)); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use
null !==
instead of casting the object top boolean.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
is_null()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistence with symfony coding standards