Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

some refactoring

  • Loading branch information...
commit 42e397f1e8470ba2e5410ab67a8c64e34f5c02fe 1 parent d7d4b1e
@kenguest authored
Showing with 101 additions and 55 deletions.
  1. +78 −55 Services/OpenStreetMap/OpeningHours.php
  2. +23 −0 tests/OpeningHoursTest.php
View
133 Services/OpenStreetMap/OpeningHours.php
@@ -75,14 +75,8 @@ public function isOpen($time = null)
$time = time();
}
if ($this->value == 'sunrise-sunset') {
- $sunrise = date_sunrise($time);
- $sunset = date_sunset($time);
- $starthour = substr($sunrise, 0, 2);
- $startmin = substr($sunrise, 3);
- $start = $starthour * 60 + $startmin;
- $endhour = substr($sunset, 0, 2);
- $endmin = substr($sunset, 3);
- $end = $endhour * 60 + $endmin;
+ $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 );
@@ -98,11 +92,13 @@ public function isOpen($time = null)
$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 )) {
- $portions = explode(' ', $rule_sequence);
+ if (preg_match('/' . $day .'/', $rule_sequence)) {
+ // @fixme: brittle. use preg_replace with \w
+ $portions = explode(' ', str_replace(', ', ',', $rule_sequence));
return $this->_openTimeSpec($portions, $time);
}
- $portions = explode(' ', $rule_sequence);
+ // @fixme: brittle. use preg_replace with \w
+ $portions = explode(' ', str_replace(', ', ',', $rule_sequence));
$open = $this->_openTimeSpec($portions, $time);
if ($open) {
$retval = true;
@@ -127,57 +123,60 @@ private function _openTimeSpec($portions, $time)
if ($time === null) {
$time = time();
}
+
$day = strtolower(substr(date('D', $time), 0, 2));
$days = $this->_daySpecToArray($portions[0]);
- 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);
- $starthour = substr($startend_times[0], 0, 2);
- $startmin = substr($startend_times[0], 3);
- $start = $starthour * 60 + $startmin;
- $endhour = substr($startend_times[1], 0, 2);
- $endmin = substr($startend_times[1], 3);
- $end = $endhour * 60 + $endmin;
- $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));
- $starthour = substr($startend_times[0], 0, 2);
- $startmin = substr($startend_times[0], 3);
- $start = $starthour * 60 + $startmin;
- $endhour = substr($startend_times[1], 0, 2);
- $endmin = substr($startend_times[1], 3);
- $end = $endhour * 60 + $endmin;
- if ($ctime >= $start && $ctime <= $end) {
- return true;
- }
+ 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;
}
- return false;
- } elseif (preg_match('/^[0-2][0-9]:[0-5][0-9]\+$/', $time_spec)) {
- // open-ended.
- $starthour = substr($time_spec, 0, 2);
- $startmin = substr($time_spec, 3, 2);
- $start = $starthour * 60 + $startmin;
- $d = getdate($time);
- $ctime = $d['hours'] * 60 + $d['minutes'];
- if ($ctime < $start) {
+ 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;
+ }
}
}
@@ -232,6 +231,30 @@ private function _daySpecToArray($day_specification)
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:
View
23 tests/OpeningHoursTest.php
@@ -63,12 +63,23 @@ public function test() {
public function testOff() {
+ // Check precedence/priority
$oh = new Services_OpenStreetMap_OpeningHours();
$oh->setValue("Tu off; Mo-Sa 10:00-20:00");
$this->assertFalse($oh->isOpen(strtotime('last tuesday 12:00')));
$oh->setValue("Mo-Sa 10:00-20:00; Tu off");
$this->assertFalse($oh->isOpen(strtotime('last tuesday 12:00')));
+ }
+
+ public function testMonthOff() {
+ $oh = new Services_OpenStreetMap_OpeningHours();
+ $oh->setValue("24/7; Aug off");
+ $this->assertTrue($oh->isOpen(strtotime('October 22 2012 07:00')));
+ $this->assertFalse($oh->isOpen(strtotime('August 22 2012 07:00')));
+ $oh->setValue("24/7; Aug 10:00-14:00");
+# $this->assertTrue($oh->isOpen(strtotime('October 22 2012 07:00')));
+ # $this->assertTrue($oh->isOpen(strtotime('August 22 2012 13:00')));
/*
$oh->setValue("mo-fr 9:00-13:00, 14:00-17:30; sa 9:00-13:00");
@@ -77,7 +88,19 @@ public function testOff() {
$oh->setValue("");
*/
}
+ public function testMultipleTimesSpecifiedForDays() {
+ $oh = new Services_OpenStreetMap_OpeningHours();
+ $oh->setValue("mo-fr 9:00-13:00, 14:00-17:30; sa 9:00-13:00");
+ // Monday...
+ $this->assertFalse($oh->isOpen(strtotime('October 22 2012 07:00')));
+ $this->assertFalse($oh->isOpen(strtotime('October 22 2012 13:30')));
+ $this->assertTrue($oh->isOpen(strtotime('October 22 2012 10:30')));
+ $this->assertTrue($oh->isOpen(strtotime('October 22 2012 14:30')));
+ // Saturday...
+ $this->assertFalse($oh->isOpen(strtotime('October 27 2012 14:30')));
+ $this->assertTrue($oh->isOpen(strtotime('October 27 2012 11:30')));
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.