-
Notifications
You must be signed in to change notification settings - Fork 55
/
StoreHours2.class.php
376 lines (306 loc) · 10.4 KB
/
StoreHours2.class.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* ----------------------------------
* PHP STORE HOURS
* ----------------------------------
* Version 3.1
* Written by Cory Etzkorn
* https://github.com/coryetzkorn/php-store-hours
*
* DO NOT MODIFY THIS CLASS FILE
*/
class StoreHours
{
/**
*
* @var array
*/
private $hours;
/**
*
* @var array
*/
private $exceptions;
/**
*
* @var array
*/
private $templates;
/**
*
* @var boolean
*/
private $yesterdayFlag;
/**
*
* @param array $hours
* @param array $exceptions
* @param array $templates
*/
public function __construct($hours = array(), $exceptions = array(), $templates = array())
{
$this->exceptions = $exceptions;
$this->templates = $templates;
$this->yesterdayFlag = false;
$weekdayToIndex = array(
'mon' => 1,
'tue' => 2,
'wed' => 3,
'thu' => 4,
'fri' => 5,
'sat' => 6,
'sun' => 7
);
$this->hours = array();
foreach ($hours as $key => $value) {
$this->hours[$weekdayToIndex[$key]] = $value;
}
// Remove empty elements from values (backwards compatibility)
foreach ($this->hours as $key => $value) {
$this->hours[$key] = array_filter($value, function ($element) {
return (trim($element) !== '');
});
}
// Remove empty elements from values (backwards compatibility)
foreach ($this->exceptions as $key => $value) {
$this->exceptions[$key] = array_filter($value, function ($element) {
return (trim($element) !== '');
});
}
$defaultTemplates = array(
'open' => '<h3>Yes, we\'re open! Today\'s hours are {%hours%}.</h3>',
'closed' => '<h3>Sorry, we\'re closed. Today\'s hours are {%hours%}.</h3>',
'closed_all_day' => '<h3>Sorry, we\'re closed.</h3>',
'separator' => ' - ',
'join' => ' and ',
'format' => 'g:ia',
'hours' => '{%open%}{%separator%}{%closed%}',
'overview_separator' => '-',
'overview_join' => ', ',
'overview_format' => 'g:ia',
'overview_weekdays' => array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
);
$this->templates += $defaultTemplates;
}
/**
*
* @param string $timestamp
* @return array Today's hours
*/
public function hours_today($timestamp = null)
{
$timestamp = (null !== $timestamp) ? $timestamp : time();
$today = strtotime(date('Y-m-d', $timestamp) . ' midnight');
$weekday_short = date('N', $timestamp);
$hours_today = array();
if (isset($this->hours[$weekday_short])) {
$hours_today = $this->hours[$weekday_short];
}
foreach ($this->exceptions as $ex_day => $ex_hours) {
if (strtotime($ex_day) === $today) {
// Today is an exception, use alternate hours instead
$hours_today = $ex_hours;
}
}
return $hours_today;
}
/**
*
* @param string $timestamp
* @return boolean
*/
public function is_open($timestamp = null)
{
$timestamp = (null !== $timestamp) ? $timestamp : time();
$is_open = false;
$this->yesterdayFlag = false;
// Check whether shop's still open from day before
$ts_yesterday = strtotime(date('Y-m-d H:i:s', $timestamp) . ' -1 day');
$yesterday = date('Y-m-d', $ts_yesterday);
$hours_yesterday = $this->hours_today($ts_yesterday);
foreach ($hours_yesterday as $range) {
$range = explode('-', $range);
$start = strtotime($yesterday . ' ' . $range[0]);
$end = strtotime($yesterday . ' ' . $range[1]);
if ($end <= $start) {
$end = strtotime($yesterday . ' ' . $range[1] . ' +1 day');
}
if ($start <= $timestamp && $timestamp <= $end) {
$is_open = true;
$this->yesterdayFlag = true;
break;
}
}
// Check today's hours
if (!$is_open) {
$day = date('Y-m-d', $timestamp);
$hours_today = $this->hours_today($timestamp);
foreach ($hours_today as $range) {
$range = explode('-', $range);
$start = strtotime($day . ' ' . $range[0]);
$end = strtotime($day . ' ' . $range[1]);
if ($end <= $start) {
$end = strtotime($day . ' ' . $range[1] . ' +1 day');
}
if ($start <= $timestamp && $timestamp <= $end) {
$is_open = true;
break;
}
}
}
return $is_open;
}
/**
* Prep HTML
*
* @param string $template_name
* @param int $timestamp
*/
private function render_html($template_name, $timestamp)
{
$template = $this->templates;
$hours_today = $this->hours_today($timestamp);
$day = date('Y-m-d', $timestamp);
$output = '';
if (count($hours_today) > 0) {
$hours_template = '';
$first = true;
foreach ($hours_today as $range) {
$range = explode('-', $range);
$start = strtotime($day . ' ' . $range[0]);
$end = strtotime($day . ' ' . $range[1]);
if (false === $first) {
$hours_template .= $template['join'];
}
$hours_template .= $template['hours'];
$hours_template = str_replace('{%open%}', date($template['format'], $start), $hours_template);
$hours_template = str_replace('{%closed%}', date($template['format'], $end), $hours_template);
$hours_template = str_replace('{%separator%}', $template['separator'], $hours_template);
$first = false;
}
$output .= str_replace('{%hours%}', $hours_template, $template[$template_name]);
} else {
$output .= $template['closed_all_day'];
}
echo $output;
}
/**
* Output HTML
*
* @param string $timestamp
*/
public function render($timestamp = null)
{
$timestamp = (null !== $timestamp) ? $timestamp : time();
if ($this->is_open($timestamp)) {
// Print yesterday's hours if shop's still open from day before
if ($this->yesterdayFlag) {
$timestamp = strtotime(date('Y-m-d H:i:s', $timestamp) . ' -1 day');
}
$this->render_html('open', $timestamp);
} else {
$this->render_html('closed', $timestamp);
}
}
/**
*
* @param array $ranges
* @return string
*/
private function hours_overview_format_hours(array $ranges)
{
$hoursparts = array();
foreach ($ranges as $range) {
$day = '2016-01-01';
$range = explode('-', $range);
$start = strtotime($day . ' ' . $range[0]);
$end = strtotime($day . ' ' . $range[1]);
$hoursparts[] = date($this->templates['overview_format'], $start)
. $this->templates['overview_separator']
. date($this->templates['overview_format'], $end);
}
return implode($this->templates['overview_join'], $hoursparts);
}
/**
*
*/
private function hours_this_week_simple()
{
$lookup = array_combine(range(1, 7), $this->templates['overview_weekdays']);
$ret = array();
for ($i = 1; $i <= 7; $i++) {
$hours_str = (isset($this->hours[$i]) && count($this->hours[$i]) > 0)
? $this->hours_overview_format_hours($this->hours[$i])
: '-';
$ret[$lookup[$i]] = $hours_str;
}
return $ret;
}
/**
*
* @return array
*/
private function hours_this_week_grouped()
{
$lookup = array_combine(range(1, 7), $this->templates['overview_weekdays']);
$blocks = array();
// Remove empty elements ("closed all day")
$hours = array_filter($this->hours, function ($element) {
return (count($element) > 0);
});
foreach ($hours as $weekday => $hours2) {
foreach ($blocks as &$block) {
if ($block['hours'] === $hours2) {
$block['days'][] = $weekday;
continue 2;
}
}
unset($block);
$blocks[] = array('days' => array($weekday), 'hours' => $hours2);
}
// Flatten
$ret = array();
foreach ($blocks as $block) {
// Format days
$keyparts = array();
$keys = $block['days'];
$buffer = array();
$lastIndex = null;
$minGroupSize = 3;
foreach ($keys as $index) {
if ($lastIndex !== null && $index - 1 !== $lastIndex) {
if (count($buffer) >= $minGroupSize) {
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
} else {
foreach ($buffer as $b) {
$keyparts[] = $lookup[$b];
}
}
$buffer = array();
}
$buffer[] = $index;
$lastIndex = $index;
}
if (count($buffer) >= $minGroupSize) {
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
} else {
foreach ($buffer as $b) {
$keyparts[] = $lookup[$b];
}
}
// Combine
$ret[implode(', ', $keyparts)] = $this->hours_overview_format_hours($block['hours']);
}
return $ret;
}
/**
*
* @return array
*/
public function hours_this_week($groupSameDays = false)
{
return (true === $groupSameDays)
? $this->hours_this_week_grouped()
: $this->hours_this_week_simple();
}
}