/** * @param Currency $currency * @return float * @throws Exception If error retrieving remote data */ public function getRate(Currency $currency) { $rates = $this->getRates(); $code = $currency->getCurrencyCode(); if (empty($rates[$code])) { throw new InvalidArgumentException(sprintf('%s (%s) is not supported by this source', $currency->getDisplayName(), $code)); } return (double) $rates[$code]; }
public function __construct($currencyCode, $precision = null, $subUnit = null, $displayName = null, $numericCode = 0) { if (null === $precision || null === $subUnit) { parent::__construct($currencyCode); } else { parent::addCurrency($currencyCode, $displayName, $numericCode, $precision, $subUnit); parent::__construct($currencyCode); } }
/** * @param Currency $currency * @return static */ public function setCurrency(Currency $currency) { $this->currency = $currency->getCurrencyCode(); return $this; }
/** * @param $value * @param Currency $currency * @return Money * @throws \Exception */ protected function string2money($value, $currency) { $parts = explode('/', $value); if (!preg_match('/^-?[0-9]+$/', $parts[0])) { throw new \Exception('Illegal currency value: ' . $value . ' for currency ' . $currency); } if (isset($parts[1])) { $div = (int) $parts[1]; if ($div === $currency->getSubUnit()) { $moneyValue = (int) $parts[0]; } elseif ($div < $currency->getSubUnit()) { $moneyValue = (int) $parts[0] * $currency->getSubUnit() / $div; if (!is_int($moneyValue)) { throw new \Exception('Illegal currency value: ' . $value . ' for currency ' . $currency . '. Value conversion resulted in a float value'); } } else { throw new \Exception('Unknown currency value: ' . $value . ' for currency ' . $currency); } } else { throw new \Exception('Unknown currency value: ' . $value . ' for currency ' . $currency); } return new Money($moneyValue, $currency); }
/** * return the monetary value represented by this object converted to its base units * * @return float */ public function getConvertedAmount() { return round($this->amount / $this->currency->getSubUnit(), $this->currency->getDefaultFractionDigits()); }
/** * {@inheritdoc} */ public function __toString() { return parent::__toString(); }
/** * Converts an int to a string for the database reporting etc * @param int $value * @param \SebastianBergmann\Money\Currency $currency * @return string */ function convertIntToString($value, Currency $currency) { $subunit = $currency->getSubUnit(); return (string) round($value / $subunit, log10($subunit), PHP_ROUND_HALF_UP); }
/** * @param string $currencyCode * @param float|string $value * @param bool $default */ public function __construct($currencyCode, $value, $default = false) { $this->value = (double) $value; $this->default = $default; parent::__construct($currencyCode); }
/** * @inheritdoc * @param Currency $object */ public function normalize($object, $format = null, array $context = []) { return $object->getCurrencyCode(); }