本文整理汇总了PHP中Money\Money::allocate方法的典型用法代码示例。如果您正苦于以下问题:PHP Money::allocate方法的具体用法?PHP Money::allocate怎么用?PHP Money::allocate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Money\Money
的用法示例。
在下文中一共展示了Money::allocate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getPlan
/**
* @param PlanDefinition $definition
* @param Money $amountToPay
* @param PlanParameters $parameters
* @return PaymentPlan
*/
public function getPlan(PlanDefinition $definition, Money $amountToPay, PlanParameters $parameters)
{
/** @var array $paymentProportions */
$paymentProportions = $definition->getAttribute('payments');
//The values of the payments array is a ratios array
/** @var Money[] $amounts */
$amounts = $amountToPay->allocate(array_values($paymentProportions));
$payments = array_map(function ($rawDate, $amount) {
//rawDate is either 'immediate' or a 'Y-m-d' string, or invalid.
if ($rawDate === 'immediate') {
return PlannedPayment::immediate($amount);
}
return PlannedPayment::withDueDate($amount, DueDate::fromString($rawDate));
}, array_keys($paymentProportions), $amounts);
return new PaymentPlan($payments, $definition->getAttribute('short_description'), $definition->getAttribute('long_description'));
}
示例2: testAllocationOrderIsImportant
public function testAllocationOrderIsImportant()
{
$m = new Money(5, new Currency('EUR'));
list($part1, $part2) = $m->allocate(array(3, 7));
$this->assertEquals(new Money(2, new Currency('EUR')), $part1);
$this->assertEquals(new Money(3, new Currency('EUR')), $part2);
$m = new Money(5, new Currency('EUR'));
list($part1, $part2) = $m->allocate(array(7, 3));
$this->assertEquals(new Money(4, new Currency('EUR')), $part1);
$this->assertEquals(new Money(1, new Currency('EUR')), $part2);
}
示例3: splitMoneyEvenly
/**
* @param Money $total
* @param integer $numberOfParts
* @return Money[]
*/
private function splitMoneyEvenly(Money $total, $numberOfParts)
{
/*
* Use money's allocate method to divide the amount by the divisor - this is better than just dividing manually
* as the allocate algorithm ensures against the total changing due to rounding error.
*/
if (1 === $numberOfParts) {
return [$total];
}
if (!is_int($numberOfParts) || $numberOfParts <= 0) {
throw new \RuntimeException("Invalid divisor");
}
//Use an array with $numberOfParts elements each with value 1/$numberOfParts as the input $ratios
return $total->allocate(array_pad([], $numberOfParts, 1 / $numberOfParts));
}