From f9612974c9eba72693709c414c17b5d9e4cdfe4a Mon Sep 17 00:00:00 2001 From: Simon Bigelmayr Date: Tue, 12 Oct 2021 14:22:08 +0200 Subject: [PATCH] Clarify distinction between weekend and holidays and rename methods accordingly --- CHANGELOG.md | 6 ++++ src/BavarianHolidays.php | 39 ++++++++++----------- tests/Unit/BavarianHolidaysTest.php | 54 ++++++++++++++++++----------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a08fdd0..5f1d65a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/BavarianHolidays.php b/src/BavarianHolidays.php index 410ecea..628ea3b 100644 --- a/src/BavarianHolidays.php +++ b/src/BavarianHolidays.php @@ -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 = [ @@ -45,11 +51,12 @@ 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(); } /** @@ -57,39 +64,31 @@ public static function isWorkingDay(Carbon $date): bool */ 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--; } } diff --git a/tests/Unit/BavarianHolidaysTest.php b/tests/Unit/BavarianHolidaysTest.php index 5c911a4..208704c 100644 --- a/tests/Unit/BavarianHolidaysTest.php +++ b/tests/Unit/BavarianHolidaysTest.php @@ -10,22 +10,20 @@ 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)); } /** @@ -33,17 +31,26 @@ public function testWorkingDays(Carbon $workingDay): void */ 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' ); @@ -53,7 +60,7 @@ public function testAddMllWorkingDays(): void ); } - protected static function workingDayWednesday(): Carbon + protected static function businessDayWednesday(): Carbon { return Carbon::createStrict(2019, 10, 30); } @@ -81,9 +88,9 @@ protected static function sunday(): Carbon /** * @return iterable */ - public static function workingDays(): iterable + public static function businessDays(): iterable { - yield [self::workingDayWednesday()]; + yield [self::businessDayWednesday()]; } /** @@ -93,6 +100,13 @@ public static function holidays(): iterable { yield [self::karfreitag2019()]; yield [self::easterSunday2019()]; + } + + /** + * @return iterable + */ + public static function weekend(): iterable + { yield [self::saturday()]; yield [self::sunday()]; } @@ -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 { @@ -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)); } }