/** * Ensure the given code is a known valid code * * @param string|Currency|Money $code * @return string Normalized code * @throws Exception\InvalidArgumentException * @throws Exception\UnknownCurrencyException */ protected function assertValidCode($code) { if ($code instanceof Money) { $code = $code->getCurrency(); } if ($code instanceof Currency) { return (string) $code; } if (!is_string($code)) { throw new Exception\InvalidArgumentException(sprintf('Currency code should be a string; %s given', gettype($code))); } $code = trim(strtoupper($code)); if (!Currencies::exists($code)) { throw new Exception\UnknownCurrencyException(sprintf('%s is not a valid ISO 4217 Currency code', $code)); } return $code; }
/** * __construct * * @param string $code * @param string $locale * * @throws Exception\UnexpectedValueException * @throws Exception\InvalidArgumentException */ public function __construct($code = null, $locale = null) { if (null === $code) { $formatter = new NumberFormatter((string) $locale ?: Locale::getDefault(), NumberFormatter::CURRENCY); $code = $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL); } if (!is_string($code)) { throw new Exception\UnexpectedValueException('Currency code should be a string'); } if (!Currencies::exists($code)) { throw new Exception\InvalidArgumentException('Currency with "' . $code . '" code does not exist!'); } $this->id = strtolower($code); $this->fromArray(Currencies::getSpecification($this->id)); }