src/Car/Serializer/Normalizer/CarNormalizer.php line 41
<?phpdeclare(strict_types=1);/*** Copyright (c) 2023 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2023 TECLA Consulting Group oü*/namespace App\Car\Serializer\Normalizer;use App\Car\Model\CarInterface;use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;use Symfony\Component\Serializer\Normalizer\NormalizerInterface;use Twig\Environment;final class CarNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface{private NormalizerInterface $normalizer;private Environment $twig;public function __construct(NormalizerInterface $normalizer,Environment $twig) {$this->normalizer = $normalizer;$this->twig = $twig;}public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null{$data = $this->normalizer->normalize($object, $format, $context);$data = $this->normalizeHtml($object, $context, $data);return $data;}public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool{return $data instanceof CarInterface;}public function hasCacheableSupportsMethod(): bool{return __CLASS__ === self::class;}private function getGroups(array $context): array{$groups = $context[AbstractNormalizer::GROUPS] ?? [];return \is_scalar($groups) ? (array) $groups : $groups;}private function normalizeHtml(CarInterface $car, array $context, array $data): array{$groups = $this->getGroups($context);if (!\in_array('car.list', $groups)) {return $data;}$html = $this->twig->load('packages/car/fragment/_car_list_row.html.twig');$data['_html'] = $html->render(['car' => $car]);return $data;}}