Skip to content

Commit e4745cc

Browse files
authored
Merge pull request #633 from TimoBakx/less-complex-normalizer
Less complex normalizer
2 parents 7371446 + edf208f commit e4745cc

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

core/serialization.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ services:
438438
```
439439

440440
The Normalizer class is a bit harder to understand, because it must ensure that it is only called once and that there is no recursion.
441-
To accomplish this, it needs to be aware of the Serializer instance itself.
441+
To accomplish this, it needs to be aware of the parent Normalizer instance itself.
442442

443443
Here is an example:
444444

@@ -450,15 +450,15 @@ namespace App\Serializer;
450450
451451
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
452452
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
453+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
454+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
453455
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
454-
use Symfony\Component\Serializer\SerializerAwareInterface;
455-
use Symfony\Component\Serializer\SerializerAwareTrait;
456456
457-
class BookAttributeNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface
457+
class BookAttributeNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
458458
{
459-
use SerializerAwareTrait;
459+
use NormalizerAwareTrait;
460460
461-
private const BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED = 'BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED';
461+
private const ALREADY_CALLED = 'BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED';
462462
463463
private $tokenStorage;
464464
@@ -473,13 +473,15 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Serial
473473
$context['groups'][] = 'can_retrieve_book';
474474
}
475475
476-
return $this->passOn($object, $format, $context);
476+
$context[self::ALREADY_CALLED] = true;
477+
478+
return $this->normalizer->normalize($object, $format, $context);
477479
}
478480
479481
public function supportsNormalization($data, $format = null, array $context = [])
480482
{
481483
// Make sure we're not called twice
482-
if (isset($context[self::BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED])) {
484+
if (isset($context[self::ALREADY_CALLED])) {
483485
return false;
484486
}
485487
@@ -492,17 +494,6 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Serial
492494
// for the current $object (book) and
493495
// return true or false
494496
}
495-
496-
private function passOn($object, $format = null, array $context = [])
497-
{
498-
if (!$this->serializer instanceof NormalizerInterface) {
499-
throw new \LogicException(sprintf('Cannot normalize object "%s" because the injected serializer is not a normalizer', $object));
500-
}
501-
502-
$context[self::BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED] = true;
503-
504-
return $this->serializer->normalize($object, $format, $context);
505-
}
506497
}
507498
```
508499

0 commit comments

Comments
 (0)