src/Offer/Serializer/Normalizer/OfferDraftNormalizer.php line 64

  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\Offer\Serializer\Normalizer;
  18. use App\Core\Serializer\Normalizer\AttributeAwareNormalizerTrait;
  19. use App\Core\Util\DecimalNumberEncoderInterface;
  20. use App\Offer\Model\OfferDraftInterface;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  23. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  24. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  25. final class OfferDraftNormalizer implements ContextAwareNormalizerInterfaceCacheableSupportsMethodInterface
  26. {
  27.     use AttributeAwareNormalizerTrait;
  28.     private NormalizerInterface $normalizer;
  29.     private UrlGeneratorInterface $urlGenerator;
  30.     private DecimalNumberEncoderInterface $encoder;
  31.     public function __construct(
  32.         NormalizerInterface $normalizer,
  33.         UrlGeneratorInterface $urlGenerator,
  34.         DecimalNumberEncoderInterface $encoder
  35.     ) {
  36.         $this->normalizer $normalizer;
  37.         $this->urlGenerator $urlGenerator;
  38.         $this->encoder $encoder;
  39.     }
  40.     public function normalize($objectstring $format null, array $context = []): float|int|bool|\ArrayObject|array|string|null
  41.     {
  42.         $data $this->normalizer->normalize($object$format$context);
  43.         if (true === $this->shouldSerializeAttribute($dataOfferDraftInterface::ATTR_GROSS_TOTAL$context)) {
  44.             $this->normalizeGrossTotal($object$data);
  45.         }
  46.         if (true === $this->shouldSerializeAttribute($dataOfferDraftInterface::ATTR__LINKS$context)) {
  47.             $this->normalize_Links($object$data);
  48.         }
  49.         return $data;
  50.     }
  51.     public function supportsNormalization($datastring $format null, array $context = []): bool
  52.     {
  53.         return $data instanceof OfferDraftInterface;
  54.     }
  55.     public function hasCacheableSupportsMethod(): bool
  56.     {
  57.         return __CLASS__ === static::class;
  58.     }
  59.     private function normalizeGrossTotal(OfferDraftInterface $offerDraft, array &$data): void
  60.     {
  61.         $data[OfferDraftInterface::ATTR_GROSS_TOTAL] = $this->encoder->decode($offerDraft->getItemsGrossTotal());
  62.     }
  63.     private function normalize_Links(OfferDraftInterface $offerDraft, array &$data): void
  64.     {
  65.         $data[OfferDraftInterface::ATTR__LINKS] = [
  66.             'ajax' => [
  67.                 'remove' => $this->urlGenerator->generate('app_offer_ajax_offer_draft_remove', [
  68.                     'id' => $offerDraft->getId(),
  69.                 ]),
  70.             ],
  71.             'http' => [
  72.                 'edit' => $this->urlGenerator->generate('app_offer_http_offer_draft_edit', [
  73.                     'id' => $offerDraft->getId(),
  74.                 ]),
  75.                 'export' => $this->urlGenerator->generate('app_offer_http_offer_draft_export', [
  76.                     'id' => $offerDraft->getId(),
  77.                 ]),
  78.             ],
  79.         ];
  80.     }
  81. }