Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faparicior/b358a41b031ea7d5ffa9aa1de6e8bfbe to your computer and use it in GitHub Desktop.
Save faparicior/b358a41b031ea7d5ffa9aa1de6e8bfbe to your computer and use it in GitHub Desktop.
workshop-value-object-or-not-quiz-2
final class Money
{
public function __construct(private string $amount, private string $currency)
{
if (!is_numeric($amount)) {
throw new InvalidAmountException::notNumeric($amount);
}
if (!in_array(strtoupper($currency), ['EUR', 'USD'])) {
throw new InvalidCurrencyException::notInTheAcceptedCurrencies($currency);
}
$this->amount = $amount;
$this->currency = $currency;
}
public function amount(): string
{
return $this->amount;
}
public function currency(): string
{
return $this->currency;
}
public function equals(Money $other): bool
{
return $this->amount === $other->amount() &&
$this->currency === $other->currency();
}
public function add(string $amount): void
{
$this->amount = bcadd($this->amount, $amount, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment