<?php
namespace App\Normalizer;
use App\Domain\MarginCheckList\Adapters\Controller\ApiPlatform\Resource\AbstractFinancialMargin;
use App\Entity\AbstractCollaborator;
use App\Entity\Agent;
use App\Entity\BusinessIndication;
use App\Entity\Mandate;
use App\Entity\Property;
use App\Entity\PropertyVisitVoucher;
use App\Entity\Transaction;
use App\Services\CurrencyService;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* CurrencyNormalizer
*/
class CurrencyNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
private const RESOURCE_WITH_CURRENCY = [
Agent::class,
AbstractCollaborator::class,
Property::class,
Mandate::class,
Transaction::class,
AbstractFinancialMargin::class,
BusinessIndication::class,
PropertyVisitVoucher::class
];
private CurrencyService $currencyService;
private $decorated;
public function __construct(NormalizerInterface $decorated, CurrencyService $currencyService)
{
if (!$decorated instanceof DenormalizerInterface) {
throw new \InvalidArgumentException(
sprintf(
'The decorated normalizer must implement the %s.',
DenormalizerInterface::class
)
);
}
$this->decorated = $decorated;
$this->currencyService = $currencyService;
}
public function supportsNormalization($data, $format = null): bool
{
return $this->decorated->supportsNormalization($data, $format);
}
public function normalize($object, $format = null, array $context = [])
{
$data = $this->decorated->normalize($object, $format, $context);
if (is_array($data) && in_array(get_class($object), self::RESOURCE_WITH_CURRENCY, true)) {
$data['currency'] = $this->currencyService->getCurrency();
}
return $data;
}
public function supportsDenormalization($data, $type, $format = null): bool
{
return $this->decorated->supportsDenormalization($data, $type, $format);
}
public function denormalize($data, $class, $format = null, array $context = []): object
{
return $this->decorated->denormalize($data, $class, $format, $context);
}
public function setSerializer(SerializerInterface $serializer): void
{
if ($this->decorated instanceof SerializerAwareInterface) {
$this->decorated->setSerializer($serializer);
}
}
}