Skip to content

Commit

Permalink
merged branch ericclemmons/2295-serializer-normalizable-traversable (…
Browse files Browse the repository at this point in the history
…PR #2578)

Commits
-------

7346896 Changed Serialized#supportsNormalization to PRIVATE
e851efc Updated SerializerTest with "normalizeTraversable" & "testNormalizeGivesPriorityToInterfaceOverTraversable"
d789f94 Serializer#normalize gives precedence to objects that support normalization
9e6ba9a Added protected Serializer#supportsNormalization

Discussion
----------

[Serializer] `normalize` should use supported normalizer before Traversable

Bug fix: yes
Feature addition: no
Backwards compatibility break: no (discussion needed)
Symfony2 tests pass: yes
Fixes the following tickets: #2295

**Same as PR #2539, except rebased onto `2.0`**

Should I abstract out a `supportsDenormalization` function just for symmetry?
  • Loading branch information
fabpot committed Nov 8, 2011
2 parents 1a5b923 + 682c218 commit b316d4f
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public function normalize($data, $format = null)
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_object($data) && $this->supportsNormalization($data, $format)) {
return $this->normalizeObject($data, $format);
}
if ($data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
Expand Down Expand Up @@ -153,17 +156,14 @@ private function normalizeObject($object, $format = null)
if (!$this->normalizers) {
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])) {

// If normalization is supported, cached normalizer will exist
if ($this->supportsNormalization($object, $format)) {
return $this->normalizerCache[$class][$format]->normalize($object, $format);
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsNormalization($object, $class, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;

return $normalizer->normalize($object, $format);
}
}
throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
}

Expand Down Expand Up @@ -193,6 +193,31 @@ private function denormalizeObject($data, $class, $format = null)
throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
}

/**
* Check if normalizer cache or normalizers supports provided object, which will then be cached
*
* @param object $object Object to test for normalization support
* @param string $format Format name, needed for normalizers to pivot on
*/
private function supportsNormalization($object, $format)
{
$class = get_class($object);

if (isset($this->normalizerCache[$class][$format])) {
return true;
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;

return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit b316d4f

Please sign in to comment.