diff --git a/src/Countries/Country.php b/src/Countries/Country.php index 538b89675..fa0352908 100644 --- a/src/Countries/Country.php +++ b/src/Countries/Country.php @@ -109,11 +109,11 @@ protected function ensureYearCanBeCalculated(int $year): void * https://www.php.net/manual/en/function.easter-date.php */ if ($year < 1970) { - throw InvalidYear::yearTooLow(); + throw InvalidYear::yearTooLow(1970); } if ($year > 2037) { - throw InvalidYear::yearTooHigh(); + throw InvalidYear::yearTooHigh(2038); } } } diff --git a/src/Countries/Egypt.php b/src/Countries/Egypt.php index 52ec78672..b220aca86 100644 --- a/src/Countries/Egypt.php +++ b/src/Countries/Egypt.php @@ -5,6 +5,7 @@ use Carbon\CarbonImmutable; use Carbon\CarbonInterface; use RuntimeException; +use Spatie\Holidays\Exceptions\InvalidYear; class Egypt extends Country { @@ -229,28 +230,24 @@ public function countryCode(): string return 'eg'; } - /** - * @return array - */ protected function allHolidays(int $year): array { $fixedHolidays = $this->fixedHolidays($year); $variableHolidays = $this->variableHolidays($year); return array_merge([ - // These are fixed, but seasonal holidays that aren't observed in Egypt - 'New Year\'s Day' => CarbonImmutable::create($year, 1, 1), - 'Flooding of the Nile' => CarbonImmutable::create($year, 8, 15), - 'March Equinox' => CarbonImmutable::create($year, 3, 20), - 'June Solstice' => CarbonImmutable::create($year, 6, 21), - 'September Equinox' => CarbonImmutable::create($year, 9, 22), - 'Nayrouz' => CarbonImmutable::create($year, 9, 11), - 'December Solstice' => CarbonImmutable::create($year, 12, 21), + 'New Year\'s Day' => '1-1', + 'Flooding of the Nile' => '8-15', + 'March Equinox' => '3-20', + 'June Solstice' => '6-21', + 'Nayrouz' => '9-11', + 'September Equinox' => '9-22', + 'December Solstice' => '12-21', ], $fixedHolidays, $variableHolidays); } /** - * @return array + * @return array */ protected function variableHolidays(int $year): array { @@ -277,7 +274,7 @@ protected function variableHolidays(int $year): array * @param int $year The year for which to prepare holiday dates. * @param string $holidayName The name of the holiday. * @param int $duration The duration of the holiday in days. - * @return array An array of holiday dates. + * @return array An array of holiday dates. */ private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, string $holidayName, int $duration = 1): array { @@ -289,22 +286,25 @@ private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, s * * @see https://www.timeanddate.com/holidays/egypt */ - if (isset($holidayDates[$year])) { - $startDay = CarbonImmutable::createFromFormat('Y-m-d', sprintf('%s-%s', $year, $holidayDates[$year])); - if (! $startDay instanceof CarbonImmutable) { - throw new RuntimeException('Date could not be created.'); - } + if ($year < 2005) { + throw InvalidYear::yearTooLow(2005); + } + + if (! isset($holidayDates[$year])) { + return $dates; + } + + $startDay = CarbonImmutable::createFromFormat('Y-m-d', sprintf('%s-%s', $year, $holidayDates[$year])); - if ($duration === 1) { - // For single-day holidays, use the holiday name without "Day" - $dates[$holidayName] = $startDay; - } else { - // For multi-day holidays, append "Day N" for the second day onwards - for ($i = 0; $i < $duration; $i++) { - $dayLabel = $i === 0 ? $holidayName : sprintf('%s Day %d', $holidayName, $i + 1); - $dates[$dayLabel] = $startDay->addDays($i); - } + if ($duration === 1) { + // For single-day holidays, use the holiday name without "Day" + $dates[$holidayName] = $startDay; + } else { + // For multi-day holidays, append "Day N" for the second day onwards + for ($i = 0; $i < $duration; $i++) { + $dayLabel = $i === 0 ? $holidayName : sprintf('%s Day %d', $holidayName, $i + 1); + $dates[$dayLabel] = $startDay->addDays($i); } } @@ -312,18 +312,18 @@ private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, s } /** - * @return array + * @return array */ private function fixedHolidays(int $year): array { $holidays = [ - 'Coptic Christmas Day' => CarbonImmutable::create($year, 1, 7), - 'Revolution Day 2011' => CarbonImmutable::create($year, 1, 25), - 'Sinai Liberation Day' => CarbonImmutable::create($year, 4, 25), - 'Labour Day' => CarbonImmutable::create($year, 5, 1), - 'June 30 Revolution Day' => CarbonImmutable::create($year, 6, 30), - 'Revolution Day' => CarbonImmutable::create($year, 7, 23), - 'Armed Forces Day' => CarbonImmutable::create($year, 10, 6), + 'Coptic Christmas Day' => CarbonImmutable::createFromDate($year, 1, 7), + 'Revolution Day 2011' => CarbonImmutable::createFromDate($year, 1, 25), + 'Sinai Liberation Day' => CarbonImmutable::createFromDate($year, 4, 25), + 'Labour Day' => CarbonImmutable::createFromDate($year, 5, 1), + 'June 30 Revolution Day' => CarbonImmutable::createFromDate($year, 6, 30), + 'Revolution Day' => CarbonImmutable::createFromDate($year, 7, 23), + 'Armed Forces Day' => CarbonImmutable::createFromDate($year, 10, 6), 'Spring Festival' => $this->orthodoxEaster($year)->addDay()->toImmutable(), ]; @@ -335,16 +335,12 @@ private function fixedHolidays(int $year): array } /** - * @return array + * @return array */ - private function adjustForWeekend(string $name, CarbonImmutable|false $date): array + private function adjustForWeekend(string $name, CarbonImmutable $date): array { $adjustedHolidays = []; - if (! $date instanceof CarbonImmutable) { - return []; - } - // Explicitly define this logic to avoid timezone confusion on the CarbonInterface::next() method if ($date->isFriday() || $date->isSaturday()) { // If the holiday falls on a weekend (Friday or Saturday), it is observed on the following Sunday diff --git a/src/Exceptions/InvalidYear.php b/src/Exceptions/InvalidYear.php index 0eac2b9e1..13fd335ad 100644 --- a/src/Exceptions/InvalidYear.php +++ b/src/Exceptions/InvalidYear.php @@ -6,14 +6,14 @@ class InvalidYear extends RuntimeException { - public static function yearTooLow(): self + public static function yearTooLow(int $year): self { - return new self('Holidays can only be calculated for years after 1970.'); + return new self("Holidays can only be calculated for years after {$year}."); } - public static function yearTooHigh(): self + public static function yearTooHigh(int $year): self { - return new self('Holidays can only be calculated for years before 2038.'); + return new self("Holidays can only be calculated for years before {$year}."); } public static function range(string $country, int $start, int $end): self diff --git a/tests/Countries/AlbaniaTest.php b/tests/Countries/AlbaniaTest.php index 1bfb5ba1b..6099e1a77 100644 --- a/tests/Countries/AlbaniaTest.php +++ b/tests/Countries/AlbaniaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate albanian holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'al')->get(); expect($holidays) diff --git a/tests/Countries/AngolaTest.php b/tests/Countries/AngolaTest.php index ae0e07518..6021f721d 100644 --- a/tests/Countries/AngolaTest.php +++ b/tests/Countries/AngolaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate angola holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'ao')->get(); diff --git a/tests/Countries/ChileTest.php b/tests/Countries/ChileTest.php index aed6f880f..daf50b0e9 100644 --- a/tests/Countries/ChileTest.php +++ b/tests/Countries/ChileTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate chile holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'cl')->get(); diff --git a/tests/Countries/EgyptTest.php b/tests/Countries/EgyptTest.php index 842b8b4ef..6579f79c5 100644 --- a/tests/Countries/EgyptTest.php +++ b/tests/Countries/EgyptTest.php @@ -3,10 +3,11 @@ namespace Spatie\Holidays\Tests\Countries; use Carbon\CarbonImmutable; +use Spatie\Holidays\Exceptions\InvalidYear; use Spatie\Holidays\Holidays; it('can calculate egyptian holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01', 'Africa/Cairo'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'eg')->get(); @@ -16,3 +17,9 @@ expect(formatDates($holidays))->toMatchSnapshot(); }); + +it('cannot calculate egyptian holidays before 2005', function () { + CarbonImmutable::setTestNow('2024-01-01'); + + Holidays::for(country: 'eg', year: 2004)->get(); +})->throws(InvalidYear::class); diff --git a/tests/Countries/EnglandTest.php b/tests/Countries/EnglandTest.php index ee3d22e80..0746c50a3 100644 --- a/tests/Countries/EnglandTest.php +++ b/tests/Countries/EnglandTest.php @@ -40,7 +40,7 @@ ]); it('can calculate welsh holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); @@ -50,7 +50,7 @@ }); it('returns a substitute day if new years day falls on a weekend', function () { - CarbonImmutable::setTestNowAndTimezone('2033-01-01'); + CarbonImmutable::setTestNow('2033-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); @@ -59,7 +59,7 @@ }); it('can calculate welsh holidays if christmas is on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); @@ -68,7 +68,7 @@ }); it('can calculate welsh holidays if christmas is on a saturday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); @@ -77,7 +77,7 @@ }); it('can calculate welsh holidays if christmas is on a sunday', function () { - CarbonImmutable::setTestNowAndTimezone('2022-01-01'); + CarbonImmutable::setTestNow('2022-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); @@ -86,7 +86,7 @@ }); it('can calculate holidays for 2020', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); diff --git a/tests/Countries/GhanaTest.php b/tests/Countries/GhanaTest.php index 3a02ed9d3..f8186e756 100644 --- a/tests/Countries/GhanaTest.php +++ b/tests/Countries/GhanaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate Ghana holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'gh')->get(); @@ -18,7 +18,7 @@ }); it('can calculate Ghana easter based region holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'gh')->get(); @@ -30,7 +30,7 @@ }); it('can calculate Ghana date based regional holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'gh')->get(); diff --git a/tests/Countries/JamaicaTest.php b/tests/Countries/JamaicaTest.php index 1ea7b0237..5b06881f5 100644 --- a/tests/Countries/JamaicaTest.php +++ b/tests/Countries/JamaicaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate jamaican holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'jm')->get(); diff --git a/tests/Countries/KenyaTest.php b/tests/Countries/KenyaTest.php index 96d17cdf3..c5aef294d 100644 --- a/tests/Countries/KenyaTest.php +++ b/tests/Countries/KenyaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate kenyan holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'ke')->get(); diff --git a/tests/Countries/MoldovaTest.php b/tests/Countries/MoldovaTest.php index 02ac0db64..86fafa45e 100644 --- a/tests/Countries/MoldovaTest.php +++ b/tests/Countries/MoldovaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate moldavian holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'md')->get(); diff --git a/tests/Countries/MontenegroTest.php b/tests/Countries/MontenegroTest.php index 9f5078b93..e5450ba3f 100644 --- a/tests/Countries/MontenegroTest.php +++ b/tests/Countries/MontenegroTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate montenegro holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'me')->get(); diff --git a/tests/Countries/NigeriaTest.php b/tests/Countries/NigeriaTest.php index 8fdb2a32a..d0559a305 100644 --- a/tests/Countries/NigeriaTest.php +++ b/tests/Countries/NigeriaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate nigerian holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'ng')->get(); diff --git a/tests/Countries/NorthernIrelandTest.php b/tests/Countries/NorthernIrelandTest.php index 11266d3af..1f5dc237c 100644 --- a/tests/Countries/NorthernIrelandTest.php +++ b/tests/Countries/NorthernIrelandTest.php @@ -47,7 +47,7 @@ ]); it('can calculate northern irish holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2025-01-01'); + CarbonImmutable::setTestNow('2025-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -57,7 +57,7 @@ }); it('returns a substitute day if new years day falls on a weekend', function () { - CarbonImmutable::setTestNowAndTimezone('2033-01-01'); + CarbonImmutable::setTestNow('2033-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -66,7 +66,7 @@ }); it('returns a substitute day for second of january if new years day falls on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -75,7 +75,7 @@ }); it('can calculate northern irish holidays if christmas is on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -84,7 +84,7 @@ }); it('can calculate northern irish holidays if christmas is on a saturday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -93,7 +93,7 @@ }); it('can calculate northern irish holidays if christmas is on a sunday', function () { - CarbonImmutable::setTestNowAndTimezone('2022-01-01'); + CarbonImmutable::setTestNow('2022-01-01'); $holidays = Holidays::for(country: 'gb-nir')->get(); @@ -102,7 +102,7 @@ }); it('can calculate northern irish for 2020', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); diff --git a/tests/Countries/ScotlandTest.php b/tests/Countries/ScotlandTest.php index f8aa5d926..dc2d9aaad 100644 --- a/tests/Countries/ScotlandTest.php +++ b/tests/Countries/ScotlandTest.php @@ -43,7 +43,7 @@ ]); it('can calculate scottish holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2025-01-01'); + CarbonImmutable::setTestNow('2025-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -53,7 +53,7 @@ }); it('returns a substitute day if new years day falls on a weekend', function () { - CarbonImmutable::setTestNowAndTimezone('2033-01-01'); + CarbonImmutable::setTestNow('2033-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -62,7 +62,7 @@ }); it('returns a substitute day for second of january if new years day falls on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -71,7 +71,7 @@ }); it('can calculate scottish holidays if christmas is on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -80,7 +80,7 @@ }); it('can calculate scottish holidays if christmas is on a saturday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -89,7 +89,7 @@ }); it('can calculate scottish holidays if christmas is on a sunday', function () { - CarbonImmutable::setTestNowAndTimezone('2022-01-01'); + CarbonImmutable::setTestNow('2022-01-01'); $holidays = Holidays::for(country: 'gb-sct')->get(); @@ -98,7 +98,7 @@ }); it('can calculate holidays for 2020', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-eng')->get(); diff --git a/tests/Countries/SloveniaTest.php b/tests/Countries/SloveniaTest.php index 0dc846a6e..da8a6e011 100644 --- a/tests/Countries/SloveniaTest.php +++ b/tests/Countries/SloveniaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate slovenian holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'si')->get(); diff --git a/tests/Countries/SwitzerlandTest.php b/tests/Countries/SwitzerlandTest.php index 0e4a97f41..386019210 100644 --- a/tests/Countries/SwitzerlandTest.php +++ b/tests/Countries/SwitzerlandTest.php @@ -8,7 +8,7 @@ use Spatie\Holidays\Holidays; it('can calculate swiss holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'ch')->get(); @@ -20,7 +20,7 @@ }); it('can get swiss holidays for a specified region (zh)', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $switzerland = new Switzerland(region: 'ch-zh'); diff --git a/tests/Countries/TaiwanTest.php b/tests/Countries/TaiwanTest.php index 75c7b6906..9aa6578bc 100644 --- a/tests/Countries/TaiwanTest.php +++ b/tests/Countries/TaiwanTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate taiwan holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'tw')->get(); diff --git a/tests/Countries/TanzaniaTest.php b/tests/Countries/TanzaniaTest.php index 69777866b..38f15ec39 100644 --- a/tests/Countries/TanzaniaTest.php +++ b/tests/Countries/TanzaniaTest.php @@ -6,7 +6,7 @@ use Spatie\Holidays\Holidays; it('can calculate tanzania holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'tz')->get(); diff --git a/tests/Countries/WalesTest.php b/tests/Countries/WalesTest.php index 2e840357b..01d65370e 100644 --- a/tests/Countries/WalesTest.php +++ b/tests/Countries/WalesTest.php @@ -58,7 +58,7 @@ ]); it('can calculate welsh holidays', function () { - CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + CarbonImmutable::setTestNow('2024-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get(); @@ -68,7 +68,7 @@ }); it('returns a substitute day if new years day falls on a weekend', function () { - CarbonImmutable::setTestNowAndTimezone('2033-01-01'); + CarbonImmutable::setTestNow('2033-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get(); @@ -77,7 +77,7 @@ }); it('can calculate welsh holidays if christmas is on a friday', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get(); @@ -86,7 +86,7 @@ }); it('can calculate welsh holidays if christmas is on a saturday', function () { - CarbonImmutable::setTestNowAndTimezone('2021-01-01'); + CarbonImmutable::setTestNow('2021-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get(); @@ -95,7 +95,7 @@ }); it('can calculate welsh holidays if christmas is on a sunday', function () { - CarbonImmutable::setTestNowAndTimezone('2022-01-01'); + CarbonImmutable::setTestNow('2022-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get(); @@ -104,7 +104,7 @@ }); it('can calculate holidays for 2020', function () { - CarbonImmutable::setTestNowAndTimezone('2020-01-01'); + CarbonImmutable::setTestNow('2020-01-01'); $holidays = Holidays::for(country: 'gb-cym')->get();