-
Notifications
You must be signed in to change notification settings - Fork 20
/
Calculator.php
244 lines (205 loc) · 5.15 KB
/
Calculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace BusinessDays;
/**
* Class Calculator
*
* @package BusinessDays
*/
class Calculator
{
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
const WEEK_DAY_FORMAT = 'N';
const HOLIDAY_FORMAT = 'm-d';
const FREE_DAY_FORMAT = 'Y-m-d';
/** @var \DateTime */
private $date;
/** @var \DateTime[] */
private $holidays = array();
/** @var \DateTime[] */
private $freeDays = array();
/** @var int[] */
private $freeWeekDays = array();
/**
* @param \DateTime $startDate Date to start calculations from
*
* @return $this
*/
public function setStartDate(\DateTime $startDate)
{
// Use clone so parameter is not passed as a reference.
// If not, it can brake caller method by changing $startDate parameter while changing it here.
$this->date = clone $startDate;
return $this;
}
/**
* @param \DateTime[] $holidays Array of holidays that repeats each year. (Only month and date is used to match).
*
* @return $this
*/
public function setHolidays(array $holidays)
{
$this->holidays = $holidays;
return $this;
}
/**
* @return \DateTime[]
*/
private function getHolidays()
{
return $this->holidays;
}
/**
* @param \DateTime[] $freeDays Array of free days that dose not repeat.
*
* @return $this
*/
public function setFreeDays(array $freeDays)
{
$this->freeDays = $freeDays;
return $this;
}
/**
* @return \DateTime[]
*/
private function getFreeDays()
{
return $this->freeDays;
}
/**
* @param int[] $freeWeekDays Array of days of the week which are not business days.
*
* @return $this
*/
public function setFreeWeekDays(array $freeWeekDays)
{
$this->freeWeekDays = $freeWeekDays;
return $this;
}
/**
* @return int[]
*/
private function getFreeWeekDays()
{
if (count($this->freeWeekDays) >= 7) {
throw new \InvalidArgumentException('Too many non business days provided');
}
return $this->freeWeekDays;
}
/**
* @param int $howManyDays
*
* @return $this
*/
public function addBusinessDays($howManyDays)
{
if ($howManyDays < 1) {
throw new \InvalidArgumentException('The parameter $howManyDays must be greater than 0');
}
$iterator = 0;
while ($iterator < $howManyDays) {
$this->getDate()->modify('+1 day');
if ($this->isBusinessDay($this->getDate())) {
$iterator++;
}
}
return $this;
}
/**
* @param int $howManyDays
*
* @return $this
*/
public function subBusinessDays($howManyDays)
{
if ($howManyDays < 1) {
throw new \InvalidArgumentException('The parameter $howManyDays must be greater than 0');
}
$iterator = 0;
while ($iterator < $howManyDays) {
if ($this->isBusinessDay($this->getDate())) {
$iterator++;
}
if ($iterator < $howManyDays) { //Don`t modify the date if we are on the last iteration
$this->getDate()->modify('-1 day');
}
}
return $this;
}
/**
* @return \DateTime
*/
public function getDate()
{
if ($this->date === null) {
$this->date = new \DateTime();
}
return $this->date;
}
/**
* @param \DateTime $date
*
* @return bool
*/
public function isBusinessDay(\DateTime $date)
{
if ($this->isFreeWeekDayDay($date)) {
return false;
}
if ($this->isHoliday($date)) {
return false;
}
if ($this->isFreeDay($date)) {
return false;
}
return true;
}
/**
* @param \DateTime $date
*
* @return bool
*/
public function isFreeWeekDayDay(\DateTime $date)
{
$currentWeekDay = (int)$date->format(self::WEEK_DAY_FORMAT);
if (in_array($currentWeekDay, $this->getFreeWeekDays())) {
return true;
}
return false;
}
/**
* @param \DateTime $date
*
* @return bool
*/
public function isHoliday(\DateTime $date)
{
$holidayFormatValue = $date->format(self::HOLIDAY_FORMAT);
foreach ($this->getHolidays() as $holiday) {
if ($holidayFormatValue == $holiday->format(self::HOLIDAY_FORMAT)) {
return true;
}
}
return false;
}
/**
* @param \DateTime $date
*
* @return bool
*/
public function isFreeDay(\DateTime $date)
{
$freeDayFormatValue = $date->format(self::FREE_DAY_FORMAT);
foreach ($this->getFreeDays() as $freeDay) {
if ($freeDayFormatValue == $freeDay->format(self::FREE_DAY_FORMAT)) {
return true;
}
}
return false;
}
}