|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kauri\Loan; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class PaymentPeriods |
|
9
|
|
|
* @package Kauri\Loan |
|
10
|
|
|
*/ |
|
11
|
|
|
class PaymentPeriods implements PaymentPeriodsInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $periods = array(); |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $averagePeriodLenght; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* PaymentPeriods constructor. |
|
24
|
|
|
* @param int $averagePeriodLenght |
|
25
|
|
|
*/ |
|
26
|
7 |
|
public function __construct(int $averagePeriodLenght) |
|
27
|
|
|
{ |
|
28
|
7 |
|
$this->averagePeriodLenght = $averagePeriodLenght; |
|
29
|
7 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param PeriodInterface $period |
|
33
|
|
|
* @param int|null $sequenceNo |
|
34
|
|
|
*/ |
|
35
|
7 |
|
public function add(PeriodInterface $period, int $sequenceNo = null): void |
|
36
|
|
|
{ |
|
37
|
7 |
|
if (is_null($sequenceNo)) { |
|
38
|
4 |
|
$sequenceNo = $this->getNoOfPeriods() + 1; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
7 |
|
$period->setSequenceNo($sequenceNo); |
|
42
|
7 |
|
$this->periods[$sequenceNo] = $period; |
|
43
|
7 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $calculationMode |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function getPeriodsLengths(int $calculationMode = self::CALCULATION_MODE_AVERAGE): array |
|
50
|
|
|
{ |
|
51
|
5 |
|
$periodsLengths = array(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var PeriodInterface $period */ |
|
54
|
5 |
|
foreach ($this->getPeriods() as $period) { |
|
55
|
5 |
|
$length = $period->getLength(); |
|
56
|
|
|
|
|
57
|
5 |
|
if ($calculationMode == self::CALCULATION_MODE_AVERAGE) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$length = $this->averagePeriodLenght; |
|
60
|
|
|
} |
|
61
|
5 |
|
$periodsLengths[$period->getSequenceNo()] = $length; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
return $periodsLengths; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
7 |
|
public function getPeriods(): array |
|
71
|
|
|
{ |
|
72
|
7 |
|
return $this->periods; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function getNoOfPeriods(): int |
|
79
|
|
|
{ |
|
80
|
4 |
|
return count($this->periods); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|