From 2bb5f45aea15df6824eadf9bf9c10b9145a3ec11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Wed, 6 Jan 2016 22:10:50 +0100 Subject: [PATCH] Refactor serializer normalize method --- .../Component/Serializer/Serializer.php | 42 ++++--------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index eb83544b42..00e8b2b8ce 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -127,10 +127,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz if (null === $data || is_scalar($data)) { return $data; } - if (is_object($data) && $this->supportsNormalization($data, $format)) { - return $this->normalizeObject($data, $format, $context); - } - if ($data instanceof \Traversable) { + + if (is_array($data) || $data instanceof \Traversable) { $normalized = array(); foreach ($data as $key => $val) { $normalized[$key] = $this->normalize($val, $format, $context); @@ -138,16 +136,15 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz return $normalized; } + if (is_object($data)) { - return $this->normalizeObject($data, $format, $context); - } - if (is_array($data)) { - foreach ($data as $key => $val) { - $data[$key] = $this->normalize($val, $format, $context); + if (!$this->normalizers) { + throw new LogicException('You must register at least one normalizer to be able to normalize objects.'); } - return $data; + throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data))); } + throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true))); } @@ -230,31 +227,6 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz return $this->decoder->decode($data, $format, $context); } - /** - * Normalizes an object into a set of arrays/scalars. - * - * @param object $object object to normalize - * @param string $format format name, present to give the option to normalizers to act differently based on formats - * @param array $context The context data for this particular normalization - * - * @return array|string|bool|int|float|null - * - * @throws LogicException - * @throws UnexpectedValueException - */ - 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.'); - } - - 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))); - } - /** * Denormalizes data back into an object of the given class. *