From 1467ae657057d9fd060ed1292b78d28e756a468e Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Sat, 15 Mar 2014 00:57:23 +0100 Subject: [PATCH 1/3] Move normalizer cache to getNormalier()/getDenormalizer(), use those in normalizeObject()/denormalizeObject() --- .../Component/Serializer/Serializer.php | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 73ae9fa47411e..5e0677042ad94 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -168,12 +168,28 @@ public function supportsDenormalization($data, $type, $format = null) } /** - * {@inheritdoc} + * Returns a matching normalizer. + * + * @param $data + * @param null $format + * + * @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 */ 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) { + 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)); } /** From 646050134596dc036b79b4a09de15bf007534bd5 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Sat, 15 Mar 2014 11:43:22 +0100 Subject: [PATCH 2/3] Remove exception from getNormaler()/getDenormalizer(), documentation improvements --- .../Component/Serializer/Serializer.php | 71 +++++++------------ 1 file changed, 25 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 5e0677042ad94..1e21d317b01be 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -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,13 +143,7 @@ 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); } /** @@ -158,27 +151,18 @@ public function supportsNormalization($data, $format = null) */ 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); } /** * 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 * - * @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)); } /** From 24b2ab7fcd2f0519bf59a364f0a47144d8d1a491 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Sat, 15 Mar 2014 14:37:48 +0100 Subject: [PATCH 3/3] Remove wrong null assignment --- src/Symfony/Component/Serializer/Serializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 1e21d317b01be..023d680e34b87 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -151,7 +151,7 @@ public function supportsNormalization($data, $format = null) */ public function supportsDenormalization($data, $type, $format = null) { - return (bool) $this->getDenormalizer($data, $type, $format = null); + return (bool) $this->getDenormalizer($data, $type, $format); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy