1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\DateUtils; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Countable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Ivan Kerin <[email protected]> |
10
|
|
|
* @copyright 2015, Clippings Ltd. |
11
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
12
|
|
|
*/ |
13
|
|
|
class Holidays implements Countable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var DateTime[] |
17
|
|
|
*/ |
18
|
|
|
private $dates = []; |
19
|
|
|
|
20
|
1 |
|
public function __construct(array $dates = array()) |
21
|
|
|
{ |
22
|
1 |
|
foreach ($dates as $date) { |
23
|
1 |
|
$this->add($date); |
24
|
|
|
} |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
1 |
|
public function isEmpty() |
31
|
|
|
{ |
32
|
1 |
|
return empty($this->dates); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Implement countable |
37
|
|
|
* |
38
|
|
|
* @return integer |
39
|
|
|
*/ |
40
|
1 |
|
public function count() |
41
|
|
|
{ |
42
|
1 |
|
return count($this->dates); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return DateTime[] |
47
|
|
|
*/ |
48
|
1 |
|
public function getDates() |
49
|
|
|
{ |
50
|
1 |
|
return $this->dates; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param DateTime $current |
|
|
|
|
55
|
|
|
* @param DateTime $to |
|
|
|
|
56
|
|
|
*/ |
57
|
1 |
|
public function addDateTimeSpan(DateTimeSpan $span) |
58
|
|
|
{ |
59
|
1 |
|
$current = clone $span->getFrom(); |
60
|
|
|
|
61
|
1 |
|
while ($current <= $span->getTo()) { |
62
|
1 |
|
$this->add($current); |
63
|
|
|
|
64
|
1 |
|
$current = clone $current; |
65
|
1 |
|
$current->modify('+1 day'); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param DateTime $day |
73
|
|
|
*/ |
74
|
1 |
|
public function add(DateTime $day) |
75
|
|
|
{ |
76
|
1 |
|
$this->dates []= $day; |
77
|
|
|
|
78
|
1 |
|
sort($this->dates); |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if a date is within any holiday |
85
|
|
|
* |
86
|
|
|
* @param DateTime $date |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
1 |
|
public function isActive(DateTime $date = null) |
90
|
|
|
{ |
91
|
1 |
|
$date = $date ?: new DateTime(); |
92
|
|
|
|
93
|
1 |
|
foreach ($this->dates as $holiday) { |
94
|
1 |
|
if ($date->format('Y-m-d') == $holiday->format('Y-m-d')) { |
95
|
1 |
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param DateTimeSpan $span |
104
|
|
|
* @param DateTime $start_date |
105
|
|
|
* @return DateTimeSpan |
106
|
|
|
*/ |
107
|
5 |
|
public function extendDateTimeSpan(DateTimeSpan $span, DateTime $start_date = null) |
108
|
|
|
{ |
109
|
5 |
|
$from = clone $span->getFrom(); |
110
|
5 |
|
$to = clone $span->getTo(); |
111
|
|
|
|
112
|
5 |
|
foreach ($this->dates as $holiday) { |
113
|
5 |
|
if ($start_date and $holiday < $start_date) { |
114
|
1 |
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
if ($holiday <= $from) { |
118
|
3 |
|
$from->modify('+1 day'); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
if ($holiday <= $to) { |
122
|
5 |
|
$to->modify('+1 day'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
5 |
|
return new DateTimeSpan($from, $to); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param DateTimeSpan $span |
131
|
|
|
* @param DateTime $start_date |
132
|
|
|
* @return DateTimeSpan |
133
|
|
|
*/ |
134
|
5 |
|
public function extendBusinessDateTimeSpan(DateTimeSpan $span, DateTime $start_date = null) |
135
|
|
|
{ |
136
|
5 |
|
$from = clone $span->getFrom(); |
137
|
5 |
|
$to = clone $span->getTo(); |
138
|
|
|
|
139
|
5 |
|
foreach ($this->dates as $holiday) { |
140
|
5 |
|
if (($start_date and $holiday < $start_date) or 5 < $holiday->format('N')) { |
141
|
5 |
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
5 |
|
if ($holiday <= $from) { |
145
|
3 |
|
$from->modify('+1 weekday'); |
146
|
|
|
} |
147
|
|
|
|
148
|
5 |
|
if ($holiday <= $to) { |
149
|
5 |
|
$to->modify('+1 weekday'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
5 |
|
return new DateTimeSpan($from, $to); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.