amount = $amount; $this->code = $code; } /** * Get an amount. */ public function getAmount(): float { return $this->amount; } /** * Get a currency code. */ public function getCode(): string { return $this->code; } /** * Add a currency value. */ public function add(self $value): self { $amount = $this->getAmount(); if ($this->getCode() !== $value->getCode()) { throw new RuntimeException("Can't add a currency value with a different code."); } $amount += $value->getAmount(); return new self($amount, $this->getCode()); } /** * Subtract a currency value. */ public function subtract(self $value): self { $amount = $this->getAmount(); if ($this->getCode() !== $value->getCode()) { throw new RuntimeException("Can't substract a currency value with a different code."); } $amount -= $value->getAmount(); return new self($amount, $this->getCode()); } /** * Multiply by a multiplier. */ public function multiply(float $multiplier): self { $amount = $this->getAmount(); $amount *= $multiplier; return new self($amount, $this->getCode()); } /** * Divide by a divider. */ public function divide(float $divider): self { $amount = $this->getAmount(); $amount /= $divider; return new self($amount, $this->getCode()); } /** * Round with a precision. */ public function round(int $precision = 0): self { $amount = round($this->getAmount(), $precision); return new self($amount, $this->getCode()); } /** * Create from an amount and code. */ public static function create(float $amount, string $code): self { return new self($amount, $code); } }