|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kauri\Loan; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class PaymentPeriodsFactory |
|
10
|
|
|
* @package Kauri\Loan |
|
11
|
|
|
*/ |
|
12
|
|
|
class PaymentPeriodsFactory implements PaymentPeriodsFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param PaymentScheduleInterface $paymentSchedule |
|
16
|
|
|
* @return PaymentPeriodsInterface |
|
17
|
|
|
*/ |
|
18
|
3 |
|
public static function generate(PaymentScheduleInterface $paymentSchedule): PaymentPeriodsInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
3 |
|
$periods = new PaymentPeriods($paymentSchedule->getConfig()->getAverageIntervalLength()); |
|
22
|
|
|
|
|
23
|
3 |
|
$periodStart = clone $paymentSchedule->getConfig()->getStartDate(); |
|
24
|
|
|
|
|
25
|
3 |
|
foreach ($paymentSchedule->getPaymentDates() as $paymentNo => $paymentDate) { |
|
26
|
3 |
|
$periodStart = self::calculatePeriodStart($periodStart); |
|
27
|
3 |
|
$periodEnd = self::calculatePeriodEnd($paymentDate); |
|
28
|
|
|
|
|
29
|
3 |
|
$period = new Period($periodStart, $periodEnd, $periods->getAvgPeriodLength()); |
|
30
|
3 |
|
$periods->add($period, $paymentNo); |
|
31
|
|
|
|
|
32
|
3 |
|
$periodStart = clone $paymentDate; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
return $periods; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \DateTimeInterface $periodStart |
|
40
|
|
|
* @return \DateTimeInterface |
|
41
|
|
|
*/ |
|
42
|
3 |
|
private static function calculatePeriodStart(\DateTimeInterface $periodStart): \DateTimeInterface |
|
43
|
|
|
{ |
|
44
|
3 |
|
$periodStart = clone $periodStart; |
|
45
|
|
|
// Move to next day |
|
46
|
3 |
|
$periodStart->add(new \DateInterval('P1D')); |
|
47
|
|
|
|
|
48
|
3 |
|
return $periodStart; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \DateTimeInterface $paymentDate |
|
53
|
|
|
* @return \DateTimeInterface |
|
54
|
|
|
*/ |
|
55
|
3 |
|
private static function calculatePeriodEnd(\DateTimeInterface $paymentDate): \DateTimeInterface |
|
56
|
|
|
{ |
|
57
|
3 |
|
$periodEnd = clone $paymentDate; |
|
58
|
|
|
// Move to the end of the day |
|
59
|
3 |
|
$periodEnd->add(new \DateInterval('P1D'))->sub(new \DateInterval('PT1S')); |
|
60
|
3 |
|
return $periodEnd; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|