code = $code ?? $this->code ?? ''; $this->name = $name ?? $this->name ?? ''; $this->rate = $rate ?? $this->rate ?? 1; $this->prefix = $prefix ?? $this->prefix ?? ''; $this->suffix = $suffix ?? $this->suffix ?? ''; $this->mathDecimals = $mathDecimals ?? $this->mathDecimals ?? 2; $this->displayDecimals = $displayDecimals ?? $this->displayDecimals ?? 2; $this->decimalSeparator = $decimalSeparator ?? $this->decimalSeparator ?? '.'; $this->thousandsSeparator = $thousandsSeparator ?? $this->thousandsSeparator ?? ','; $this->rounding = $rounding ?? $this->rounding ?? $this->mathDecimals; $this->trimTrailingDecimalZeros = $trimTrailingDecimalZeros ?? $this->trimTrailingDecimalZeros ?? false; $this->check(); } /** Create an anonymous Currency instance from an array. */ public static function fromArray(array $currency): static { return new static(...$currency); } /** Get the currency's code. */ public function code(): string { return $this->code; } /** Get the currency's name. */ public function name(): string { return $this->name; } /** Get the currency's rate. */ public function rate(): float { return $this->rate; } /** Get the currency's prefix. */ public function prefix(): string { return $this->prefix; } /** Get the currency's suffix. */ public function suffix(): string { return $this->suffix; } /** Get the currency's math decimal count. */ public function mathDecimals(): int { return $this->mathDecimals; } /** Get the currency's math decimal count. */ public function displayDecimals(): int { return $this->displayDecimals; } /** Get the currency's decimal separator. */ public function decimalSeparator(): string { return $this->decimalSeparator; } /** Get the currency's thousands separator. */ public function thousandsSeparator(): string { return $this->thousandsSeparator; } /** Get the currency's rounding. */ public function rounding(): int { return $this->rounding; } /** Get the setting for how trailing decimal zeroes should be handled. */ public function trimTrailingDecimalZeros(): bool { return $this->trimTrailingDecimalZeros; } /** Convert the currency to a string (returns the code). */ public function __toString() { return $this->code(); } /** Convert the currency to an array. */ public function toArray(): array { return [ 'code' => $this->code, 'name' => $this->name, 'rate' => $this->rate, 'prefix' => $this->prefix, 'suffix' => $this->suffix, 'mathDecimals' => $this->mathDecimals, 'displayDecimals' => $this->displayDecimals, 'rounding' => $this->rounding, 'decimalSeparator' => $this->decimalSeparator, 'thousandsSeparator' => $this->thousandsSeparator, 'trimTrailingDecimalZeros' => $this->trimTrailingDecimalZeros, ]; } /** Get the data used for JSON serialization. */ public function jsonSerialize(): array { return $this->toArray(); } /** Create a currency from JSON. */ public static function fromJson(string|array $json): self { if (is_string($json)) { $json = json_decode($json, true, flags: JSON_THROW_ON_ERROR); } return static::fromArray($json); } /** * Ensure that the currency has all required values. * * @throws InvalidCurrencyException */ protected function check(): void { if (! $this->code() || ! $this->name()) { throw new InvalidCurrencyException('This currency does not have a code or a name.'); } } }