-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
…in normalizeObject()/denormalizeObject()
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,12 +168,28 @@ public function supportsDenormalization($data, $type, $format = null) | |
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Returns a matching normalizer. | ||
* | ||
* @param $data | ||
* @param null $format | ||
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.
|
||
* | ||
* @return mixed | ||
* | ||
* @throws Exception\LogicException | ||
* @throws Exception\UnexpectedValueException | ||
*/ | ||
private function getNormalizer($data, $format = null) | ||
{ | ||
|
||
$class = get_class($data); | ||
if (isset($this->normalizerCache[$class][$format])) { | ||
return $this->normalizerCache[$class][$format]; | ||
} | ||
|
||
foreach ($this->normalizers as $normalizer) { | ||
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) { | ||
$this->normalizerCache[$class][$format] = $normalizer; | ||
|
||
return $normalizer; | ||
} | ||
} | ||
|
@@ -182,12 +198,27 @@ private function getNormalizer($data, $format = null) | |
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Returns a matching denormalizer. | ||
* | ||
* @param $data | ||
* @param $type | ||
* @param null $format | ||
* | ||
* @return mixed | ||
* | ||
* @throws Exception\LogicException | ||
* @throws Exception\UnexpectedValueException | ||
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. RuntimeException missing |
||
*/ | ||
private function getDenormalizer($data, $type, $format = null) | ||
{ | ||
if (isset($this->denormalizerCache[$type][$format])) { | ||
return $this->denormalizerCache[$type][$format]; | ||
} | ||
|
||
foreach ($this->normalizers as $normalizer) { | ||
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format)) { | ||
$this->denormalizerCache[$type][$format] = $normalizer; | ||
|
||
return $normalizer; | ||
} | ||
} | ||
|
@@ -229,21 +260,11 @@ private function normalizeObject($object, $format = null, array $context = array | |
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, $context); | ||
} | ||
|
||
foreach ($this->normalizers as $normalizer) { | ||
if ($normalizer instanceof NormalizerInterface | ||
&& $normalizer->supportsNormalization($object, $format)) { | ||
$this->normalizerCache[$class][$format] = $normalizer; | ||
|
||
return $normalizer->normalize($object, $format, $context); | ||
} | ||
try { | ||
return $this->getNormalizer($object, $format)->normalize($object, $format, $context); | ||
} catch (RuntimeException $e) { | ||
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. This could swallow other RuntimeExceptions thrown within the normaler classes... Maybe better return null in getNormalizer() and throw here |
||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object))); | ||
} | ||
|
||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', $class)); | ||
} | ||
|
||
/** | ||
|
@@ -265,20 +286,11 @@ private function denormalizeObject($data, $class, $format = null, array $context | |
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, $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); | ||
} | ||
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)); | ||
} | ||
|
||
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.
The type is missing here