src/Serializer/OrganizationNormalizer.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Serializer;
  3. use App\Service\SubscriptionUtils;
  4. use Evo\Infrastructure\MappingORM\Organization;
  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 OrganizationNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  10. {
  11.     use NormalizerAwareTrait;
  12.     private const ALREADY_CALLED 'ORGANIZATION_NORMALIZER_ALREADY_CALLED';
  13.     private SubscriptionUtils $subscriptionUtils;
  14.     public function __construct(
  15.         SubscriptionUtils $subscriptionUtils
  16.     ) {
  17.         $this->subscriptionUtils $subscriptionUtils;
  18.     }
  19.     /**
  20.      * @param Organization $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 Organization) && is_array($normalizedData)) {
  29.             $packs = [];
  30.             foreach ($object->getSubscriptions() as $subscription) {
  31.                 $packs[] = $this->subscriptionUtils->getPackUniqueKey($subscription);
  32.             }
  33.             if (isset($context['groups']) && 'read_search' === $context['groups'][0]) {
  34.                 $normalizedData['pack'] = implode(','$packs);
  35.             }
  36.         }
  37.         return $normalizedData;
  38.     }
  39.     public function supportsNormalization($data$format null, array $context = []): bool
  40.     {
  41.         if (isset($context[self::ALREADY_CALLED])) {
  42.             return false;
  43.         }
  44.         return $data instanceof Organization;
  45.     }
  46. }