/** * 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()); }
/** * 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); }
/** * @covers \NetglueMoney\Money\Money::multiply * @covers \NetglueMoney\Money\Money::newMoney * @covers \NetglueMoney\Money\Money::castToInt * @uses \NetglueMoney\Money\Money::__construct * @uses \NetglueMoney\Money\Money::handleCurrencyArgument * @uses \NetglueMoney\Money\Money::getAmount * @uses \NetglueMoney\Money\Money::assertInsideIntegerBounds * @uses \NetglueMoney\Money\Currency */ public function testCanBeMultipliedByAFactor() { $a = new Money(1, new Currency('EUR')); $b = $a->multiply(2); $this->assertEquals(1, $a->getAmount()); $this->assertEquals(2, $b->getAmount()); }
/** * 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()); }