From f3ba3736c38f8f7de2259ca07d6aeba568c48f3c Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Wed, 17 Jan 2024 21:06:34 +0100 Subject: [PATCH] add support for regions for countries --- phpstan-baseline.neon | 10 ------- src/Countries/Austria.php | 52 ++++++++++++++++++++++++++++++++--- src/Countries/Belgium.php | 1 - src/Countries/Brazil.php | 1 - src/Countries/Country.php | 8 ++++-- src/Countries/Hungary.php | 1 - src/Countries/Netherlands.php | 1 - tests/HolidaysTest.php | 12 +++++++- 8 files changed, 65 insertions(+), 21 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 40f94f7ef..692dc4d91 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Belgium\\:\\:allHolidays\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/Countries/Belgium.php - - message: "#^Argument of an invalid type array\\\\|false supplied for foreach, only iterables are supported\\.$#" count: 1 @@ -20,11 +15,6 @@ parameters: count: 1 path: src/Countries/Country.php - - - message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Hungary\\:\\:allHolidays\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/Countries/Hungary.php - - message: "#^Cannot call method isSunday\\(\\) on Carbon\\\\CarbonImmutable\\|false\\.$#" count: 1 diff --git a/src/Countries/Austria.php b/src/Countries/Austria.php index c8eea5d62..50fb7ea95 100644 --- a/src/Countries/Austria.php +++ b/src/Countries/Austria.php @@ -11,10 +11,19 @@ public function countryCode(): string return 'at'; } - /** @return array */ protected function allHolidays(int $year): array { - return array_merge([ + return array_merge( + $this->regionalHolidays(), + $this->fixedHolidays(), + $this->variableHolidays($year), + ); + } + + /** @return array */ + protected function fixedHolidays(): array + { + return [ 'Neujahr' => '01-01', 'Heilige Drei Könige' => '01-06', 'Staatsfeiertag' => '05-01', @@ -24,7 +33,7 @@ protected function allHolidays(int $year): array 'Mariä Empfängnis' => '12-08', 'Christtag' => '12-25', 'Stefanitag' => '12-26', - ], $this->variableHolidays($year)); + ]; } /** @return array */ @@ -34,10 +43,45 @@ protected function variableHolidays(int $year): array ->setTimezone('Europe/Vienna'); return [ - 'Ostermontag' => $easter->addDay(1), + 'Ostermontag' => $easter->addDay(), 'Christi Himmelfahrt' => $easter->addDays(39), 'Pfingstmontag' => $easter->addDays(50), 'Fronleichnam' => $easter->addDays(60), ]; } + + /** @return array */ + protected function regionalHolidays(): array + { + return match ($this->region) { + 'bg' => [ + 'Martinitag' => '11-11', + ], + 'ka' => [ + 'Kärntner Landtag' => '10-10', + ], + 'no' => [ + 'Niederösterreichischer Leopolditag' => '11-15', + ], + 'oo' => [ + 'Oberösterreichischer Landesfeiertag' => '11-15', + ], + 'sb' => [ + 'Rupertitag' => '09-24', + ], + 'st' => [ + 'Tag der Steiermark' => '10-26', + ], + 'ti' => [ + 'Tiroler Landesfeiertag' => '04-26', + ], + 'vo' => [ + 'Vorarlberger Landtag' => '11-09', + ], + 'wi' => [ + 'Wiener Landtag' => '01-21', + ], + default => [], + }; + } } diff --git a/src/Countries/Belgium.php b/src/Countries/Belgium.php index f5d268db5..af52b6810 100644 --- a/src/Countries/Belgium.php +++ b/src/Countries/Belgium.php @@ -11,7 +11,6 @@ public function countryCode(): string return 'be'; } - /** @return array */ protected function allHolidays(int $year): array { return array_merge([ diff --git a/src/Countries/Brazil.php b/src/Countries/Brazil.php index 4c93f100b..479d78360 100644 --- a/src/Countries/Brazil.php +++ b/src/Countries/Brazil.php @@ -11,7 +11,6 @@ public function countryCode(): string return 'br'; } - /** @return array */ protected function allHolidays(int $year): array { return array_merge([ diff --git a/src/Countries/Country.php b/src/Countries/Country.php index b99262a5c..60934c9b0 100644 --- a/src/Countries/Country.php +++ b/src/Countries/Country.php @@ -8,6 +8,10 @@ abstract class Country { + protected function __construct(protected ?string $region = null) + { + } + abstract public function countryCode(): string; /** @return array */ @@ -35,9 +39,9 @@ public function get(int $year): array return $allHolidays; } - public static function make(): static + public static function make(?string $region = null): static { - return new static(); + return new static(region: $region ? strtolower($region) : null); } public static function find(string $countryCode): ?Country diff --git a/src/Countries/Hungary.php b/src/Countries/Hungary.php index 8a6467455..15a46e997 100644 --- a/src/Countries/Hungary.php +++ b/src/Countries/Hungary.php @@ -11,7 +11,6 @@ public function countryCode(): string return 'hu'; } - /** @return array */ protected function allHolidays(int $year): array { return array_merge([ diff --git a/src/Countries/Netherlands.php b/src/Countries/Netherlands.php index c79223dd0..427c670d9 100644 --- a/src/Countries/Netherlands.php +++ b/src/Countries/Netherlands.php @@ -11,7 +11,6 @@ public function countryCode(): string return 'nl'; } - /** @return array */ protected function allHolidays(int $year): array { return array_merge([ diff --git a/tests/HolidaysTest.php b/tests/HolidaysTest.php index 2477e6053..1901aa71d 100644 --- a/tests/HolidaysTest.php +++ b/tests/HolidaysTest.php @@ -1,6 +1,7 @@ get(); + + expect($holidays)->toContainElement(function (array $holidayProperties) { + return $holidayProperties['name'] === 'Martinitag' + && $holidayProperties['date']->format('Y-m-d') === '2024-11-11'; + }); +}); + it('cannot get all holidays of an unknown country code', function () { Holidays::for(country: 'unknown'); })->throws(UnsupportedCountry::class); @@ -68,7 +78,7 @@ expect($result)->toBeFalse(); }); -it('can see if a name is a holiday', function () { +it('can see if a string date is a holiday', function () { $result = Holidays::for('be')->isHoliday('2024-01-01'); expect($result)->toBeTrue();