/
OpeningHours.php
261 lines (248 loc) · 8.61 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
<?php
/**
* OpeningHours.php
* 23-Oct-2012
*
* PHP Version 5
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link OpeningHours.php
* @todo
*/
/**
* Services_OpenStreetMap_OpeningHours
*
* @category Services
* @package Services_OpenStreetMap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link OpeningHours.php
*/
class Services_OpenStreetMap_OpeningHours
{
protected $value;
/**
* __construct
*
* @param string $value An opening_hours value
*
* @return Services_OpenStreetMap_OpeningHours
*/
public function __construct($value = null)
{
$this->value = $value;
return $this;
}
/**
* Set opening_hours value.
*
* @param string $value An opening_hours value
*
* @return Services_OpenStreetMap_OpeningHours
*/
public function setValue($value)
{
$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 double $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($time = null)
{
if ($this->value === null) {
return null;
}
if ($this->value == '24/7') {
return true;
}
if ($time === null) {
$time = time();
}
if ($this->value == 'sunrise-sunset') {
$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 );
}
// other simple test would be sunrise-sunset - with
// offsets that would need to be taken into account
// time specs...
$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));
$open = $this->_openTimeSpec($portions, $time);
if ($open) {
$retval = true;
} elseif ($open === false) {
$retval = false;
}
}
return $retval;
}
/**
* Return true/false/null if a valid portion of an opening_hours value
* indicates whether a venue is open/closed or not incalculable.
*
* @param mixed $portions Part of an opening_hours specification
* @param mixed $time The time to calculate against.
*
* @return null|boolean
*/
private function _openTimeSpec($portions, $time)
{
if ($time === null) {
$time = time();
}
$day = strtolower(substr(date('D', $time), 0, 2));
$days = $this->_daySpecToArray($portions[0]);
if (is_array($days)) {
foreach ($days as $rday) {
if ($rday == $day) {
//day is a match
$time_spec = trim($portions[1]);
if (strtolower($time_spec) == 'off') {
return false;
}
if (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]);
$d = getdate($time);
$ctime = $d['hours'] * 60 + $d['minutes'];
return ($ctime >= $start && $ctime <= $end);
} elseif (strpos($time_spec, '-') && (strpos($time_spec, ','))) {
$times = explode(',', $time_spec);
$d = getdate($time);
$ctime = $d['hours'] * 60 + $d['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;
} elseif (preg_match('/^[0-2][0-9]:[0-5][0-9]\+$/', $time_spec)) {
// open-ended.
if ($this->_evaluateOpenEnded($time_spec) === false) {
return false;
}
}
}
}
} else {
// here we go again... need to refactor/decide a better algorithm.
$months = array('jan', 'feb', 'mar', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
if (in_array($portions[0], $months)) {
$month = strtolower(date('M', $time));
$time_spec = trim($portions[1]);
if ($portions[0] == $month && $time_spec == 'off') {
return false;
}
}
if ($portions[0] == '24/7') {
return true;
}
}
}
/**
* 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($day_specification)
{
$days = array('mo','tu', 'we','th','fr', 'sa', 'su');
$spec = trim(strtolower($day_specification));
if ($pos = strpos($spec, '-')) {
$start_day = substr($spec, 0, $pos);
$end_day = substr($spec, $pos + 1);
if ($start_day != 'mo') {
foreach ($days as $day) {
if ($day != $start_day) {
$off = array_shift($days);
} else {
break;
}
}
}
$rdays = array_reverse($days);
if ($end_day != 'su') {
foreach ($rdays as $day) {
if ($day != $end_day) {
$off = array_shift($rdays);
} else {
break;
}
}
$days = array_reverse($rdays);
}
return $days;
} elseif (strlen($spec) == 2) {
if (in_array($spec, $days)) {
return array($spec);
}
} elseif (strpos($spec, ',')) {
$delimited = explode(',', $spec);
$ret = array();
foreach ($delimited as $item) {
if (in_array($item, $days)) {
$ret[] = $item;
}
}
return $ret;
}
}
private function _evaluateOpenEnded($time_spec)
{
$start = $this->_startTime($time_spec);
$d = getdate($time);
$ctime = $d['hours'] * 60 + $d['minutes'];
if ($ctime < $start) {
return false;
}
}
private function _startTime($time_spec)
{
$starthour = substr($time_spec, 0, 2);
$startmin = substr($time_spec, 3, 2);
return $starthour * 60 + $startmin;
}
private function _endTime($time_spec)
{
$endhour = substr($time_spec, 0, 2);
$endmin = substr($time_spec, 3);
return $endhour * 60 + $endmin;
}
}
// vim:set et ts=4 sw=4:
?>