コード例 #1
0
ファイル: CurrencyListFactory.php プロジェクト: coolms/money
 /**
  * {@inheritDoc}
  *
  * @return CurrencyListInterface
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     /* @var $options ModuleOptionsInterface */
     $options = $serviceLocator->get(ModuleOptions::class);
     if ($specs = $options->getCurrencySpecs()) {
         foreach ($specs as $spec) {
             Currencies::register($spec);
         }
     }
     return new CurrencyList($options->getAllowed(), $options->getExcluded(), $options->getDefault());
 }
コード例 #2
0
ファイル: CurrencyList.php プロジェクト: coolms/money
 /**
  * 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;
 }
コード例 #3
0
ファイル: Currency.php プロジェクト: coolms/money
 /**
  * __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));
 }