public function convert(Money $money, $currency)
 {
     if (!$currency instanceof Currency) {
         $currency = new Currency($currency);
     }
     if ($money->getCurrency()->equals($currency)) {
         return $money;
     }
     $rates = $this->getRates($money->getCurrency());
     if (!isset($rates[$currency->getName()])) {
         throw new RuntimeException(sprintf('Could not get rate for currency %s', $currency->getName()));
     }
     $pair = new CurrencyPair($money->getCurrency(), $currency, $rates[$currency->getName()]);
     return $pair->convert($money);
 }
 public function getAmountInBaseUnits(Money $money)
 {
     $iso = $this->iso4217->getByAlpha3($money->getCurrency()->getName());
     $decimals = $iso['exp'];
     $dividend = pow(10, $decimals);
     return $money->getAmount() / $dividend;
 }
示例#3
0
 /**
  * @param \Money\Money $money
  * @param        $rounding_mode
  * @return \Money\Money
  * @expectedException InvalidArgumentException
  */
 public function convert(Money $money, RoundingMode $rounding_mode = null)
 {
     if (!$money->getCurrency()->equals($this->baseCurrency)) {
         throw new InvalidArgumentException("The Money has the wrong currency");
     }
     $rounding_mode = $rounding_mode ?: RoundingMode::halfUp();
     return new Money((int) round($money->getAmount() * $this->ratio, 0, $rounding_mode->getRoundingMode()), $this->counterCurrency);
 }
示例#4
0
 public function convert(Money $money, $target)
 {
     if (!$target instanceof MoneyCurrency) {
         throw new InvalidArgumentException(sprintf('Second argument must be Currency, %s given.', gettype($target)));
     }
     $rate = $this->swap->quote(new CurrencyPair($money->getCurrency(), $target));
     return new Money($money->multiply((double) $rate->getValue())->getAmount(), $target);
 }
 public function testHydratorHydratesAsExpected()
 {
     $hydrator = new MoneyHydrator();
     $data = ['amount' => 500, 'currency' => 'BRL'];
     $money = new Money(500, new Currency('BRL'));
     $object = $hydrator->hydrate($data, new \stdClass());
     $this->assertEquals($money->getAmount(), $object->getAmount());
     $this->assertEquals($money->getCurrency(), $object->getCurrency());
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     $currencyCode = $money->getCurrency()->getCode();
     if (isset($this->formatters[$currencyCode])) {
         return $this->formatters[$currencyCode]->format($money);
     }
     if (isset($this->formatters['*'])) {
         return $this->formatters['*']->format($money);
     }
     throw new FormatterException('No formatter found for currency ' . $currencyCode);
 }
示例#7
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     $valueBase = (string) $money->getAmount();
     $negative = false;
     if ($valueBase[0] === '-') {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $subunit = $this->currencies->subunitFor($money->getCurrency());
     $valueLength = strlen($valueBase);
     if ($valueLength > $subunit) {
         $formatted = substr($valueBase, 0, $valueLength - $subunit) . '.';
         $formatted .= substr($valueBase, $valueLength - $subunit);
     } else {
         $formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
     }
     if ($negative === true) {
         $formatted = '-' . $formatted;
     }
     return $this->formatter->formatCurrency($formatted, $money->getCurrency()->getCode());
 }
示例#8
0
 function it_converts_money_using_swap(SwapInterface $swap, Money $money, Money $converted, Currency $from, Currency $to)
 {
     $money->getAmount()->willReturn(100);
     $money->getCurrency()->willReturn($from);
     $money->multiply(120.3971)->willReturn($converted);
     $converted->getAmount()->willReturn(12039);
     $converted->getCurrency()->willReturn($to);
     $from->__toString()->willReturn('EUR');
     $to->__toString()->willReturn('RSD');
     $rate = new Rate(120.3971);
     $swap->quote('EUR/RSD')->shouldBeCalled()->willReturn($rate);
     $this->convert($money, $to)->getAmount()->shouldBe(12039);
 }
示例#9
0
 /**
  * Create a new Product
  *
  * @param string $sku
  * @param string $name
  * @param Money $price
  * @param TaxRate $rate
  * @return void
  */
 public function __construct($sku, $name, Money $price, TaxRate $rate)
 {
     $this->sku = $sku;
     $this->name = $name;
     $this->price = $price;
     $this->rate = $rate;
     $this->quantity = 1;
     $this->freebie = false;
     $this->taxable = true;
     $this->delivery = new Money(0, $price->getCurrency());
     $this->coupons = new Collection();
     $this->tags = new Collection();
 }
示例#10
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;
 }
