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); }
/** * @param Quote|Invoice $object * @param Money $total * * @return mixed */ private function setDiscount($object, Money $total) { if (null !== $object->getDiscount()) { $discount = $total->multiply($object->getDiscount()); $total = $total->subtract($discount); return $total; } return $total; }
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); }
public function testMultiplication() { $m = new Money(1, new Currency('EUR')); $this->assertEquals(new Money(2, new Currency('EUR')), $m->multiply(1.5)); $this->assertEquals(new Money(1, new Currency('EUR')), $m->multiply(1.5, Money::ROUND_HALF_DOWN)); $this->assertNotSame($m, $m->multiply(2)); }
/** * PrePersist listener to update the line total. * * @ORM\PrePersist */ public function updateTotal() { $this->total = $this->price->multiply($this->qty); }
/** * @return Money */ public function negative() : Money { return new self($this->money->multiply(-1)); }