From a2a467e547ed4d20a9c1ee10e6347906c3e48dd5 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Fri, 12 Jan 2024 16:56:19 +0100 Subject: [PATCH 1/2] wip --- src/Actions/Executable.php | 11 ----- src/{Actions => Countries}/Belgium.php | 24 ++++------ src/Countries/Country.php | 47 +++++++++++++++++++ src/Enums/Country.php | 18 ------- ...{HolidaysException.php => InvalidYear.php} | 2 +- src/Exceptions/UnsupportedCountry.php | 13 +++++ src/Holidays.php | 23 +++++---- tests/HolidaysTest.php | 11 +++-- 8 files changed, 91 insertions(+), 58 deletions(-) delete mode 100644 src/Actions/Executable.php rename src/{Actions => Countries}/Belgium.php (76%) create mode 100644 src/Countries/Country.php delete mode 100644 src/Enums/Country.php rename src/Exceptions/{HolidaysException.php => InvalidYear.php} (87%) create mode 100644 src/Exceptions/UnsupportedCountry.php diff --git a/src/Actions/Executable.php b/src/Actions/Executable.php deleted file mode 100644 index f11ace884..000000000 --- a/src/Actions/Executable.php +++ /dev/null @@ -1,11 +0,0 @@ - */ - public function execute(int $year): array; -} diff --git a/src/Actions/Belgium.php b/src/Countries/Belgium.php similarity index 76% rename from src/Actions/Belgium.php rename to src/Countries/Belgium.php index d75928880..6cc4e86f6 100644 --- a/src/Actions/Belgium.php +++ b/src/Countries/Belgium.php @@ -1,13 +1,17 @@ ensureYearCanBeCalculated($year); @@ -17,17 +21,6 @@ public function execute(int $year): array return array_merge($fixedHolidays, $variableHolidays); } - protected function ensureYearCanBeCalculated(int $year): void - { - if ($year < 1970) { - throw HolidaysException::yearTooLow(); - } - - if ($year > 2037) { - throw HolidaysException::yearTooHigh(); - } - } - /** @return array */ protected function fixedHolidays(int $year): array { @@ -60,4 +53,5 @@ protected function variableHolidays(int $year): array 'Pinkstermaandag' => $easter->addDays(50), ]; } + } diff --git a/src/Countries/Country.php b/src/Countries/Country.php new file mode 100644 index 000000000..28dc44ff8 --- /dev/null +++ b/src/Countries/Country.php @@ -0,0 +1,47 @@ +countryCode()) === $countryCode) { + return $country; + } + } + + return null; + } + + abstract function countryCode(): string; + + abstract public function get(int $year): array; + + protected static function ensureYearCanBeCalculated(int $year): void + { + if ($year < 1970) { + throw InvalidYear::yearTooLow(); + } + + if ($year > 2037) { + throw InvalidYear::yearTooHigh(); + } + } +} diff --git a/src/Enums/Country.php b/src/Enums/Country.php deleted file mode 100644 index ae147f4bc..000000000 --- a/src/Enums/Country.php +++ /dev/null @@ -1,18 +0,0 @@ - new Belgium(), - }; - } -} diff --git a/src/Exceptions/HolidaysException.php b/src/Exceptions/InvalidYear.php similarity index 87% rename from src/Exceptions/HolidaysException.php rename to src/Exceptions/InvalidYear.php index 5fa4fb6c3..bea3dcbb1 100644 --- a/src/Exceptions/HolidaysException.php +++ b/src/Exceptions/InvalidYear.php @@ -4,7 +4,7 @@ use RuntimeException; -class HolidaysException extends RuntimeException +class InvalidYear extends RuntimeException { public static function yearTooLow(): self { diff --git a/src/Exceptions/UnsupportedCountry.php b/src/Exceptions/UnsupportedCountry.php new file mode 100644 index 000000000..79bcabb34 --- /dev/null +++ b/src/Exceptions/UnsupportedCountry.php @@ -0,0 +1,13 @@ +year = $year ?? CarbonImmutable::now()->year; - $this->country = $country ?? Country::Belgium; // @todo make configurable ? + $this->country = $country ?? Country::find('be'); } public static function new(): static @@ -37,12 +38,20 @@ public static function all(): array public function year(int $year): static { - return new static(year: $year); + return new static(year: $year, country: $this->country); } - public function country(string $countryCode): static + public function country(string $countryCode) { - return new static(country: Country::from($countryCode)); + $country = Country::find($countryCode); + + if (! $country) { + throw UnsupportedCountry::make($countryCode); + } + + $this->country = Country::find($countryCode); + + return $this; } /** @return array */ @@ -57,9 +66,7 @@ public function get(): array protected function calculate(): self { - $this->holidays = $this->country - ->action() - ->execute($this->year); + $this->holidays = $this->country->get($this->year); uasort($this->holidays, fn (CarbonImmutable $a, CarbonImmutable $b) => $a->timestamp <=> $b->timestamp diff --git a/tests/HolidaysTest.php b/tests/HolidaysTest.php index 1680c26da..1c4cabc0e 100644 --- a/tests/HolidaysTest.php +++ b/tests/HolidaysTest.php @@ -1,7 +1,8 @@ year(2023) + ->year(2024) ->country('BE') ->get(); @@ -39,12 +40,12 @@ it('cannot get all holidays of an unknown country code', function () { Holidays::new()->country('unknown')->get(); -})->throws(ValueError::class); +})->throws(UnsupportedCountry::class); it('cannot get holidays for years before 1970', function () { Holidays::new()->year(1969)->get(); -})->throws(HolidaysException::class, 'Holidays can only be calculated for years after 1970.'); +})->throws(InvalidYear::class, 'Holidays can only be calculated for years after 1970.'); it('cannot get holidays for years after 2037', function () { Holidays::new()->year(2038)->get(); -})->throws(HolidaysException::class, 'Holidays can only be calculated for years before 2038'); +})->throws(InvalidYear::class, 'Holidays can only be calculated for years before 2038'); From 54dbbf4a6d0265bc2c11e4b6ca42fb70ed0fc386 Mon Sep 17 00:00:00 2001 From: freekmurze Date: Fri, 12 Jan 2024 15:56:51 +0000 Subject: [PATCH 2/2] Fix styling --- src/Countries/Belgium.php | 1 - src/Countries/Country.php | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Countries/Belgium.php b/src/Countries/Belgium.php index 6cc4e86f6..e318098bb 100644 --- a/src/Countries/Belgium.php +++ b/src/Countries/Belgium.php @@ -53,5 +53,4 @@ protected function variableHolidays(int $year): array 'Pinkstermaandag' => $easter->addDays(50), ]; } - } diff --git a/src/Countries/Country.php b/src/Countries/Country.php index 28dc44ff8..e24261e9e 100644 --- a/src/Countries/Country.php +++ b/src/Countries/Country.php @@ -10,18 +10,17 @@ public static function find(string $countryCode): ?Country { $countryCode = strtolower($countryCode); - foreach (glob(__DIR__ . '/../Countries/*.php') as $filename) { + foreach (glob(__DIR__.'/../Countries/*.php') as $filename) { if (basename($filename) === 'Country.php') { continue; } // determine class name from file name - $countryClass = "\\Spatie\\Holidays\\Countries\\" . basename($filename, '.php'); + $countryClass = '\\Spatie\\Holidays\\Countries\\'.basename($filename, '.php'); /** @var \Spatie\Holidays\Countries\Country $country */ $country = new $countryClass; - if (strtolower($country->countryCode()) === $countryCode) { return $country; } @@ -30,7 +29,7 @@ public static function find(string $countryCode): ?Country return null; } - abstract function countryCode(): string; + abstract public function countryCode(): string; abstract public function get(int $year): array;