From 0b2618f7877c54cb4377415a2a4b70024169420c Mon Sep 17 00:00:00 2001 From: Adam Stipak Date: Wed, 29 Jul 2015 10:37:30 +0200 Subject: [PATCH 1/3] Smartest API for method getHolidays(). --- src/Rekurzia/SlovakHolidays.php | 31 ++++++++++++++++++------------- tests/SlovakHolidays.test.phpt | 12 ++++++------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Rekurzia/SlovakHolidays.php b/src/Rekurzia/SlovakHolidays.php index 0d3cfdf..89d6c11 100644 --- a/src/Rekurzia/SlovakHolidays.php +++ b/src/Rekurzia/SlovakHolidays.php @@ -49,12 +49,13 @@ public function __construct() /** * Gets holidays for specified year - * @param int + * @param int $year + * @param int $month * @return array */ - public static function getHolidays($year = null) + public static function getHolidays($year = null, $month = null) { - $year = ($year === null ? date('Y') : $year); + $year = $year ?: date('Y'); $easterSunday = (new \DateTime)->setTimestamp(EasterDate::get($year)); $holidays = [ @@ -68,27 +69,31 @@ public static function getHolidays($year = null) ksort($holidays); - return $holidays; + if ($month !== null) { + return self::getHolidaysForYearAndMonth($holidays, $year, $month); + } + else { + return $holidays; + } } /** * Gets holiday for specified year and month - * @param int - * @param int + * @param array $holidays + * @param int $year + * @param int $month * @return array * @throws SlovakHolidaysException */ - public static function getHolidaysForYearAndMonth($year, $month) + private static function getHolidaysForYearAndMonth(array $holidays, $year, $month) { if (!checkdate($month, 1, $year)) { throw new SlovakHolidaysException('Invalid input year or month'); } - $holidays = []; - - foreach (self::getHolidays($year) as $key => $holiday) { - if (substr($key, 0, 7) === sprintf("%4d-%02d", $year, $month)) { - $holidays[$key] = $holiday; + foreach ($holidays as $key => $holiday) { + if (substr($key, 0, 7) !== sprintf("%4d-%02d", $year, $month)) { + unset($holidays[$key]); } } @@ -134,4 +139,4 @@ public static function isTodayHoliday() class SlovakHolidaysException extends \Exception { -} \ No newline at end of file +} diff --git a/tests/SlovakHolidays.test.phpt b/tests/SlovakHolidays.test.phpt index cf3d23b..7d76772 100644 --- a/tests/SlovakHolidays.test.phpt +++ b/tests/SlovakHolidays.test.phpt @@ -10,7 +10,7 @@ Assert::exception(function() { }, 'Rekurzia\SlovakHolidaysException', 'Class cannot be instantiated'); Assert::exception(function() { - SlovakHolidays::getHolidaysForYearAndMonth(2015, 13); + SlovakHolidays::getHolidays(2015, 13); }, 'Rekurzia\SlovakHolidaysException', 'Invalid input year or month'); Assert::exception(function() { @@ -38,7 +38,7 @@ Assert::same([ Assert::same([ '2015-04-03', '2015-04-06', -], array_keys(SlovakHolidays::getHolidaysForYearAndMonth(2015, 4))); +], array_keys(SlovakHolidays::getHolidays(2015, 4))); Assert::same(false, SlovakHolidays::isDayHoliday(2015, 01, 02)); Assert::same(false, SlovakHolidays::isDayHoliday(2015, 12, 23)); @@ -67,7 +67,7 @@ Assert::same([ Assert::same([ '2014-04-18', '2014-04-21', -], array_keys(SlovakHolidays::getHolidaysForYearAndMonth(2014, 4))); +], array_keys(SlovakHolidays::getHolidays(2014, 4))); Assert::same(false, SlovakHolidays::isDayHoliday(2014, 03, 30)); Assert::same(false, SlovakHolidays::isDayHoliday(2014, 10, 02)); @@ -95,14 +95,14 @@ Assert::same([ Assert::same([ '2013-03-29', -], array_keys(SlovakHolidays::getHolidaysForYearAndMonth(2013, 3))); +], array_keys(SlovakHolidays::getHolidays(2013, 3))); Assert::same([ '2013-04-01', -], array_keys(SlovakHolidays::getHolidaysForYearAndMonth(2013, 4))); +], array_keys(SlovakHolidays::getHolidays(2013, 4))); Assert::same(false, SlovakHolidays::isDayHoliday(2013, 02, 28)); Assert::same(false, SlovakHolidays::isDayHoliday(2013, 07, 06)); Assert::same(true, SlovakHolidays::isDayHoliday(2013, 03, 29)); -Assert::same(true, SlovakHolidays::isDayHoliday(2013, 04, 01)); \ No newline at end of file +Assert::same(true, SlovakHolidays::isDayHoliday(2013, 04, 01)); From 597922a00a5791ffb1bc3ff5a96643b0adf52ae8 Mon Sep 17 00:00:00 2001 From: Adam Stipak Date: Wed, 29 Jul 2015 10:42:29 +0200 Subject: [PATCH 2/3] Update README. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5fa6f28..5ac7f57 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ Very easy, static: ```php \Rekurzia\SlovakHolidays::getHolidays(); // for current year -\Rekurzia\SlovakHolidays::getHolidays(2014); -\Rekurzia\SlovakHolidays::getHolidaysForYearAndMonth(2014, 1); +\Rekurzia\SlovakHolidays::getHolidays(2014); // only for year +\Rekurzia\SlovakHolidays::getHolidays(2014, 8); // for year and month \Rekurzia\SlovakHolidays::isTodayHoliday(); // date('Y-m-d') \Rekurzia\SlovakHolidays::isDayHoliday(2015, 12, 24); From 9a6a147c2fe21fa4f979c55ba6187f3f88c5a41f Mon Sep 17 00:00:00 2001 From: Adam Stipak Date: Wed, 29 Jul 2015 10:44:59 +0200 Subject: [PATCH 3/3] Allow failure for nightly. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f0f338c..122d774 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ php: matrix: allow_failures: - php: hhvm + - php: nightly script: - ./tests/run.sh