/**
  * @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
0
 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);
 }
 /**
  * @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));
 }