src/Contact/Serializer/Normalizer/ContactRelationshipCompanyContactPersonNormalizer.php line 57

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\Serializer\Normalizer;
  18. use App\Contact\Model\ContactRelationshipCompanyContactPersonInterface;
  19. use App\Core\Serializer\Normalizer\AttributeAwareNormalizerTrait;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  22. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  23. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  24. final class ContactRelationshipCompanyContactPersonNormalizer implements ContextAwareNormalizerInterfaceCacheableSupportsMethodInterface
  25. {
  26.     use AttributeAwareNormalizerTrait;
  27.     private NormalizerInterface $normalizer;
  28.     private UrlGeneratorInterface $urlGenerator;
  29.     public function __construct(NormalizerInterface $normalizerUrlGeneratorInterface $urlGenerator)
  30.     {
  31.         $this->normalizer $normalizer;
  32.         $this->urlGenerator $urlGenerator;
  33.     }
  34.     public function normalize($objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  35.     {
  36.         $data $this->normalizer->normalize($object$format$context);
  37.         if (true === $this->shouldSerializeAttribute($dataContactRelationshipCompanyContactPersonInterface::ATTR_IS_DEFAULT$context)) {
  38.             $this->normalizeIsDefault($object$data);
  39.         }
  40.         if (true === $this->shouldSerializeAttribute($dataContactRelationshipCompanyContactPersonInterface::ATTR__LINKS$context)) {
  41.             $this->normalize_Links($object$data);
  42.         }
  43.         return $data;
  44.     }
  45.     public function supportsNormalization($datastring $format null, array $context = []): bool
  46.     {
  47.         return $data instanceof ContactRelationshipCompanyContactPersonInterface;
  48.     }
  49.     public function hasCacheableSupportsMethod(): bool
  50.     {
  51.         return __CLASS__ === static::class;
  52.     }
  53.     private function normalizeIsDefault(ContactRelationshipCompanyContactPersonInterface $relationship, array &$data): void
  54.     {
  55.         $data[ContactRelationshipCompanyContactPersonInterface::ATTR_IS_DEFAULT] = $relationship === $relationship->getCompany()->getDefaultRelationshipCompanyContactPerson();
  56.     }
  57.     private function normalize_Links(ContactRelationshipCompanyContactPersonInterface $relationship, array &$data): void
  58.     {
  59.         $data[ContactRelationshipCompanyContactPersonInterface::ATTR__LINKS] = [
  60.             'ajax' => [
  61.                 'edit' => $this->urlGenerator->generate('app.contact.ajax.contact_relationship_contact_person.edit', [
  62.                     'contactId' => $relationship->getCompany()->getId(),
  63.                     'relationshipId' => $relationship->getId(),
  64.                 ]),
  65.                 'remove' => $this->urlGenerator->generate('app.contact.ajax.contact_relationship_contact_person.remove', [
  66.                     'contactId' => $relationship->getCompany()->getId(),
  67.                     'relationshipId' => $relationship->getId(),
  68.                 ]),
  69.             ],
  70.         ];
  71.     }
  72. }