<?php
namespace App\Serializer;
use App\Service\SubscriptionUtils;
use Evo\Infrastructure\MappingORM\Subscription;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class SubscriptionNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const ALREADY_CALLED = 'SUBSCRIPTION_NORMALIZER_ALREADY_CALLED';
private SubscriptionUtils $subscriptionUtils;
public function __construct(
SubscriptionUtils $subscriptionUtils
) {
$this->subscriptionUtils = $subscriptionUtils;
}
/**
* @param Subscription $object
*
* @throws ExceptionInterface
*/
public function normalize($object, $format = null, array $context = [])
{
$context[self::ALREADY_CALLED] = true;
$normalizedData = $this->normalizer->normalize($object, $format, $context);
if (($object instanceof Subscription) && is_array($normalizedData)) {
$packInfos = $this->subscriptionUtils->getSubscriptionPackInfo($object);
$normalizedData['pack'] = $packInfos['uniqueKey'];
$normalizedData['packLabel'] = $packInfos['packName'];
}
return $normalizedData;
}
public function supportsNormalization($data, $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}
return $data instanceof Subscription;
}
}