Skip to content

Commit

Permalink
fixes for Egypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Feb 7, 2024
1 parent 9df73f8 commit 3e9ccce
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
78 changes: 37 additions & 41 deletions src/Countries/Egypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use RuntimeException;
use Spatie\Holidays\Exceptions\InvalidYear;

class Egypt extends Country
{
Expand Down Expand Up @@ -229,28 +230,24 @@ public function countryCode(): string
return 'eg';
}

/**
* @return array<string, bool|CarbonImmutable>
*/
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<string, bool|CarbonImmutable>
* @return array<string, CarbonInterface>
*/
protected function variableHolidays(int $year): array
{
Expand All @@ -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<string, CarbonImmutable|false> An array of holiday dates.
* @return array<string, CarbonImmutable> An array of holiday dates.
*/
private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, string $holidayName, int $duration = 1): array
{
Expand All @@ -289,41 +286,44 @@ 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);
}
}

return $dates;
}

/**
* @return array<string, bool|CarbonImmutable>
* @return array<string, CarbonInterface>
*/
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(),
];

Expand All @@ -335,16 +335,12 @@ private function fixedHolidays(int $year): array
}

/**
* @return array<string, CarbonImmutable>
* @return array<string, CarbonInterface>
*/
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
Expand Down
8 changes: 4 additions & 4 deletions src/Exceptions/InvalidYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/AlbaniaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/AngolaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/ChileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 8 additions & 1 deletion tests/Countries/EgyptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
12 changes: 6 additions & 6 deletions tests/Countries/EnglandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions tests/Countries/GhanaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/JamaicaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/KenyaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/MoldovaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/MontenegroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Countries/NigeriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading

0 comments on commit 3e9ccce

Please sign in to comment.