1 | <?php |
||
2 | |||
3 | namespace carono\production; |
||
4 | |||
5 | /** |
||
6 | * Class Calendar |
||
7 | * |
||
8 | * @package carono\production |
||
9 | */ |
||
10 | class Calendar |
||
11 | { |
||
12 | public $format = 'Y-m-d'; |
||
13 | private static $_instance; |
||
14 | protected static $holidays; |
||
15 | /** |
||
16 | * @var \DateTime |
||
17 | */ |
||
18 | private static $date; |
||
19 | |||
20 | public function __toString() |
||
21 | { |
||
22 | return $this->date()->format($this->format); |
||
23 | } |
||
24 | |||
25 | public static function date() |
||
26 | { |
||
27 | return static::$date; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
28 | } |
||
29 | |||
30 | public function timestamp() |
||
31 | { |
||
32 | return static::date()->getTimestamp(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param \DateTime|string $date |
||
37 | * |
||
38 | * @param array $weekend |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | public static function isWorking($date, $weekend = [6, 0]) |
||
43 | { |
||
44 | $isWork = static::findDateInArray($date, static::getWorkingsByYear($date)); |
||
45 | $isWeekend = static::isWeekend($date, $weekend); |
||
46 | $isHoliday = static::isHoliday($date); |
||
47 | |||
48 | return static::isPreHoliday($date) || (!$isHoliday && !$isWeekend) || $isWork; |
||
49 | } |
||
50 | |||
51 | protected static function findDateInArray($date, $array) |
||
52 | { |
||
53 | $date = static::prepareDate($date); |
||
54 | return in_array($date->format('Y-m-d'), $array); |
||
55 | |||
56 | } |
||
57 | |||
58 | public static function isPreHoliday($date) |
||
59 | { |
||
60 | return static::findDateInArray($date, static::getPreHolidaysByYear($date)); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param $date |
||
65 | * @return bool |
||
66 | */ |
||
67 | public static function isHoliday($date = null) |
||
68 | { |
||
69 | return (static::isWeekend($date) && !static::isPreHoliday($date)) || static::findDateInArray($date, static::getHolidaysByYear($date)); |
||
70 | } |
||
71 | |||
72 | public static function isWeekend($date, $weekend = [6, 0]) |
||
73 | { |
||
74 | $date = static::prepareDate($date); |
||
75 | return in_array($date->format('w'), $weekend); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param integer|string|\DateTime $year |
||
80 | * @return array |
||
81 | */ |
||
82 | public static function getHolidaysByYear($year) |
||
83 | { |
||
84 | if (!is_numeric($year)) { |
||
85 | $year = static::prepareDate($year)->format('Y'); |
||
86 | } |
||
87 | $holidays = static::getHolidays(); |
||
88 | return isset($holidays[$year]['holidays']) ? $holidays[$year]['holidays'] : []; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param integer|string|\DateTime $year |
||
93 | * @return array |
||
94 | */ |
||
95 | public static function getWorkingsByYear($year) |
||
96 | { |
||
97 | if (!is_numeric($year)) { |
||
98 | $year = static::prepareDate($year)->format('Y'); |
||
99 | } |
||
100 | $holidays = static::getHolidays(); |
||
101 | return isset($holidays[$year]['works']) ? $holidays[$year]['works'] : []; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param integer|string|\DateTime $year |
||
106 | * @return array |
||
107 | */ |
||
108 | public static function getPreHolidaysByYear($year) |
||
109 | { |
||
110 | if (!is_numeric($year)) { |
||
111 | $year = static::prepareDate($year)->format('Y'); |
||
112 | } |
||
113 | $holidays = static::getHolidays(); |
||
114 | return isset($holidays[$year]['preholidays']) ? $holidays[$year]['preholidays'] : []; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function day() |
||
121 | { |
||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param null|string $format |
||
127 | * @return string |
||
128 | */ |
||
129 | public function format($format = null) |
||
130 | { |
||
131 | return $this->date()->format($format ? $format : $this->format); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function working() |
||
138 | { |
||
139 | while (!static::isWorking(static::$date)) { |
||
0 ignored issues
–
show
|
|||
140 | $this->next(); |
||
141 | } |
||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function holiday() |
||
149 | { |
||
150 | while (!static::isHoliday(static::$date) && static::haveData()) { |
||
0 ignored issues
–
show
|
|||
151 | $this->next(); |
||
152 | } |
||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | protected static function haveData($date = null) |
||
157 | { |
||
158 | $date = $date ? static::prepareDate($date) : static::date(); |
||
159 | return isset(static::getHolidays()[$date->format('Y')]); |
||
160 | |||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function preHoliday() |
||
167 | { |
||
168 | while (!static::isPreHoliday(static::$date) && static::haveData($this->date())) { |
||
0 ignored issues
–
show
|
|||
169 | $this->next(); |
||
170 | } |
||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function next() |
||
178 | { |
||
179 | static::$date->add(new \DateInterval('P1D')); |
||
0 ignored issues
–
show
|
|||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function prev() |
||
187 | { |
||
188 | static::$date->sub(new \DateInterval('P1D')); |
||
0 ignored issues
–
show
|
|||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | public static function getInstance() |
||
193 | { |
||
194 | return static::$_instance ?: static::find(); |
||
0 ignored issues
–
show
|
|||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param null|string|\DateTime $date |
||
199 | * @return Calendar |
||
200 | */ |
||
201 | public static function find($date = null) |
||
202 | { |
||
203 | static::$date = static::prepareDate($date); |
||
0 ignored issues
–
show
|
|||
204 | if (static::$_instance) { |
||
0 ignored issues
–
show
|
|||
205 | return static::$_instance; |
||
206 | } else { |
||
207 | $json = file_get_contents(__DIR__ . '/holidays.json'); |
||
208 | static::$_instance = new self(); |
||
209 | static::$holidays = json_decode($json, true); |
||
210 | return static::$_instance; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param string|\DateTime $date |
||
216 | * @return \DateTime |
||
217 | */ |
||
218 | protected static function prepareDate($date) |
||
219 | { |
||
220 | if (is_null($date) && static::$date) { |
||
0 ignored issues
–
show
|
|||
221 | $date = static::$date; |
||
222 | } elseif (!$date instanceof \DateTime) { |
||
223 | $date = new \DateTime($date); |
||
224 | } |
||
225 | return $date; |
||
226 | } |
||
227 | |||
228 | public static function getHolidays() |
||
229 | { |
||
230 | if (!static::$_instance) { |
||
0 ignored issues
–
show
|
|||
231 | static::find(); |
||
232 | } |
||
233 | return static::$holidays; |
||
234 | } |
||
235 | |||
236 | private function __construct() |
||
237 | { |
||
238 | } |
||
239 | |||
240 | protected function __clone() |
||
241 | { |
||
242 | } |
||
243 | } |