Skip to content

Commit ba0a5dd

Browse files
committed
Refactor serializer normalize method
1 parent 7f755cd commit ba0a5dd

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

Serializer.php

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,24 @@ public function normalize($data, $format = null, array $context = array())
127127
if (null === $data || is_scalar($data)) {
128128
return $data;
129129
}
130-
if (is_object($data) && $this->supportsNormalization($data, $format)) {
131-
return $this->normalizeObject($data, $format, $context);
132-
}
133-
if ($data instanceof \Traversable) {
130+
131+
if (is_array($data) || $data instanceof \Traversable) {
134132
$normalized = array();
135133
foreach ($data as $key => $val) {
136134
$normalized[$key] = $this->normalize($val, $format, $context);
137135
}
138136

139137
return $normalized;
140138
}
139+
141140
if (is_object($data)) {
142-
return $this->normalizeObject($data, $format, $context);
143-
}
144-
if (is_array($data)) {
145-
foreach ($data as $key => $val) {
146-
$data[$key] = $this->normalize($val, $format, $context);
141+
if (!$this->normalizers) {
142+
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
147143
}
148144

149-
return $data;
145+
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
150146
}
147+
151148
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
152149
}
153150

@@ -230,31 +227,6 @@ final public function decode($data, $format, array $context = array())
230227
return $this->decoder->decode($data, $format, $context);
231228
}
232229

233-
/**
234-
* Normalizes an object into a set of arrays/scalars.
235-
*
236-
* @param object $object object to normalize
237-
* @param string $format format name, present to give the option to normalizers to act differently based on formats
238-
* @param array $context The context data for this particular normalization
239-
*
240-
* @return array|string|bool|int|float|null
241-
*
242-
* @throws LogicException
243-
* @throws UnexpectedValueException
244-
*/
245-
private function normalizeObject($object, $format, array $context = array())
246-
{
247-
if (!$this->normalizers) {
248-
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
249-
}
250-
251-
if ($normalizer = $this->getNormalizer($object, $format)) {
252-
return $normalizer->normalize($object, $format, $context);
253-
}
254-
255-
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object)));
256-
}
257-
258230
/**
259231
* Denormalizes data back into an object of the given class.
260232
*

0 commit comments

Comments
 (0)