Skip to content

Commit

Permalink
update Egypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Mar 22, 2024
1 parent d77fd5a commit f45c884
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 79 deletions.
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@ parameters:
count: 1
path: src/Countries/Country.php

-
message: "#^Access to undefined constant Spatie\\\\Holidays\\\\Countries\\\\Turkey\\:\\:arafat\\.$#"
count: 1
path: src/Countries/Turkey.php

-
message: "#^Access to undefined constant Spatie\\\\Holidays\\\\Countries\\\\Turkey\\:\\:ashura\\.$#"
count: 1
path: src/Countries/Turkey.php

-
message: "#^Access to undefined constant Spatie\\\\Holidays\\\\Countries\\\\Turkey\\:\\:islamicNewYear\\.$#"
count: 1
path: src/Countries/Turkey.php

-
message: "#^Access to undefined constant Spatie\\\\Holidays\\\\Countries\\\\Turkey\\:\\:prophetMuhammadBirthday\\.$#"
count: 1
path: src/Countries/Turkey.php

-
message: "#^Cannot call method setTimeStamp\\(\\) on DateTime\\|false\\.$#"
count: 1
Expand Down
42 changes: 38 additions & 4 deletions src/Calendars/IslamicCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Carbon\CarbonPeriod;
use Carbon\Exceptions\InvalidFormatException;
use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Exceptions\InvalidYear;

Expand All @@ -13,26 +14,59 @@ trait IslamicCalendar
/** @return array<CarbonPeriod> */
public function eidAlFitr(int $year, int $totalDays = 3): array
{
return $this->getHoliday(self::eidAlFitr, $year, $totalDays);
return $this->getMultiDayHoliday(self::eidAlFitr, $year, $totalDays);
}

/** @return array<CarbonPeriod> */
public function eidAlAdha(int $year, int $totalDays = 4): array
{
return $this->getHoliday(self::eidAlAdha, $year, $totalDays);
return $this->getMultiDayHoliday(self::eidAlAdha, $year, $totalDays);
}

/** @return array<CarbonPeriod> */
protected function ashura(int $year, int $totalDays = 2): array
{
return $this->getHoliday(self::ashura, $year, $totalDays);
return $this->getMultiDayHoliday(self::ashura, $year, $totalDays);
}

protected function arafat(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::arafat, $year);
}

protected function islamicNewYear(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::islamicNewYear, $year);
}

protected function prophetMuhammadBirthday(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::prophetMuhammadBirthday, $year);
}

/** @param array<string> $collection */
protected function getSingleDayHoliday(array $collection, int $year): CarbonImmutable
{
$date = $collection[$year] ?? null;

if ($date === null) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

$date = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();

if ($date === null) {
throw new InvalidFormatException("Invalid date for holiday");
}

return $date;
}

/**
* @param array<string|array<string>> $collection
* @return array<CarbonPeriod>
*/
protected function getHoliday(array $collection, int $year, int $totalDays): array
protected function getMultiDayHoliday(array $collection, int $year, int $totalDays): array
{
$date = $collection[$year] ?? null;

Expand Down
8 changes: 4 additions & 4 deletions src/Countries/Bahrain.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Bahrain extends Country implements HasTranslations, Islamic
2037 => '11-09',
];

