Refactor serializer normalize method

This commit is contained in:
Jérôme Vasseur 2016-01-06 22:10:50 +01:00
parent 07673dcd38
commit 2bb5f45aea

View File

@ -127,10 +127,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
if (null === $data || is_scalar($data)) { if (null === $data || is_scalar($data)) {
return $data; return $data;
} }
if (is_object($data) && $this->supportsNormalization($data, $format)) {
return $this->normalizeObject($data, $format, $context); if (is_array($data) || $data instanceof \Traversable) {
}
if ($data instanceof \Traversable) {
$normalized = array(); $normalized = array();
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$normalized[$key] = $this->normalize($val, $format, $context); $normalized[$key] = $this->normalize($val, $format, $context);
@ -138,16 +136,15 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
return $normalized; return $normalized;
} }
if (is_object($data)) { if (is_object($data)) {
return $this->normalizeObject($data, $format, $context); if (!$this->normalizers) {
} throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = $this->normalize($val, $format, $context);
} }
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))); 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); 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. * Denormalizes data back into an object of the given class.
* *