-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathOpeningHours.php
441 lines (419 loc) · 13.4 KB
/
OpeningHours.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* OpeningHours.php
* 23-Oct-2012
*
* PHP Version 5
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <[email protected]>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link OpeningHours.php
*/
/**
* Services_OpenStreetMap_OpeningHours
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <[email protected]>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link OpeningHours.php
*/
class Services_OpenStreetMap_OpeningHours
{
/**
* The value set against the OpeningHours tag
*
* @var string
*/
protected $value;
/**
* Constructor
*
* @param string $value An opening_hours value
*/
public function __construct(string $value = null)
{
$this->value = $value;
}
/**
* Set opening_hours value.
*
* @param string $value An opening_hours value
*
* @return Services_OpenStreetMap_OpeningHours
*/
public function setValue(string $value): Services_OpenStreetMap_OpeningHours
{
$this->value = $value;
return $this;
}
/**
* Return true, false or null depending on whether the [opening hours]
* value explicitly indicates an open, closed or undecided result.
*
* @param int $time A numeric value representing a time. If null, the
* current time is used.
*
* @link http://wiki.openstreetmap.org/wiki/Key:opening_hours
* @return null|boolean
*/
public function isOpen(?int $time = null): ?bool
{
if ($time === null) {
$time = time();
}
$rVal = null;
switch ($this->value) {
case null:
$rVal = null;
break;
case '24/7':
$rVal = true;
break;
case 'sunrise-sunset':
$rVal = $this->_isBetweenSunriseAndSunset($time);
break;
default:
$matched = [];
$isVariableSunRiseSunSet = preg_match(
'/([^\(]?sunrise.*[^\)-]+).*-.*([^\(]?sunset.*[^\)])/u',
$this->value,
$matched
);
if ($isVariableSunRiseSunSet === 1) {
return $this->_isBetweenVariableSunriseAndSunset($time, $matched);
}
// time specs...
$rVal = $this->_evaluateComplexTimeSpec($time);
}
return $rVal;
}
/**
* Return true/false/null if a valid portion of an opening_hours value
* indicates whether a venue is open/closed or not incalculable.
*
* @param array $portions Part of an opening_hours specification
* @param int $time The time to calculate against.
*
* @return null|boolean
*/
private function _openTimeSpec($portions, $time):? bool
{
$days = $this->_daySpecToArray(trim($portions[0], ":"));
if (is_array($days)) {
return $this->_openTimeSpecDays($portions, $days, $time);
}
return $this->_openTimeSpecMonths($portions, $time);
}
/**
* Work on time-spec with day portion in spec
*
* @param array $portions Part of an opening_hour specification
* @param array $days Day spec converted to array
* @param int $time time value to evaluate against
*
* @return null|boolean
*/
private function _openTimeSpecDays($portions, $days, $time):? bool
{
foreach ($days as $day) {
$result = $this->_openTimeSpecDay($portions, $day, $time);
if (is_bool($result)) {
return $result;
}
}
return null;
}
/**
* _openTimeSpecDay
*
* @param array $portions Part of an opening_hour specification
* @param string $day Day spec
* @param int $time time value to evaluate against
*
* @return null|bool
*/
private function _openTimeSpecDay(array $portions, string $day, int $time):? bool
{
$daytime = strtolower(substr(date('D', $time), 0, 2));
$pattern = '/^[0-2][0-9]:[0-5][0-9]\+$/';
$open = null;
if ($day === $daytime) {
//day is a match
$time_spec = trim($portions[1]);
if (strtolower($time_spec) === 'off') {
$open = false;
} elseif (strpos($time_spec, '-') && (strpos($time_spec, ',') === false)) {
// specified starting and end times for just one range - not
// comma delimited.
$startend_times = explode('-', $time_spec);
$start = $this->_startTime($startend_times[0]);
$end = $this->_endTime($startend_times[1]);
$date = getdate($time);
$ctime = $date['hours'] * 60 + $date['minutes'];
$open = ($ctime >= $start && $ctime <= $end);
} elseif (strpos($time_spec, '-') && (strpos($time_spec, ','))) {
return $this->_timeIsBetweenTimeSpecTimes($time_spec, $time);
} elseif (preg_match($pattern, $time_spec)) {
// open-ended.
if (!$this->_evaluateOpenEnded($time_spec)) {
$open = false;
}
}
}
return $open;
}
/**
* _timeIsBetweenTimeSpecTimes
*
* @param string $time_spec Opening Hours specification
* @param int $time The time to calculate against.
*
* @return bool
*/
private function _timeIsBetweenTimeSpecTimes($time_spec, $time)
{
$times = explode(',', $time_spec);
$date = getdate($time);
$ctime = $date['hours'] * 60 + $date['minutes'];
foreach ($times as $time_spec) {
$startend_times = explode('-', trim($time_spec));
$start = $this->_startTime($startend_times[0]);
$end = $this->_endTime($startend_times[1]);
if ($ctime >= $start && $ctime <= $end) {
return true;
}
}
return false;
}
/**
* _evaluateComplexTimeSpec
*
* @param int $time Time to evaluate against
*
* @return bool
*/
private function _evaluateComplexTimeSpec($time): bool
{
$rule_sequences = explode(';', $this->value);
$day = strtolower(substr(date('D', $time), 0, 2));
$retval = false;
foreach ($rule_sequences as $rule_sequence) {
$rule_sequence = strtolower(trim($rule_sequence));
// If the day is explicitly specified in the rule sequence then
// processing it takes precedence.
if (preg_match('/' . $day . '/', $rule_sequence)) {
// @fixme: brittle. use preg_replace with \w
$portions = explode(' ', str_replace(', ', ',', $rule_sequence));
return $this->_openTimeSpec($portions, $time);
}
// @fixme: brittle. use preg_replace with \w
$portions = explode(' ', str_replace(', ', ',', $rule_sequence));
$retval = $this->_openTimeSpec($portions, $time) !== false;
}
return $retval;
}
/**
* OpenTimeSpecMonths
*
* @param array $spec time-spec
* @param integer $time time to evaluate against
*
* @return bool|null
*/
private function _openTimeSpecMonths($spec, $time):? bool
{
$months = [
'jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
];
$retVal = null;
if (in_array($spec[0], $months)) {
$month = strtolower(date('M', $time));
$time_spec = trim($spec[1]);
if ($spec[0] == $month && is_numeric($spec[1])) {
$startend_times = explode('-', $spec[2]);
$start = $this->_startTime($startend_times[0]);
$end = $this->_endTime($startend_times[1]);
$atime = getdate($time);
$ctime = ($atime['hours'] * 60) + $atime['minutes'];
$retVal = ($ctime >= $start && $ctime <= $end);
} elseif ($spec[0] === $month && $time_spec === 'off') {
$retVal = false;
}
}
if ($spec[0] === '24/7') {
$retVal = true;
}
return $retVal;
}
/**
* Convert a day list, such as mo-sa, into an array indicating
* which days have been specified.
*
* @param string $day_specification Day list, eg "mo-sa" or "mo,we"
*
* @return array
*/
private function _daySpecToArray(string $day_specification): ?array
{
$days = ['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su'];
$spec = trim(strtolower($day_specification));
$retVal = null;
if (strpos($spec, '-')) {
return $this->_narrowDayRange($days, $spec);
} elseif (strlen($spec) === 2) {
if (in_array($spec, $days)) {
$retVal = [$spec];
}
} elseif (strpos($spec, ',')) {
$delimited = explode(',', $spec);
$retVal = [];
foreach ($delimited as $item) {
if (in_array($item, $days)) {
$retVal[] = $item;
}
}
}
return $retVal;
}
/**
* Narrow day range
*
* @param array $days Days
* @param string $spec day specification
*
* @return array
*/
private function _narrowDayRange(array $days, string $spec): array
{
$start_day = '';
$pos = strpos($spec, '-');
if ($pos !== false) {
$start_day = substr($spec, 0, $pos);
}
$end_day = substr($spec, $pos + 1);
if ($start_day !== 'mo') {
foreach ($days as $day) {
if ($day === $start_day) {
break;
}
array_shift($days);
}
}
$rdays = array_reverse($days);
if ($end_day !== 'su') {
foreach ($rdays as $day) {
if ($day === $end_day) {
break;
}
array_shift($rdays);
}
$days = array_reverse($rdays);
}
return $days;
}
/**
* Return true/false depending on whether a given time_spec value is
* open-ended.
*
* @param string $time_spec Timespec
*
* @return bool
*/
private function _evaluateOpenEnded(string $time_spec): bool
{
$start = $this->_startTime($time_spec);
$d = getdate($start);
$ctime = $d['hours'] * 60 + $d['minutes'];
return ($ctime >= $start);
}
/**
* Return number of seconds representing the start time in
* the provided time_spec string.
*
* @param string $time_spec Timespec
*
* @return int
*/
private function _startTime(string $time_spec): int
{
$starthour = (int) substr($time_spec, 0, 2);
$startmin = (int) substr($time_spec, 3, 2);
return $starthour * 60 + $startmin;
}
/**
* Return number of seconds representing the end time in
* the provided time_spec string.
*
* @param string $time_spec Timespec
*
* @return int
*/
private function _endTime(string $time_spec): int
{
$endhour = (int) substr($time_spec, 0, 2);
$endmin = (int) substr($time_spec, 3);
return $endhour * 60 + $endmin;
}
/**
* Determine if the time is between a sunrise and sunset on day of that time
*
* @param int $time Time to check against
*
* @return bool
*/
private function _isBetweenSunriseAndSunset($time): bool
{
$start = $this->_startTime(date_sunrise($time));
$end = $this->_endTime(date_sunset($time));
$d = getdate($time);
$ctime = $d['hours'] * 60 + $d['minutes'];
return ($ctime >= $start && $ctime <= $end);
}
/**
* Determine if the time is between a variable sunrise and sunset.
*
* @param int $time Time to check against
* @param array $matched Matches from regexp
*
* @return bool
*/
private function _isBetweenVariableSunriseAndSunset($time, $matched): bool
{
$term1 = $matched[1];
$term1modifier = '';
$bork = strpbrk($term1, "+-");
$term1minutes = 0;
if ($bork !== false) {
$term1modifier = $bork[0];
$term1segments = sscanf(trim(substr($bork, 1)), "%d:%d");
$term1minutes = $term1segments[0] * 60 + $term1segments[1];
if ($term1modifier === '-') {
$term1minutes = -$term1minutes;
}
}
$term2 = $matched[2];
$bork2 = strpbrk($term2, "+-");
$term2minutes = 0;
if ($bork2 !== false) {
$term2modifier = $bork2[0];
$term2segments = sscanf(trim(substr($bork2, 1)), "%d:%d");
$term2minutes = $term2segments[0] * 60 + $term2segments[1];
if ($term2modifier === '-') {
$term2minutes = -$term2minutes;
}
}
$start = $this->_startTime(date_sunrise($time));
$start += $term1minutes;
$end = $this->_endTime(date_sunset($time));
$end += $term2minutes;
$d = getdate($time);
$ctime = $d['hours'] * 60 + $d['minutes'];
return ($ctime >= $start && $ctime <= $end );
}
}
// vim:set et ts=4 sw=4: