Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function parse($money, $forceCurrency = null)
 {
     if (!is_string($money)) {
         throw new ParserException('Formatted raw money should be string, e.g. 1.00');
     }
     if (null === $forceCurrency) {
         throw new ParserException('DecimalMoneyParser cannot parse currency symbols. Use forceCurrency argument');
     }
     $currency = new Currency($forceCurrency);
     $decimal = trim($money);
     $subunit = $this->currencies->subunitFor($currency);
     if (!preg_match(self::DECIMAL_PATTERN, $decimal, $matches)) {
         throw new ParserException(sprintf('Cannot parse "%s" to Money.', $decimal));
     }
     $negative = isset($matches['sign']) && $matches['sign'] === '-';
     $decimal = $matches['digits'];
     if ($negative) {
         $decimal = '-' . $decimal;
     }
     if (isset($matches['fraction'])) {
         $fractionDigits = strlen($matches['fraction']);
         $decimal .= $matches['fraction'];
         $decimal = Number::roundMoneyValue($decimal, $subunit, $fractionDigits);
         if ($fractionDigits > $subunit) {
             $decimal = substr($decimal, 0, $subunit - $fractionDigits);
         } elseif ($fractionDigits < $subunit) {
             $decimal .= str_pad('', $subunit - $fractionDigits, '0');
         }
     } else {
         $decimal .= str_pad('', $subunit, '0');
     }
     if ($negative) {
         $decimal = '-' . ltrim(substr($decimal, 1), '0');
     } else {
         $decimal = ltrim($decimal, '0');
     }
     if ($decimal === '' || $decimal === '-') {
         $decimal = '0';
     }
     return new Money($decimal, $currency);
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     if (BitcoinCurrencies::CODE !== $money->getCurrency()->getCode()) {
         throw new FormatterException('Bitcoin Formatter can only format Bitcoin currency');
     }
     $valueBase = $money->getAmount();
     $negative = false;
     if ('-' === $valueBase[0]) {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $subunit = $this->currencies->subunitFor($money->getCurrency());
     $valueBase = Number::roundMoneyValue($valueBase, $this->fractionDigits, $subunit);
     $valueLength = strlen($valueBase);
     if ($valueLength > $subunit) {
         $formatted = substr($valueBase, 0, $valueLength - $subunit);
         if ($subunit) {
             $formatted .= '.';
             $formatted .= substr($valueBase, $valueLength - $subunit);
         }
     } else {
         $formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
     }
     if ($this->fractionDigits === 0) {
         $formatted = substr($formatted, 0, strpos($formatted, '.'));
     } elseif ($this->fractionDigits > $subunit) {
         $formatted .= str_pad('', $this->fractionDigits - $subunit, '0');
     } elseif ($this->fractionDigits < $subunit) {
         $lastDigit = strpos($formatted, '.') + $this->fractionDigits + 1;
         $formatted = substr($formatted, 0, $lastDigit);
     }
     $formatted = BitcoinCurrencies::SYMBOL . $formatted;
     if (true === $negative) {
         $formatted = '-' . BitcoinCurrencies::SYMBOL . $formatted;
     }
     return $formatted;
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function parse($money, $forceCurrency = null)
 {
     if (!is_string($money)) {
         throw new ParserException('Formatted raw money should be string, e.g. $1.00');
     }
     $currencyCode = null;
     $decimal = $this->formatter->parseCurrency($money, $currencyCode);
     if (null !== $forceCurrency) {
         $currencyCode = $forceCurrency;
     }
     $currency = new Currency($currencyCode);
     if (false === $decimal) {
         throw new ParserException('Cannot parse ' . $money . ' to Money. ' . $this->formatter->getErrorMessage());
     }
     $decimal = (string) $decimal;
     $subunit = $this->currencies->subunitFor($currency);
     $decimalPosition = strpos($decimal, '.');
     if (false !== $decimalPosition) {
         $decimalLength = strlen($decimal);
         $fractionDigits = $decimalLength - $decimalPosition - 1;
         $decimal = str_replace('.', '', $decimal);
         $decimal = Number::roundMoneyValue($decimal, $subunit, $fractionDigits);
         if ($fractionDigits > $subunit) {
             $decimal = substr($decimal, 0, $decimalPosition + $subunit);
         } elseif ($fractionDigits < $subunit) {
             $decimal .= str_pad('', $subunit - $fractionDigits, '0');
         }
     } else {
         $decimal .= str_pad('', $subunit, '0');
     }
     if ('-' === $decimal[0]) {
         $decimal = '-' . ltrim(substr($decimal, 1), '0');
     } else {
         $decimal = ltrim($decimal, '0');
     }
     return new Money($decimal, $currency);
 }