Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Clarify distinction between weekend and holidays and rename methods a…
Browse files Browse the repository at this point in the history
…ccordingly
  • Loading branch information
simbig authored Oct 12, 2021
1 parent 6285883 commit f961297
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v3.0.0

### Changed

- Clarify distinction between weekend and holidays and rename methods accordingly

## v2.1.0

### Changed
Expand Down
39 changes: 19 additions & 20 deletions src/BavarianHolidays.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use Carbon\Carbon;

/**
* Some definitions:
* Holiday: special occasions on a mostly fixed date where there is no work
* Weekend Day: Saturday and Sunday
* Business Day: any day that is neither a Holiday nor a Weekend Day.
*/
class BavarianHolidays
{
public const HOLIDAYS_STATIC = [
Expand Down Expand Up @@ -45,51 +51,44 @@ class BavarianHolidays
public static $loadUserDefinedHolidays;

/**
* Checks if given date is a working day.
* Checks if given date is a business day.
*/
public static function isWorkingDay(Carbon $date): bool
public static function isBusinessDay(Carbon $date): bool
{
return is_null(self::name($date));
return ! self::isHoliday($date)
&& ! $date->isWeekend();
}

/**
* Checks if given date is a holiday.
*/
public static function isHoliday(Carbon $date): bool
{
return ! self::isWorkingDay($date);
return is_string(self::nameHoliday($date));
}

/**
* Returns the name of the holiday if the date happens to land on one.
* Saturday and Sunday are not evaluated as holiday.
*/
public static function name(Carbon $date): ?string
public static function nameHoliday(Carbon $date): ?string
{
$holidayMap = self::buildHolidayMap($date);
$holiday = $holidayMap[self::dayOfTheYear($date)] ?? null;
if (is_string($holiday)) {
return $holiday;
}

if ($date->isSaturday()) {
return self::SAMSTAG;
}

if ($date->isSunday()) {
return self::SONNTAG;
}

return null;
return $holidayMap[self::dayOfTheYear($date)] ?? null;
}

public static function addWorkingDays(Carbon $date, int $days): Carbon
/**
* Returns a new carbon instance with the given number of business days added.
*/
public static function addBusinessDays(Carbon $date, int $days): Carbon
{
// Make sure we do not mutate the original date
$copy = $date->clone();

while ($days > 0) {
$copy->addDay();
if (self::isWorkingDay($copy)) {
if (self::isBusinessDay($copy)) {
$days--;
}
}
Expand Down
54 changes: 34 additions & 20 deletions tests/Unit/BavarianHolidaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,47 @@

final class BavarianHolidaysTest extends TestCase
{
public function testName(): void
public function testNameHoliday(): void
{
self::assertNull(BavarianHolidays::name(self::workingDayWednesday()));
self::assertSame(BavarianHolidays::SAMSTAG, BavarianHolidays::name(self::saturday()));
self::assertSame(BavarianHolidays::SONNTAG, BavarianHolidays::name(self::sunday()));
self::assertSame(BavarianHolidays::KARFREITAG, BavarianHolidays::name(self::karfreitag2019()));
self::assertSame(BavarianHolidays::OSTERSONNTAG, BavarianHolidays::name(self::easterSunday2019()));
self::assertNull(BavarianHolidays::nameHoliday(self::businessDayWednesday()));
self::assertSame(BavarianHolidays::KARFREITAG, BavarianHolidays::nameHoliday(self::karfreitag2019()));
self::assertSame(BavarianHolidays::OSTERSONNTAG, BavarianHolidays::nameHoliday(self::easterSunday2019()));
}

/**
* @dataProvider workingDays
* @dataProvider businessDays
*/
public function testWorkingDays(Carbon $workingDay): void
public function testBusinessDays(Carbon $businessDay): void
{
self::assertTrue(BavarianHolidays::isWorkingDay($workingDay));
self::assertFalse(BavarianHolidays::isHoliday($workingDay));
self::assertTrue(BavarianHolidays::isBusinessDay($businessDay));
self::assertFalse(BavarianHolidays::isHoliday($businessDay));
}

/**
* @dataProvider holidays
*/
public function testHolidays(Carbon $holiday): void
{
self::assertFalse(BavarianHolidays::isWorkingDay($holiday));
self::assertFalse(BavarianHolidays::isBusinessDay($holiday));
self::assertTrue(BavarianHolidays::isHoliday($holiday));
}

public function testAddMllWorkingDays(): void
/**
* @dataProvider weekend
*/
public function testWeekend(Carbon $weekend): void
{
self::assertFalse(BavarianHolidays::isBusinessDay($weekend));
self::assertFalse(BavarianHolidays::isHoliday($weekend));
}

public function testAddMllBusinessDays(): void
{
$saturday = self::saturday();
$mondayAfter = self::saturday()->addDays(2);

self::assertTrue(
BavarianHolidays::addWorkingDays($saturday, 1)
BavarianHolidays::addBusinessDays($saturday, 1)
->isSameDay($mondayAfter),
'Skips over sunday'
);
Expand All @@ -53,7 +60,7 @@ public function testAddMllWorkingDays(): void
);
}

protected static function workingDayWednesday(): Carbon
protected static function businessDayWednesday(): Carbon
{
return Carbon::createStrict(2019, 10, 30);
}
Expand Down Expand Up @@ -81,9 +88,9 @@ protected static function sunday(): Carbon
/**
* @return iterable<int, array{Carbon}>
*/
public static function workingDays(): iterable
public static function businessDays(): iterable
{
yield [self::workingDayWednesday()];
yield [self::businessDayWednesday()];
}

/**
Expand All @@ -93,6 +100,13 @@ public static function holidays(): iterable
{
yield [self::karfreitag2019()];
yield [self::easterSunday2019()];
}

/**
* @return iterable<int, array{Carbon}>
*/
public static function weekend(): iterable
{
yield [self::saturday()];
yield [self::sunday()];
}
Expand All @@ -101,9 +115,9 @@ public function testLoadUserDefinedHolidays(): void
{
$dayOfTheTentacle = Carbon::createStrict(2019, 8, 22);

self::assertNull(BavarianHolidays::name($dayOfTheTentacle));
self::assertNull(BavarianHolidays::nameHoliday($dayOfTheTentacle));
self::assertFalse(BavarianHolidays::isHoliday($dayOfTheTentacle));
self::assertTrue(BavarianHolidays::isWorkingDay($dayOfTheTentacle));
self::assertTrue(BavarianHolidays::isBusinessDay($dayOfTheTentacle));

$name = 'Day of the Tentacle';
BavarianHolidays::$loadUserDefinedHolidays = static function (int $year) use ($dayOfTheTentacle, $name): array {
Expand All @@ -115,8 +129,8 @@ public function testLoadUserDefinedHolidays(): void
}
};

self::assertSame($name, BavarianHolidays::name($dayOfTheTentacle));
self::assertSame($name, BavarianHolidays::nameHoliday($dayOfTheTentacle));
self::assertTrue(BavarianHolidays::isHoliday($dayOfTheTentacle));
self::assertFalse(BavarianHolidays::isWorkingDay($dayOfTheTentacle));
self::assertFalse(BavarianHolidays::isBusinessDay($dayOfTheTentacle));
}
}

0 comments on commit f961297

Please sign in to comment.