setValue($value); } if (null !== $currency) { $this->setCurrency($currency); } } } /** * Возвращает значение суммы. * * @return string Сумма */ public function getValue(): string { $negative = ($this->_value < 0 ? '-' : ''); $mod = abs($this->_value); if ($mod < 10) { return $negative . '0.0' . $mod; } if ($mod < 100) { return $negative . '0.' . $mod; } return $negative . substr($mod, 0, -2) . '.' . substr($mod, -2); } /** * Устанавливает сумму. * * @param numeric|string $value Сумма * * @return self */ public function setValue(mixed $value): self { if (is_numeric($value)) { $value = (int) round($value * 100.0); } $this->_value = $this->validatePropertyValue('_value', $value); return $this; } /** * Возвращает сумму в копейках в виде целого числа. * * @return int Сумма в копейках/центах */ public function getIntegerValue(): int { return $this->_value; } /** * Возвращает валюту. * * @return string Код валюты */ public function getCurrency(): string { return $this->_currency; } /** * Устанавливает код валюты. * * @param string|null $currency Код валюты * * @return self */ public function setCurrency(?string $currency): self { $this->_currency = $this->validatePropertyValue('_currency', $currency); return $this; } /** * @return array */ public function jsonSerialize(): array { return [ 'value' => number_format($this->_value / 100.0, 2, '.', ''), 'currency' => $this->_currency, ]; } }