src/Serializer/SubscriptionNormalizer.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Serializer;
  3. use App\Service\SubscriptionUtils;
  4. use Evo\Infrastructure\MappingORM\Subscription;
  5. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  6. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  7. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  8. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  9. class SubscriptionNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  10. {
  11.     use NormalizerAwareTrait;
  12.     private const ALREADY_CALLED 'SUBSCRIPTION_NORMALIZER_ALREADY_CALLED';
  13.     private SubscriptionUtils $subscriptionUtils;
  14.     public function __construct(
  15.         SubscriptionUtils $subscriptionUtils
  16.     ) {
  17.         $this->subscriptionUtils $subscriptionUtils;
  18.     }
  19.     /**
  20.      * @param Subscription $object
  21.      *
  22.      * @throws ExceptionInterface
  23.      */
  24.     public function normalize($object$format null, array $context = [])
  25.     {
  26.         $context[self::ALREADY_CALLED] = true;
  27.         $normalizedData $this->normalizer->normalize($object$format$context);
  28.         if (($object instanceof Subscription) && is_array($normalizedData)) {
  29.             $packInfos $this->subscriptionUtils->getSubscriptionPackInfo($object);
  30.             $normalizedData['pack'] = $packInfos['uniqueKey'];
  31.             $normalizedData['packLabel'] = $packInfos['packName'];
  32.         }
  33.         return $normalizedData;
  34.     }
  35.     public function supportsNormalization($data$format null, array $context = []): bool
  36.     {
  37.         if (isset($context[self::ALREADY_CALLED])) {
  38.             return false;
  39.         }
  40.         return $data instanceof Subscription;
  41.     }
  42. }