Skip to content

Commit

Permalink
Added protected Serializer#supportsNormalization
Browse files Browse the repository at this point in the history
This is very useful for cleaning up the logic in Serializer#normalize
and allow easy checking of both the cache & stored normalizers
  • Loading branch information
ericclemmons committed Nov 8, 2011
1 parent fc97472 commit 9e6ba9a
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,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 +190,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
*/
protected 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 9e6ba9a

Please sign in to comment.