src/Contact/Serializer/Normalizer/ContactNormalizer.php line 42

  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\ContactInterface;
  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 ContactNormalizer 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 = []): float|int|bool|\ArrayObject|array|string|null
  35.     {
  36.         $data $this->normalizer->normalize($object$format$context);
  37.         if (true === $this->shouldSerializeAttribute($dataContactInterface::ATTR_DEFAULT_EMAIL$context)) {
  38.             $this->normalizeDefaultEmail($object$data);
  39.         }
  40.         if (true === $this->shouldSerializeAttribute($dataContactInterface::ATTR_DEFAULT_PHONE$context)) {
  41.             $this->normalizeDefaultPhone($object$data);
  42.         }
  43.         if (true === $this->shouldSerializeAttribute($dataContactInterface::ATTR__LINKS$context)) {
  44.             $this->normalize_Links($object$data);
  45.         }
  46.         if (true === isset($context['context']) && true === \in_array('company_for_sale_collection'$context['context'])) {
  47.             $data['logo'] = null;
  48.             $data['address'] = $data['contactAddress'];
  49.             $data['price'] = $data['salePrice'];
  50.             $data['description'] = $data['saleDescription'];
  51.             $data['currency'] = 'NOK';
  52.             unset($data['contactAddress']);
  53.             unset($data['salePrice']);
  54.             unset($data['saleDescription']);
  55.         }
  56.         if (true === isset($context['context']) && true === \in_array('company_for_sale.get'$context['context'])) {
  57.             $data['logo'] = null;
  58.             $data['images'] = [];
  59.             $data['address'] = $data['contactAddress'];
  60.             $data['price'] = $data['salePrice'];
  61.             $data['description'] = $data['saleDescription'];
  62.             $data['currency'] = 'NOK';
  63.             unset($data['contactAddress']);
  64.             unset($data['salePrice']);
  65.             unset($data['saleDescription']);
  66.         }
  67.         return $data;
  68.     }
  69.     public function supportsNormalization($datastring $format null, array $context = []): bool
  70.     {
  71.         return $data instanceof ContactInterface;
  72.     }
  73.     public function hasCacheableSupportsMethod(): bool
  74.     {
  75.         return __CLASS__ === static::class;
  76.     }
  77.     private function normalizeDefaultEmail(ContactInterface $contact, array &$data): void
  78.     {
  79.         $contactEmail $contact->getContactEmail();
  80.         if (null === $contactEmail) {
  81.             return;
  82.         }
  83.         $data[ContactInterface::ATTR_DEFAULT_EMAIL] = $contactEmail->getEmail();
  84.     }
  85.     private function normalizeDefaultPhone(ContactInterface $contact, array &$data): void
  86.     {
  87.         $data[ContactInterface::ATTR_DEFAULT_PHONE] = $data[ContactInterface::ATTR_PHONE];
  88.     }
  89.     private function normalize_Links(ContactInterface $contact, array &$data): void
  90.     {
  91.         $data[ContactInterface::ATTR__LINKS] = [
  92.             'http' => [
  93.                 'view' => $this->urlGenerator->generate('app.contact.http.contact.view', ['contactId' => $contact->getId()]),
  94.             ],
  95.         ];
  96.     }
  97. }