diff --git a/module/holiday/model.php b/module/holiday/model.php index 2bc88080a4..c822905018 100644 --- a/module/holiday/model.php +++ b/module/holiday/model.php @@ -410,26 +410,29 @@ class holidayModel extends model } /** + * API: 获取节假日。 * Get holidays by api. * * @param string $year * @access public * @return array */ - public function getHolidayByAPI($year = '') + public function getHolidayByAPI(string $year = ''): array { + /* Get holidays by api. */ if(empty($year)) $year = date('Y'); $apiRoot = sprintf($this->config->holiday->apiRoot, $year); $data = json_decode(common::http($apiRoot)); $days = isset($data->days) ? (array)$data->days : array(); + /* Build holiday data. */ $holidays = array(); $privDay = 0; $prevDayOff = true; $daysIndex = count($days) - 1; foreach($days as $index => $day) { - if($index < $daysIndex and (strtotime($day->date) - $privDay > 86400 or $day->isOffDay != $prevDayOff)) + if($index < $daysIndex && (strtotime($day->date) - $privDay > 86400 || $day->isOffDay != $prevDayOff)) { $holiday = new stdClass(); $holiday->type = $day->isOffDay ? 'holiday' : 'working'; @@ -439,7 +442,7 @@ class holidayModel extends model $holidays[] = $holiday; } - if(isset($holiday) or $index == $daysIndex) + if(isset($holiday) || $index == $daysIndex) { $holidayNum = count($holidays); if($holidayNum > 1) diff --git a/module/holiday/test/holiday.class.php b/module/holiday/test/holiday.class.php index 383296e560..60927274ed 100644 --- a/module/holiday/test/holiday.class.php +++ b/module/holiday/test/holiday.class.php @@ -357,4 +357,24 @@ class holidayTest return $task->realDuration; } + + /** + * 测试通过 API 获取节假日。 + * Test get holidays by api. + * + * @param string $year + * @access public + * @return int|array + */ + public function getHolidayByAPITest(string $year): int|array + { + if($year == 'this year') $year = date('Y'); + if($year == 'last year') $year = date('Y', strtotime('-1 year')); + if($year == 'next year') $year = date('Y', strtotime('+1 year')); + $objects = $this->objectModel->getHolidayByAPI($year); + + if(dao::isError()) return dao::getError(); + + return count($objects); + } } diff --git a/module/holiday/test/model/getholidaybyapi.php b/module/holiday/test/model/getholidaybyapi.php new file mode 100644 index 0000000000..66601f2c85 --- /dev/null +++ b/module/holiday/test/model/getholidaybyapi.php @@ -0,0 +1,26 @@ +#!/usr/bin/env php +gen(50); +zdTable('user')->gen(1); + +su('admin'); + +/** + +title=测试 holidayModel->getHolidayByAPI(); +cid=1 +pid=1 + +*/ + +$holiday = new holidayTest(); +$year = array('this year', 'last year', 'next year', ''); + +r($holiday->getHolidayByAPITest($year[0])) && p() && e('11'); //返回 本年 的节假日。 +r($holiday->getHolidayByAPITest($year[1])) && p() && e('12'); //返回 上年 的节假日。 +r($holiday->getHolidayByAPITest($year[2])) && p() && e('14'); //返回 下年 的节假日。 +r($holiday->getHolidayByAPITest($year[3])) && p() && e('11'); //返回 不设置年份 的节假日。