src/Normalizer/CurrencyNormalizer.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Normalizer;
  3. use App\Domain\MarginCheckList\Adapters\Controller\ApiPlatform\Resource\AbstractFinancialMargin;
  4. use App\Entity\AbstractCollaborator;
  5. use App\Entity\Agent;
  6. use App\Entity\BusinessIndication;
  7. use App\Entity\Mandate;
  8. use App\Entity\Property;
  9. use App\Entity\PropertyVisitVoucher;
  10. use App\Entity\Transaction;
  11. use App\Services\CurrencyService;
  12. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  13. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  14. use Symfony\Component\Serializer\SerializerAwareInterface;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. /**
  17.  * CurrencyNormalizer
  18.  */
  19. class CurrencyNormalizer implements NormalizerInterfaceDenormalizerInterfaceSerializerAwareInterface
  20. {
  21.     private const RESOURCE_WITH_CURRENCY = [
  22.         Agent::class,
  23.         AbstractCollaborator::class,
  24.         Property::class,
  25.         Mandate::class,
  26.         Transaction::class,
  27.         AbstractFinancialMargin::class,
  28.         BusinessIndication::class,
  29.         PropertyVisitVoucher::class
  30.     ];
  31.     private CurrencyService $currencyService;
  32.     private $decorated;
  33.     public function __construct(NormalizerInterface $decoratedCurrencyService $currencyService)
  34.     {
  35.         if (!$decorated instanceof DenormalizerInterface) {
  36.             throw new \InvalidArgumentException(
  37.                 sprintf(
  38.                     'The decorated normalizer must implement the %s.',
  39.                     DenormalizerInterface::class
  40.                 )
  41.             );
  42.         }
  43.         $this->decorated $decorated;
  44.         $this->currencyService $currencyService;
  45.     }
  46.     public function supportsNormalization($data$format null): bool
  47.     {
  48.         return $this->decorated->supportsNormalization($data$format);
  49.     }
  50.     public function normalize($object$format null, array $context = [])
  51.     {
  52.         $data $this->decorated->normalize($object$format$context);
  53.         if (is_array($data) && in_array(get_class($object), self::RESOURCE_WITH_CURRENCYtrue)) {
  54.             $data['currency'] = $this->currencyService->getCurrency();
  55.         }
  56.         return $data;
  57.     }
  58.     public function supportsDenormalization($data$type$format null): bool
  59.     {
  60.         return $this->decorated->supportsDenormalization($data$type$format);
  61.     }
  62.     public function denormalize($data$class$format null, array $context = []): object
  63.     {
  64.         return $this->decorated->denormalize($data$class$format$context);
  65.     }
  66.     public function setSerializer(SerializerInterface $serializer): void
  67.     {
  68.         if ($this->decorated instanceof SerializerAwareInterface) {
  69.             $this->decorated->setSerializer($serializer);
  70.         }
  71.     }
  72. }