protected const arafatDay = [
protected const arafat = [
2020 => '07-30',
2021 => '07-19',
2022 => '07-09',
Expand Down Expand Up @@ -167,9 +167,9 @@ public function islamicHolidays(int $year): array
$ashura = $this->ashura($year);

$holidays = [
'Arafat Day' => self::arafatDay[$year],
'Islamic New Year' => self::islamicNewYear[$year],
'Birthday of the Prophet Muhammad' => self::prophetMuhammadBirthday[$year],
'Arafat Day' => $this->arafat($year),
'Islamic New Year' => $this->islamicNewYear($year),
'Birthday of the Prophet Muhammad' => $this->prophetMuhammadBirthday($year),
];

return array_merge($holidays,
Expand Down
107 changes: 36 additions & 71 deletions src/Countries/Egypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ public function defaultLocale(): string

protected function allHolidays(int $year): array
{
$fixedHolidays = $this->fixedHolidays($year);
$variableHolidays = $this->variableHolidays($year);

return array_merge([
'New Year\'s Day' => '1-1',
'Flooding of the Nile' => '8-15',
Expand All @@ -255,75 +252,23 @@ protected function allHolidays(int $year): array
'Nayrouz' => '9-11',
'September Equinox' => '9-22',
'December Solstice' => '12-21',
], $fixedHolidays, $variableHolidays);
],
$this->fixedHolidays($year),
$this->variableHolidays($year),
$this->islamicHolidays($year),
);
}

/**
* @return array<string, CarbonInterface>
*/
/** @return array<string, CarbonInterface> */
protected function variableHolidays(int $year): array
{
$orthodoxEaster = $this->orthodoxEaster($year);

$eidAlFitrDates = $this->getIslamicHolidayDatesForYear(self::eidAlFitr, $year, 'Eid al-Fitr', 3);
$eidAlAdhaDates = $this->getIslamicHolidayDatesForYear(self::eidAlAdha, $year, 'Eid al-Adha', 4);
$arafatDayDates = $this->getIslamicHolidayDatesForYear(self::arafat, $year, 'Arafat Day');
$islamicNewYearDates = $this->getIslamicHolidayDatesForYear(self::islamicNewYear, $year, 'Islamic New Year');
$ashuraDates = $this->getIslamicHolidayDatesForYear(self::ashura, $year, 'Ashura');
$prophetMuhammadBirthdayDates = $this->getIslamicHolidayDatesForYear(self::prophetMuhammadBirthday, $year, 'Birthday of the Prophet Muhammad');

return array_merge([
return [
'Coptic Good Friday' => $orthodoxEaster->subDays(2)->toImmutable(),
'Coptic Holy Saturday' => $orthodoxEaster->subDays()->toImmutable(),
'Coptic Easter Sunday' => $orthodoxEaster->toImmutable(),
], $eidAlFitrDates, $eidAlAdhaDates, $arafatDayDates, $islamicNewYearDates, $ashuraDates, $prophetMuhammadBirthdayDates);
}

/**
* Prepare holiday dates for the given year.
*
* @param array<int, string> $holidayDates Array mapping years to dates.
* @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> An array of holiday dates.
*/
private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, string $holidayName, int $duration = 1): array
{
$dates = [];

/**
* No reliable sources exist for Islamic holidays observed in Egypt prior to 2005.
* So we'll only calculate holidays from 2005 onwards.
*
* @see https://www.timeanddate.com/holidays/egypt
*/
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 ($startDay === null) {
throw new InvalidFormatException("Invalid date format for holiday: {$holidayName}");
}

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;
];
}

/**
Expand All @@ -349,9 +294,34 @@ private function fixedHolidays(int $year): array
return $holidays;
}

/**
* @return array<string, CarbonInterface>
*/
public function islamicHolidays(int $year): array
{
/**
* No reliable sources exist for Islamic holidays observed in Egypt prior to 2005.
* So we'll only calculate holidays from 2005 onwards.
* @see https://www.timeanddate.com/holidays/egypt
*/
if ($year < 2005) {
throw InvalidYear::yearTooLow(2005);
}

$eidAlFitr = $this->eidAlFitr($year);
$eidAlAdha = $this->eidAlAdha($year);
$ashura = $this->ashura($year, 1);

return array_merge(
[
'Arafat Day' => $this->arafat($year),
'Islamic New Year' => $this->islamicNewYear($year),
'Birthday of the Prophet Muhammad' => $this->prophetMuhammadBirthday($year),
],
$this->convertPeriods($eidAlAdha[0], $year, 'Eid al-Adha'),
$this->convertPeriods($eidAlFitr[0], $year, 'Eid al-Fitr'),
$this->convertPeriods($ashura[0], $year, 'Ashura'),
);
}

/** @return array<string, CarbonInterface> */
private function adjustForWeekend(string $name, CarbonImmutable $date): array
{
$adjustedHolidays = [];
Expand All @@ -370,9 +340,4 @@ private function adjustForWeekend(string $name, CarbonImmutable $date): array

return $adjustedHolidays;
}

public function islamicHolidays(int $year): array
{
// TODO: Implement islamicHolidays() method.
}
}

0 comments on commit f45c884

Please sign in to comment.