|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kauri\Loan; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class PaymentScheduleConfig implements PaymentScheduleConfigInterface |
|
9
|
|
|
{ |
|
10
|
|
|
private $noOfPayments; |
|
11
|
|
|
private $startDate; |
|
12
|
|
|
private $averageIntervalLength = 0; |
|
13
|
|
|
private $dateInterval; |
|
14
|
|
|
private $firstPaymentDate; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* PaymentScheduleConfig constructor. |
|
18
|
|
|
* @param int $noOfPayments |
|
19
|
|
|
* @param \DateTimeInterface $startDate |
|
20
|
|
|
* @param string $dateIntervalPattern |
|
21
|
|
|
* @param \DateTimeInterface|null $firstPaymentDate |
|
22
|
|
|
*/ |
|
23
|
8 |
|
public function __construct( |
|
24
|
|
|
int $noOfPayments, |
|
25
|
|
|
\DateTimeInterface $startDate, |
|
26
|
|
|
string $dateIntervalPattern, |
|
27
|
|
|
\DateTimeInterface $firstPaymentDate = null |
|
28
|
|
|
) { |
|
29
|
8 |
|
$this->noOfPayments = $noOfPayments; |
|
30
|
8 |
|
$this->startDate = new \DateTime($startDate->format('Y-m-d'), new \DateTimeZone('UTC')); |
|
31
|
|
|
|
|
32
|
8 |
|
if (!is_null($firstPaymentDate)) { |
|
33
|
4 |
|
$this->firstPaymentDate = new \DateTime($firstPaymentDate->format('Y-m-d'), new \DateTimeZone('UTC')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
$this->dateInterval = new \DateInterval($dateIntervalPattern); |
|
37
|
|
|
|
|
38
|
8 |
|
$this->averageIntervalLength = $this->extractIntervalLength($this->dateInterval); |
|
39
|
8 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param \DateInterval $dateInterval |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
8 |
|
private function extractIntervalLength(\DateInterval $dateInterval): int |
|
46
|
|
|
{ |
|
47
|
8 |
|
$intervalLength = 0; |
|
48
|
|
|
$intervalMultiplier = array( |
|
49
|
8 |
|
'd' => 1, |
|
50
|
|
|
'm' => 30, |
|
51
|
|
|
'y' => 30 * 12 |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
8 |
|
foreach ($intervalMultiplier as $pattern => $multiplier) { |
|
55
|
8 |
|
if ($dateInterval->format('%' . $pattern) > 0) { |
|
56
|
8 |
|
$intervalLength = $intervalLength + (int)$dateInterval->format('%' . $pattern) * $multiplier; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
return (int)$intervalLength; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
7 |
|
public function getNoOfPayments(): int |
|
67
|
|
|
{ |
|
68
|
7 |
|
return $this->noOfPayments; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return \DateTimeInterface |
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function getStartDate(): \DateTimeInterface |
|
75
|
|
|
{ |
|
76
|
7 |
|
return $this->startDate; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getAverageIntervalLength(): int |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->averageIntervalLength; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \DateInterval |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function getDateInterval(): \DateInterval |
|
91
|
|
|
{ |
|
92
|
7 |
|
return $this->dateInterval; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \DateTimeInterface|null |
|
97
|
|
|
*/ |
|
98
|
7 |
|
public function getFirstPaymentDate(): ?\DateTimeInterface |
|
99
|
|
|
{ |
|
100
|
7 |
|
return $this->firstPaymentDate; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|