1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhFlexiTime\Options; |
4
|
|
|
|
5
|
|
|
use JhFlexiTime\DateTime\DateTime; |
6
|
|
|
use Zend\Stdlib\AbstractOptions; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class NotificationOptions |
10
|
|
|
* @package JhFlexiTime\Options |
11
|
|
|
* @author Aydin Hassan <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class NotificationOptions extends AbstractOptions |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var DateTime |
18
|
|
|
*/ |
19
|
|
|
protected $remindStart = '2 days ago'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $remindDays = '7 days'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return DateTime |
28
|
|
|
*/ |
29
|
|
|
public function getRemindStart() |
30
|
|
|
{ |
31
|
|
|
$date = new DateTime($this->remindStart); |
32
|
|
|
$date->setTime(0, 0, 0); |
33
|
|
|
return $date; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $remindStart |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function setRemindStart($remindStart) |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
new DateTime($remindStart); |
44
|
|
|
} catch (\Exception $e) { |
45
|
|
|
throw $e; |
46
|
|
|
} |
47
|
|
|
$this->remindStart = $remindStart; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return DateTime |
52
|
|
|
*/ |
53
|
|
|
public function getRemindDays() |
54
|
|
|
{ |
55
|
|
|
return $this->remindDays; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $remindDays |
60
|
|
|
*/ |
61
|
|
|
public function setRemindDays($remindDays) |
62
|
|
|
{ |
63
|
|
|
if (!preg_match('/^\s*\d+\s+days?\s*$/', $remindDays)) { |
64
|
|
|
throw new \InvalidArgumentException('remind_days should be like: "7 days"'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->remindDays = $remindDays; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \JhFlexiTime\DateTime\DateTime[] |
72
|
|
|
*/ |
73
|
|
|
public function getRemindPeriod() |
74
|
|
|
{ |
75
|
|
|
$interval = \DateInterval::createFromDateString($this->remindDays); |
76
|
|
|
return $this->getXWorkingDaysToDate($this->getRemindStart(), (int) $interval->format('%d')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get an array of dates, counting backwards using |
81
|
|
|
* passed in total. Any dates which are weekends are excluded. |
82
|
|
|
* You will always get an array of dates with a count equal to that of |
83
|
|
|
* $totalDaysToCountBack |
84
|
|
|
* |
85
|
|
|
* |
86
|
|
|
* @param DateTime $toDate |
87
|
|
|
* @param int $totalDaysToCountBack |
88
|
|
|
* |
89
|
|
|
* @return DateTime[] |
90
|
|
|
*/ |
91
|
|
|
private function getXWorkingDaysToDate(DateTime $toDate, $totalDaysToCountBack) |
92
|
|
|
{ |
93
|
|
|
$dates = []; |
94
|
|
|
$date = clone $toDate; |
95
|
|
|
$date->add(new \DateInterval('P1D')); |
96
|
|
|
while ($totalDaysToCountBack > 0) { |
97
|
|
|
$date = clone $date; |
98
|
|
|
$date->sub(new \DateInterval('P1D')); |
99
|
|
|
|
100
|
|
|
if ($date->format('N') < 6) { |
101
|
|
|
array_unshift($dates, $date); |
102
|
|
|
$totalDaysToCountBack--; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $dates; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..