<?php
namespace App\Entity\Crm\Embed;
use App\Enum\CurrencyEnum;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Embeddable]
class Money
{
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $amount = null; // in centimes
#[ORM\Column(type: Types::STRING, length: 8, nullable: true)]
#[Assert\Choice(callback: [CurrencyEnum::class, 'getChoices'], multiple: false)]
#[Assert\Length(max: 8)]
private ?string $currency = null;
public function __construct(?string $currency = null)
{
$this->currency = $currency;
}
#[Assert\Callback]
public function validate(ExecutionContextInterface $context): void
{
if ($this->isEmpty() || $this->isComplete()) {
return;
}
$context
->buildViolation('money.validate.empty_or_complete')
->atPath('amount')
->addViolation()
;
}
public function isEmpty(): bool
{
return null === $this->amount;
}
public function isComplete(): bool
{
return null !== $this->amount
&& null !== $this->currency
;
}
public function getAmount(): ?int
{
return $this->amount;
}
public function setAmount(?int $amount): static
{
$this->amount = $amount;
return $this;
}
public function getCurrency(): ?string
{
return $this->currency;
}
public function setCurrency(?string $currency): static
{
$this->currency = $currency;
return $this;
}
}