@@ -438,7 +438,7 @@ services:
438
438
` ` `
439
439
440
440
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.
442
442
443
443
Here is an example :
444
444
@@ -450,15 +450,15 @@ namespace App\Serializer;
450
450
451
451
use Symfony\C omponent\S ecurity\C ore\A uthentication\T oken\S torage\T okenStorageInterface;
452
452
use Symfony\C omponent\S erializer\N ormalizer\C ontextAwareNormalizerInterface;
453
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareInterface;
454
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareTrait;
453
455
use Symfony\C omponent\S erializer\N ormalizer\N ormalizerInterface;
454
- use Symfony\C omponent\S erializer\S erializerAwareInterface;
455
- use Symfony\C omponent\S erializer\S erializerAwareTrait;
456
456
457
- class BookAttributeNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface
457
+ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
458
458
{
459
- use SerializerAwareTrait ;
459
+ use NormalizerAwareTrait ;
460
460
461
- private const BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED = 'BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED';
461
+ private const ALREADY_CALLED = 'BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED';
462
462
463
463
private $tokenStorage;
464
464
@@ -473,13 +473,15 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Serial
473
473
$context['groups'][] = 'can_retrieve_book';
474
474
}
475
475
476
- return $this->passOn($object, $format, $context);
476
+ $context[self::ALREADY_CALLED] = true;
477
+
478
+ return $this->normalizer->normalize($object, $format, $context);
477
479
}
478
480
479
481
public function supportsNormalization($data, $format = null, array $context = [])
480
482
{
481
483
// Make sure we're not called twice
482
- if (isset($context[self::BOOK_ATTRIBUTE_NORMALIZER_ALREADY_CALLED ])) {
484
+ if (isset($context[self::ALREADY_CALLED ])) {
483
485
return false;
484
486
}
485
487
@@ -492,17 +494,6 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Serial
492
494
// for the current $object (book) and
493
495
// return true or false
494
496
}
495
-
496
- private function passOn($object, $format = null, array $context = [])
497
- {
498
- if (!$this->serializer instanceof NormalizerInterface) {
499
- throw new \L ogicException(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
- }
506
497
}
507
498
` ` `
508
499
0 commit comments