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
|
3 |
|
$periods = new PaymentPeriods($paymentSchedule->getConfig()->getAverageIntervalLength()); |
21
|
|
|
|
22
|
3 |
|
$periodStart = clone $paymentSchedule->getConfig()->getStartDate(); |
23
|
|
|
|
24
|
3 |
|
foreach ($paymentSchedule->getPaymentDates() as $paymentNo => $paymentDate) { |
25
|
3 |
|
$periodStart = self::calculatePeriodStart($periodStart); |
26
|
3 |
|
$periodEnd = self::calculatePeriodEnd($paymentDate); |
27
|
|
|
|
28
|
3 |
|
$period = new Period($periodStart, $periodEnd); |
29
|
3 |
|
$periods->add($period, $paymentNo); |
30
|
|
|
|
31
|
3 |
|
$periodStart = clone $paymentDate; |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
return $periods; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \DateTimeInterface $periodStart |
39
|
|
|
* @return \DateTimeInterface |
40
|
|
|
*/ |
41
|
3 |
|
private static function calculatePeriodStart(\DateTimeInterface $periodStart): \DateTimeInterface |
42
|
|
|
{ |
43
|
3 |
|
$periodStart = clone $periodStart; |
44
|
|
|
// Move to next day |
45
|
3 |
|
$periodStart->add(new \DateInterval('P1D')); |
46
|
|
|
|
47
|
3 |
|
return $periodStart; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \DateTimeInterface $paymentDate |
52
|
|
|
* @return \DateTimeInterface |
53
|
|
|
*/ |
54
|
3 |
|
private static function calculatePeriodEnd(\DateTimeInterface $paymentDate): \DateTimeInterface |
55
|
|
|
{ |
56
|
3 |
|
$periodEnd = clone $paymentDate; |
57
|
|
|
// Move to the end of the day |
58
|
3 |
|
$periodEnd->add(new \DateInterval('P1D'))->sub(new \DateInterval('PT1S')); |
59
|
3 |
|
return $periodEnd; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|