本文整理汇总了PHP中Money类的典型用法代码示例。如果您正苦于以下问题:PHP Money类的具体用法?PHP Money怎么用?PHP Money使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Money类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addMoney
public function addMoney(Money $m)
{
if ($this->currency() == $m->currency()) {
return new Money($this->amount() + $m->amount(), $this->currency());
}
return MoneyBag::create($this, $m);
}
示例2: convert
/**
* Converts Money from base to counter currency.
*
* @param Money $money
* @param int $roundingMode
*
* @return Money
*
* @throws \InvalidArgumentException If $money's currency is not equal to base currency
*/
public function convert(Money $money, $roundingMode = Money::ROUND_HALF_UP)
{
if (!$money->getCurrency()->equals($this->baseCurrency)) {
throw new \InvalidArgumentException('The Money has the wrong currency');
}
return $money->convert($this->counterCurrency, $this->conversionRatio, $roundingMode);
}
示例3: add
/**
* @param money $amount
* @return money
*/
public function add(Money $amount)
{
if (!$this->currency->equals($amount->getCurrency())) {
throw new \InvalidArgumentException('Currency mismatch');
}
return new Money($this->currency, $this->amount + $amount->getValue());
}
示例4: testShouldFail
public function testShouldFail()
{
$a = new Money(9);
//$b = $a->negate();
//$this->assertEquals(-9, $b->getAmount());
$this->assertEquals(-8, $a->getAmount());
}
示例5: price_for_display
public static function price_for_display($price)
{
$currency = ShopConfig::get_site_currency();
$field = new Money("Price");
$field->setAmount($price);
$field->setCurrency($currency);
return $field;
}
示例6: convert
/** @return Money */
public function convert(Money $money)
{
if (!$money->getCurrency()->equals($this->counterCurrency)) {
throw new InvalidArgumentException("The Money has the wrong currency");
}
// @todo add rounding mode?
return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency);
}
示例7: testCanBeNegated
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
示例8: testSubstractionResult
public function testSubstractionResult()
{
// Arrange
$a = new Money();
// Act
$b = $a->substraction(2, 1);
// Assert
$this->assertEquals(1, $b);
}
示例9: testAdd
public function testAdd()
{
// Arrange
$a = new Money(600);
// Act
$a->add(66);
// Assert
$this->assertEquals(666, $a->getAmount());
}
示例10: testStringAmount
/**
* @covers ::evaluate
*/
public function testStringAmount()
{
$money = new Money();
$money->setValue(['amount' => '50', 'currency' => 'XTS']);
$this->assertTrue($money->isValid());
$obj = $money->evaluate();
$this->assertInstanceOf('SebastianBergmann\\Money\\Money', $obj);
$this->assertSame(50, $obj->getAmount());
$this->assertEquals(new \SebastianBergmann\Money\Currency('XTS'), $obj->getCurrency());
}
示例11: credit
public function credit($methodId, $typeId, $marketId, $presenterId, $userId, Money $cost, $entryUser, $referenceId)
{
$result = $this->saveCreate(array("ProductCredit" => array("market_id" => $marketId, "user_id" => $userId, "presenter_id" => $presenterId, "product_credit_type_id" => $typeId, "product_credit_entry_type_id" => $methodId, "product_credit_status_type_id" => self::STATUS_SETTLED, "entry_user" => $entryUser, "amount" => (string) $cost->makePositive(), "reference_id" => $referenceId)));
if ($result) {
syslog(LOG_DEBUG, "Credit\tLedger {$this->id}\t{$marketId} {$presenterId}\t{$userId}\t{$cost}");
} else {
syslog(LOG_DEBUG, "CreditERROR\tLedger\t{$marketId} {$presenterId}\t{$userId}\t{$cost}");
}
return $result;
}
示例12: convert
/**
* @param Money $money
* @param Currency $counterCurrency
* @param int $roundingMode
*
* @return Money
*/
public function convert(Money $money, Currency $counterCurrency, $roundingMode = Money::ROUND_HALF_UP)
{
$baseCurrency = $money->getCurrency();
$ratio = $this->exchange->quote($baseCurrency, $counterCurrency)->getConversionRatio();
$baseCurrencySubunit = $this->currencies->subunitFor($baseCurrency);
$counterCurrencySubunit = $this->currencies->subunitFor($counterCurrency);
$subunitDifference = $baseCurrencySubunit - $counterCurrencySubunit;
$ratio = $ratio / pow(10, $subunitDifference);
$counterValue = $money->multiply($ratio, $roundingMode);
return new Money($counterValue->getAmount(), $counterCurrency);
}
示例13: testSetValueAsMoney
public function testSetValueAsMoney()
{
$o = new MoneyFieldTest_Object();
$f = new MoneyField('MyMoney', 'MyMoney');
$m = new Money();
$m->setAmount(123456.78);
$m->setCurrency('EUR');
$f->setValue($m);
$f->saveInto($o);
$this->assertEquals(123456.78, $o->MyMoney->getAmount());
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
}
示例14: testAddCompositedExtraFields
public function testAddCompositedExtraFields()
{
$obj = new ManyManyListTest_ExtraFields();
$obj->write();
$money = new Money();
$money->setAmount(100);
$money->setCurrency('USD');
// the actual test is that this does not generate an error in the sql.
$obj->Clients()->add($obj, array('Worth' => $money, 'Reference' => 'Foo'));
$check = $obj->Clients()->First();
$this->assertEquals('Foo', $check->Reference, 'Basic scalar fields should exist');
$this->assertInstanceOf('Money', $check->Worth, 'Composite fields should exist on the record');
$this->assertEquals(100, $check->Worth->getAmount());
}
示例15: display
/**
* @param Money|string|int|float $amount
* @param int $precision
* @return string
*/
public function display($amount, $precision = 2)
{
$amount = Utils::toStringAmount($amount);
$money = new Money($amount);
if ($this->isLeftSign) {
if ($money->isLessThan('0')) {
return '-' . $this->sign . $money->abs()->format($precision);
} else {
return $this->sign . $money->format($precision);
}
} else {
return $money->format($precision) . $this->sign;
}
}