|
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
|
1 |
|
} |
|
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
|
1 |
|
} |
|
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
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param DateTime $from |
|
|
|
|
|
|
104
|
|
|
* @param DateTime $date |
|
|
|
|
|
|
105
|
|
|
* @return DateTime |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function extendDateTimeSpan(DateTimeSpan $span) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$to = clone $span->getTo(); |
|
110
|
|
|
|
|
111
|
1 |
|
foreach ($this->dates as $holiday) { |
|
112
|
1 |
|
if ($holiday > $span->getFrom() and $holiday < $to) { |
|
113
|
1 |
|
$to->modify('+1 day'); |
|
114
|
1 |
|
} |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
return new DateTimeSpan($span->getFrom(), $to); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.