示例#11
0
 /**
  * @covers Xoops\Core\Kernel\Dtype\DtypeMoney::getVar
  * @covers Xoops\Core\Kernel\Dtype\DtypeMoney::cleanVar
  */
 public function testGetVarCleanVar()
 {
     $testValue = new Money(10000, new Currency('USD'));
     $key = 'money_test';
     $this->xObject[$key] = $testValue;
     $this->xObject[$key] = $this->object->cleanVar($this->xObject, $key);
     $value = $this->xObject->getVar($key, Dtype::FORMAT_NONE);
     $this->assertInstanceOf('\\Money\\Money', $value);
     $this->assertEquals($testValue->getAmount(), $value->getAmount());
     $this->assertEquals($testValue->getCurrency(), $value->getCurrency());
     $this->assertNotSame($value, $testValue);
     $value2 = $this->xObject->getVar($key, Dtype::FORMAT_SHOW);
     $this->assertInstanceOf('\\Money\\Money', $value2);
     $this->assertEquals($testValue->getAmount(), $value2->getAmount());
     $this->assertEquals($testValue->getCurrency(), $value2->getCurrency());
     $this->assertNotSame($value, $value2);
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     $valueBase = (string) $money->getAmount();
     $negative = false;
     if (substr($valueBase, 0, 1) === '-') {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $fractionDigits = $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS);
     $valueLength = strlen($valueBase);
     if ($valueLength > $fractionDigits) {
         $subunits = substr($valueBase, 0, $valueLength - $fractionDigits) . '.';
         $subunits .= substr($valueBase, $valueLength - $fractionDigits);
     } else {
         $subunits = '0.' . str_pad('', $fractionDigits - $valueLength, '0') . $valueBase;
     }
     if ($negative === true) {
         $subunits = '-' . $subunits;
     }
     return $this->formatter->formatCurrency($subunits, $money->getCurrency()->getCode());
 }
示例#13
0
 public function testGetters()
 {
     $m = new Money(100, $euro = new Currency('EUR'));
     $this->assertEquals(100, $m->getAmount());
     $this->assertEquals($euro, $m->getCurrency());
 }
示例#14
0
 /**
  * @param \JMS\Serializer\VisitorInterface $visitor
  * @param Money $money
  * @param mixed[] $type
  * @param \JMS\Serializer\Context $context
  * @return string
  */
 public function serializeMoney(VisitorInterface $visitor, Money $money, array $type, Context $context)
 {
     return (string) $money->getAmount() . ' ' . $money->getCurrency()->getName();
 }
示例#15
0
 /**
  * @param Money $money
  * @return string
  */
 public function formatMoney(Money $money)
 {
     return "\${$this->formatMoneyAmount(round($money->getAmount() / 100, 2))} {$money->getCurrency()}";
 }
示例#16
0
 /**
  * @param Money $cost
  *
  * @return $this
  */
 public function willCosts(Money $cost)
 {
     $this->amount = $cost->getAmount();
     $this->currency = $cost->getCurrency()->getName();
     return $this;
 }
 public function convert(Money $from, Currency $to, DateTime $date = null)
 {
     $pair = $this->getRate($from->getCurrency(), $to);
     return $pair->convert($from);
 }
 public function testFormatCurrencyAsName()
 {
     $value = $this->formatter->formatCurrencyAsName($this->inputMoney->getCurrency());
     $this->assertEquals('EUR', $value);
 }
示例#19
0
文件: Money.php 项目: Codixis/CSBill
 /**
  * Set price.
  *
  * @param MoneyObject $price
  *
  * @return $this
  */
 public function setAmount(MoneyObject $price)
 {
     $this->priceAmount = $price->getAmount();
     $this->priceCurrency = $price->getCurrency()->getName();
     return $this;
 }
示例#20
0
 /**
  * Returns the Currency object
  *
  * @param Money $money
  * @return \Money\Currency
  */
 public function getCurrency(Money $money)
 {
     return $money->getCurrency();
 }
 /**
  * @param Money  $money
  * @param bool   $showDecimals
  * @param string $locale
  * @param string $pattern
  *
  * @return string
  */
 public function __invoke(Money $money, $showDecimals = null, $locale = null, $pattern = null)
 {
     $currencyFormat = $this->getView()->plugin('currencyFormat');
     return $currencyFormat($money->getAmount() / 100, $money->getCurrency(), $showDecimals, $locale, $pattern);
 }
示例#22
0
 /**
  * @param Money $value
  *
  * @return mixed
  */
 private function code(Money $value)
 {
     return $value->getCurrency()->getName();
 }
示例#23
0
 /**
  * @inheritdoc
  */
 public function convert(Money $amount, $currencyCode)
 {
     $ratio = $this->getRelativeRatio($amount->getCurrency()->getName(), $currencyCode);
     $pair = new CurrencyPair($amount->getCurrency(), new Currency($currencyCode), $ratio);
     return $pair->convert($amount);
 }