src/Contact/Serializer/Normalizer/ContactDtoNormalizer.php line 39

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2023 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 2023 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\Serializer\Normalizer;
  18. use App\Contact\Dto\ContactDto;
  19. use App\Contact\Model\ContactInterface;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  22. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  23. final class ContactDtoNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  24. {
  25.     private NormalizerInterface $normalizer;
  26.     private UrlGeneratorInterface $urlGenerator;
  27.     public function __construct(NormalizerInterface $normalizerUrlGeneratorInterface $urlGenerator)
  28.     {
  29.         $this->normalizer $normalizer;
  30.         $this->urlGenerator $urlGenerator;
  31.     }
  32.     public function normalize(mixed $objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  33.     {
  34.         $data $this->normalizer->normalize($object$format$context);
  35.         $data['_links'] = $this->getLinks($object);
  36.         /** @var $object ContactDto */
  37.         if (ContactInterface::TYPE_COMPANY === $object->getType()) {
  38.             $data['fullName'] = $object->getName();
  39.         }
  40.         if (ContactInterface::TYPE_PERSON === $object->getType()) {
  41.             $data['fullName'] = $object->getLastName() . ', ' $object->getFirstName();
  42.         }
  43.         return $data;
  44.     }
  45.     public function supportsNormalization(mixed $data, ?string $format null, array $context = []): bool
  46.     {
  47.         return $data instanceof ContactDto;
  48.     }
  49.     public function hasCacheableSupportsMethod(): bool
  50.     {
  51.         return __CLASS__ === self::class;
  52.     }
  53.     private function getLinks(ContactDto $contactDto): array
  54.     {
  55.         return [
  56.             'ajax' => [
  57.                 'invoiceFormInfo' => [
  58.                     'href' => $this->urlGenerator->generate('app.invoice.ajax.invoice_draft.customer.info', [
  59.                         'contactId' => $contactDto->getId(),
  60.                     ]),
  61.                 ],
  62.             ],
  63.             'http' => [
  64.                 'view' => [
  65.                     'href' => $this->urlGenerator->generate('app.contact.http.contact.view', [
  66.                         'contactId' => $contactDto->getId(),
  67.                     ]),
  68.                 ],
  69.             ],
  70.         ];
  71.     }
  72. }