diff --git a/src/Entity/GatewayCheckout.php b/src/Entity/GatewayCheckout.php index 8ca05d4..b861cd0 100644 --- a/src/Entity/GatewayCheckout.php +++ b/src/Entity/GatewayCheckout.php @@ -9,6 +9,7 @@ use App\Entity\Trait\TimestampableUpdationEntity; use App\Repository\GatewayCheckoutRepository; use App\State\GatewayCheckoutProcessor; use App\Validator\GatewayName; +use App\Validator\SupportedChargeTypes; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -63,6 +64,7 @@ class GatewayCheckout #[API\ApiProperty(readableLink: true, writableLink: true)] #[Assert\NotBlank()] #[Assert\Count(min: 1)] + #[SupportedChargeTypes()] #[ORM\OneToMany(mappedBy: 'checkout', targetEntity: GatewayCharge::class, cascade: ['persist'])] private Collection $charges; diff --git a/src/Validator/SupportedChargeTypes.php b/src/Validator/SupportedChargeTypes.php new file mode 100644 index 0000000..b803659 --- /dev/null +++ b/src/Validator/SupportedChargeTypes.php @@ -0,0 +1,14 @@ + */ + $charges = $value; + + $checkout = $charges->toArray()[0]->getCheckout(); + $gateway = $this->gatewayLocator->getGatewayOf($checkout); + + foreach ($charges as $charge) { + if (!\in_array( + $charge->getType(), + $gateway->getSupportedChargeTypes() + )) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $charge->getType()->value) + ->setParameter('{{ gateway }}', $gateway->getName()) + ->addViolation(); + } + } + } +}