How to use (chain?) multiple normalizers with Symfony Serializer? How to use (chain?) multiple normalizers with Symfony Serializer? symfony symfony

How to use (chain?) multiple normalizers with Symfony Serializer?


You have to use the NormalizerAwareTrait so you can access the normalizer for books

  • add interface
  • use trait
  • call normalize() method for books

code:

class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface{    use NormalizerAwareTrait;    public function normalize($object, $format = null, array $context = array())    {        return [            'name' => $object->getName(),            'books' => $this->normalizer->normalize($object->getBooks(), $format, $context)        ];    }    public function supportsNormalization($data, $format = null)    {        return $data instanceof User;    }}