|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kauri\Loan; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Period |
|
9
|
|
|
* @package Kauri\Loan |
|
10
|
|
|
*/ |
|
11
|
|
|
class Period implements PeriodInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \DateTimeInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $start; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \DateTimeInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $end; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var float |
|
23
|
|
|
*/ |
|
24
|
|
|
private $exactLength; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var float |
|
27
|
|
|
*/ |
|
28
|
|
|
private $avgLength; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $sequenceNo; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Period constructor. |
|
36
|
|
|
* @param \DateTimeInterface $start |
|
37
|
|
|
* @param \DateTimeInterface $end |
|
38
|
|
|
* @param float $avgLength |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(\DateTimeInterface $start, \DateTimeInterface $end, float $avgLength) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->start = $start; |
|
43
|
3 |
|
$this->end = $end; |
|
44
|
3 |
|
$this->exactLength = (float) self::calculatePeriodLength($start, $end); |
|
45
|
3 |
|
$this->avgLength = $avgLength; |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $lengthMode |
|
50
|
|
|
* @return float |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function getLength(int $lengthMode): float |
|
53
|
|
|
{ |
|
54
|
3 |
|
if (self::LENGTH_MODE_EXACT == $lengthMode) { |
|
55
|
3 |
|
return $this->getExactLength(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
return $this->getAvgLength(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return float |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function getAvgLength(): float |
|
65
|
|
|
{ |
|
66
|
3 |
|
return $this->avgLength; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return float |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function getExactLength(): float |
|
73
|
|
|
{ |
|
74
|
3 |
|
return $this->exactLength; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \DateTimeInterface |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getEnd(): \DateTimeInterface |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->end; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \DateTimeInterface |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function getStart(): \DateTimeInterface |
|
89
|
|
|
{ |
|
90
|
3 |
|
return $this->start; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $sequenceNo |
|
95
|
|
|
* @return bool|int |
|
96
|
|
|
*/ |
|
97
|
5 |
|
public function setSequenceNo(int $sequenceNo) |
|
98
|
|
|
{ |
|
99
|
5 |
|
if (is_null($this->sequenceNo)) { |
|
100
|
5 |
|
$this->sequenceNo = $sequenceNo; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
return $this->sequenceNo; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
5 |
|
public function getSequenceNo(): int |
|
110
|
|
|
{ |
|
111
|
5 |
|
return (int) $this->sequenceNo; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param \DateTimeInterface $periodStart |
|
116
|
|
|
* @param \DateTimeInterface $periodEnd |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
3 |
|
private static function calculatePeriodLength(\DateTimeInterface $periodStart, \DateTimeInterface $periodEnd): int |
|
120
|
|
|
{ |
|
121
|
3 |
|
$diff = (int) $periodEnd->diff($periodStart)->days + 1; |
|
122
|
3 |
|
return $diff; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|