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..e318098bb 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 { diff --git a/src/Countries/Country.php b/src/Countries/Country.php new file mode 100644 index 000000000..e24261e9e --- /dev/null +++ b/src/Countries/Country.php @@ -0,0 +1,46 @@ +countryCode()) === $countryCode) { + return $country; + } + } + + return null; + } + + abstract public 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');