예제 #1
0
 /**
  * Extract a money object into an array converting the integer amount to a float
  * @param  Money                              $object
  * @return array
  * @throws Exception\InvalidArgumentException
  */
 public function extract($object)
 {
     if (!$object instanceof Money) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects a Money instance', __METHOD__));
     }
     $subUnit = $object->getCurrency()->getSubUnit();
     $amount = (double) ($object->getAmount() / $subUnit);
     return array('amount' => $amount, 'currency' => $object->getCurrencyCode());
 }
예제 #2
0
 /**
  * Format a number
  *
  * @param  Money  $money
  * @param  string $locale
  * @param  bool   $showDecimals
  * @param  string $pattern
  * @return string
  */
 protected function formatCurrency(Money $money, $locale, $showDecimals, $pattern)
 {
     $formatter = $this->getFormatter($locale);
     // Should we look up the default pattern and restore it after we're done formatting?
     if ($pattern !== null) {
         $formatter->setPattern($pattern);
     }
     $currency = $money->getCurrency();
     $amount = $money->getAmount() / $currency->getSubUnit();
     $code = $currency->getCurrencyCode();
     $formatter->setTextAttribute(NumberFormatter::CURRENCY_CODE, $code);
     if ($showDecimals) {
         $digits = $money->getCurrency()->getDefaultFractionDigits();
         $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $digits);
     } else {
         $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
     }
     return $formatter->formatCurrency($amount, $code);
 }
예제 #3
0
 /**
  * @param  \NetglueMoney\Money\Money                         $a
  * @param  \NetglueMoney\Money\Money                         $b
  * @throws \NetglueMoney\Exception\CurrencyMismatchException
  */
 private function assertSameCurrency(Money $a, Money $b)
 {
     if ($a->getCurrency() != $b->getCurrency()) {
         throw new Exception\CurrencyMismatchException();
     }
 }
예제 #4
0
 /**
  * @covers            \NetglueMoney\Money\Money::compareTo
  * @covers            \NetglueMoney\Money\Money::assertSameCurrency
  * @uses              \NetglueMoney\Money\Money::__construct
  * @uses              \NetglueMoney\Money\Money::handleCurrencyArgument
  * @uses              \NetglueMoney\Money\Money::getCurrency
  * @uses              \NetglueMoney\Money\Currency
  * @expectedException \NetglueMoney\Exception\CurrencyMismatchException
  */
 public function testExceptionIsRaisedWhenComparedToMoneyObjectWithDifferentCurrency()
 {
     $a = new Money(1, new Currency('EUR'));
     $b = new Money(2, new Currency('USD'));
     $a->compareTo($b);
 }
예제 #5
0
 /**
  * Formats a Money object using PHP's built-in NumberFormatter.
  *
  * @param  \NetglueMoney\Money\Money $money
  * @return string
  */
 public function format(Money $money)
 {
     return $this->numberFormatter->formatCurrency($money->getAmount() / $money->getCurrency()->getSubUnit(), $money->getCurrency()->getCurrencyCode());
 }