From b53ac331a3d8fcf05b057b7645a73947cbc1fd9e Mon Sep 17 00:00:00 2001 From: Ken Guest Date: Fri, 10 Jul 2020 23:48:53 +0100 Subject: [PATCH] Some refactoring --- Services/OpenStreetMap/OpeningHours.php | 138 ++++++++++++++---------- 1 file changed, 81 insertions(+), 57 deletions(-) diff --git a/Services/OpenStreetMap/OpeningHours.php b/Services/OpenStreetMap/OpeningHours.php index 8361090..5e7e9ac 100644 --- a/Services/OpenStreetMap/OpeningHours.php +++ b/Services/OpenStreetMap/OpeningHours.php @@ -162,74 +162,98 @@ private function _openTimeSpec($portions, $time): ?bool $time = time(); } - $day = strtolower(substr(date('D', $time), 0, 2)); $days = $this->_daySpecToArray(trim($portions[0], ":")); - $pattern = '/^[0-2][0-9]:[0-5][0-9]\+$/'; 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($pattern, $time_spec)) { - // open-ended. - if (!$this->_evaluateOpenEnded($time_spec)) { - return false; - } - } - } - } - } else { - // here we go again... need to refactor/decide a better algorithm. - $months = [ - 'jan', 'feb', 'mar', 'apr', 'may', 'jun', - 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' - ]; - if (in_array($portions[0], $months)) { - $month = strtolower(date('M', $time)); + return $this->_openTimeSpecDays($portions, $days, $time); + } + return $this->_openTimeSpecMonths($portions, $time); + } + + /** + * Work on time-spec with day portion in spec + * + * @param string $portions Part of an opening_hous 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) + { + $day = strtolower(substr(date('D', $time), 0, 2)); + $pattern = '/^[0-2][0-9]:[0-5][0-9]\+$/'; + foreach ($days as $rday) { + if ($rday === $day) { + //day is a match $time_spec = trim($portions[1]); - if ($portions[0] == $month && is_numeric($portions[1])) { - $startend_times = explode('-', $portions[2]); + 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]); - $atime = getdate($time); - $ctime = ($atime['hours'] * 60) + $atime['minutes']; + $date = getdate($time); + $ctime = $date['hours'] * 60 + $date['minutes']; return ($ctime >= $start && $ctime <= $end); - } elseif ($portions[0] === $month && $time_spec === 'off') { + } elseif (strpos($time_spec, '-') && (strpos($time_spec, ','))) { + $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; + } elseif (preg_match($pattern, $time_spec)) { + // open-ended. + if (!$this->_evaluateOpenEnded($time_spec)) { + return false; + } } } - if ($portions[0] === '24/7') { - return true; + } + } + + /** + * OpenTImeSpecMonths + * + * @param array $spec time-spec + * @param integer $time time to evaluate against + * + * @return void + */ + private function _openTImeSpecMonths($spec, $time) + { + $months = [ + 'jan', 'feb', 'mar', 'apr', 'may', 'jun', + 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' + ]; + 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']; + return ($ctime >= $start && $ctime <= $end); + } elseif ($spec[0] === $month && $time_spec === 'off') { + return false; } } - return null; + if ($spec[0] === '24/7') { + return true; + } } /**