. */ namespace App\Serializer; use App\Entity\Parts\Part; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; class PartNormalizer implements ContextAwareNormalizerInterface { private NormalizerInterface $normalizer; public function __construct(ObjectNormalizer $normalizer) { $this->normalizer = $normalizer; } public function supportsNormalization($data, string $format = null, array $context = []) { return $data instanceof Part; } public function normalize($object, string $format = null, array $context = []) { if (!$object instanceof Part) { throw new \InvalidArgumentException('This normalizer only supports Part objects!'); } $data = $this->normalizer->normalize($object, $format, $context); //Remove type field for CSV export if ($format == 'csv') { unset($data['type']); } $data['total_instock'] = $object->getAmountSum(); return $data; } }