Skip to content

Commit

Permalink
Merge branch 'newpope-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimír Kriška committed Jul 30, 2015
2 parents 26597cc + 9a6a147 commit 5e08882
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
matrix:
allow_failures:
- php: hhvm
- php: nightly

script:
- ./tests/run.sh
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
31 changes: 18 additions & 13 deletions src/Rekurzia/SlovakHolidays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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]);
}
}

Expand Down Expand Up @@ -134,4 +139,4 @@ public static function isTodayHoliday()

class SlovakHolidaysException extends \Exception
{
}
}
12 changes: 6 additions & 6 deletions tests/SlovakHolidays.test.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Assert::same(true, SlovakHolidays::isDayHoliday(2013, 04, 01));

0 comments on commit 5e08882

Please sign in to comment.