Skip to content

Commit

Permalink
refactor Bahrain too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Mar 22, 2024
1 parent 3be5393 commit af20753
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 67 deletions.
27 changes: 15 additions & 12 deletions src/Countries/Bahrain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Spatie\Holidays\Calendars\IslamicCalendar;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Contracts\Islamic;

class Bahrain extends Country implements HasTranslations
class Bahrain extends Country implements HasTranslations, Islamic
{
use IslamicCalendar;
use Translatable;
Expand Down Expand Up @@ -151,30 +152,32 @@ public function defaultLocale(): string

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

return array_merge([
'New Year\'s Day' => '1-1',
'Labour Day' => '5-1',
'National Day' => '12-16',
'National Day 2' => '12-17',
], $variableHolidays);
],
$this->islamicHolidays($year)
);
}

/**
* @return array<string, CarbonPeriod|string>
*/
protected function variableHolidays(int $year): array
public function islamicHolidays(int $year): array
{
$eidAlFitr = $this->eidAlFitr($year);
$eidAlAdha = $this->eidAlAdha($year, 3);
$ashura = $this->ashura($year);

$holidays = [
'Eid al-Fitr' => $this->eidAlFitr($year),
'Eid al-Adha' => $this->eidAlAdha($year, 3),
'Arafat Day' => self::arafatDay[$year],
'Islamic New Year' => self::islamicNewYear[$year],
'Ashura' => $this->ashura($year),
'Birthday of the Prophet Muhammad' => self::prophetMuhammadBirthday[$year],
];

return $this->convertPeriods($holidays, $year);
return array_merge($holidays,
$this->convertPeriods($eidAlAdha[0], $year, 'Eid al-Adha'),
$this->convertPeriods($eidAlFitr[0], $year, 'Eid al-Fitr'),
$this->convertPeriods($ashura[0], $year, 'Ashura'),
);
}
}
52 changes: 1 addition & 51 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,63 +123,13 @@ protected function ensureYearCanBeCalculated(int $year): void
}
}

/**
* Convert holidays that are represented as CarbonPeriods to an array of CarbonImmutable dates.
* This is useful for holidays like Eid-al-Fitr that happen on multiple days.
*
* @param array<string, string|CarbonImmutable|CarbonPeriod|array<CarbonPeriod>> $holidays
* @return array<string, CarbonImmutable|string>
*/
protected function convertPeriods(
array $holidays,
int $year,
string $suffix = 'Day',
string $prefix = ''
): array {
$allDays = [];

foreach ($holidays as $name => $holiday) {
if (is_string($holiday)) {
$allDays[$name] = $holiday;

continue;
}

if ($holiday instanceof CarbonImmutable) {
$allDays[$name] = $holiday;

continue;
}

if ($holiday instanceof CarbonPeriod) {
$allDays = array_merge(
$allDays,
$this->handleCarbonPeriod($holiday, $year, $name, $suffix, $prefix)
);

continue;
}

foreach ($holiday as $day) {
if ($day instanceof CarbonPeriod) {
$allDays = array_merge(
$allDays,
$this->handleCarbonPeriod($day, $year, $name, $suffix, $prefix)
);
}
}
}

return $allDays;
}

/**
* Convert holidays that are represented as CarbonPeriods to an array of CarbonImmutable dates.
* This is useful for holidays like `Eid-al-Fitr` that happen on multiple days.
*
* @return array<string, CarbonImmutable>
*/
protected function handleCarbonPeriod(
protected function convertPeriods(
CarbonPeriod $period,
int $year,
string $name,
Expand Down
8 changes: 4 additions & 4 deletions src/Countries/Turkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,19 @@ public function islamicHolidays(int $year): array
$eidAlAdha = $this->eidAlAdha($year);

$holidays = array_merge(
$this->handleCarbonPeriod($eidAlAdha[0], $year, 'Eid al-Adha', includeEve: true),
$this->handleCarbonPeriod($eidAlFitr[0], $year, 'Eid al-Fitr', includeEve: true),
$this->convertPeriods($eidAlAdha[0], $year, 'Eid al-Adha', includeEve: true),
$this->convertPeriods($eidAlFitr[0], $year, 'Eid al-Fitr', includeEve: true),
);

if (count($eidAlAdha) > 1) {
$holidays = array_merge($holidays,
$this->handleCarbonPeriod($eidAlAdha[1], $year, '2. Eid al-Adha', includeEve: true),
$this->convertPeriods($eidAlAdha[1], $year, '2. Eid al-Adha', includeEve: true),
);
}

if (count($eidAlFitr) > 1) {
$holidays = array_merge($holidays,
$this->handleCarbonPeriod($eidAlFitr[1], $year, '2. Eid al-Fitr', includeEve: true),
$this->convertPeriods($eidAlFitr[1], $year, '2. Eid al-Fitr', includeEve: true),
);
}

Expand Down

0 comments on commit af20753

Please sign in to comment.