src/Entity/Crm/Embed/Money.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Crm\Embed;
  3. use App\Enum\CurrencyEnum;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  8. #[ORM\Embeddable]
  9. class Money
  10. {
  11.     #[ORM\Column(typeTypes::INTEGERnullabletrue)]
  12.     private ?int $amount null// in centimes
  13.     #[ORM\Column(typeTypes::STRINGlength8nullabletrue)]
  14.     #[Assert\Choice(callback: [CurrencyEnum::class, 'getChoices'], multiplefalse)]
  15.     #[Assert\Length(max8)]
  16.     private ?string $currency null;
  17.     public function __construct(?string $currency null)
  18.     {
  19.         $this->currency $currency;
  20.     }
  21.     #[Assert\Callback]
  22.     public function validate(ExecutionContextInterface $context): void
  23.     {
  24.         if ($this->isEmpty() || $this->isComplete()) {
  25.             return;
  26.         }
  27.         $context
  28.             ->buildViolation('money.validate.empty_or_complete')
  29.             ->atPath('amount')
  30.             ->addViolation()
  31.         ;
  32.     }
  33.     public function isEmpty(): bool
  34.     {
  35.         return null === $this->amount;
  36.     }
  37.     public function isComplete(): bool
  38.     {
  39.         return null !== $this->amount
  40.             && null !== $this->currency
  41.         ;
  42.     }
  43.     public function getAmount(): ?int
  44.     {
  45.         return $this->amount;
  46.     }
  47.     public function setAmount(?int $amount): static
  48.     {
  49.         $this->amount $amount;
  50.         return $this;
  51.     }
  52.     public function getCurrency(): ?string
  53.     {
  54.         return $this->currency;
  55.     }
  56.     public function setCurrency(?string $currency): static
  57.     {
  58.         $this->currency $currency;
  59.         return $this;
  60.     }
  61. }