Skip to content

Commit

Permalink
handle more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Mar 11, 2024
1 parent e003bf4 commit 683e399
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
44 changes: 27 additions & 17 deletions src/Calendars/IslamicCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,30 @@ trait IslamicCalendar
{
/** @return CarbonPeriod|array<CarbonPeriod> */
public function eidAlFitr(int $year, int $totalDays = 3): CarbonPeriod|array
{
return $this->getHoliday(self::eidAlFitr, $year, $totalDays);
}

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

protected function getHoliday(array $collection, int $year, int $totalDays): CarbonPeriod|array
{
try {
$date = self::eidAlFitr[$year];
$date = $collection[$year];
} catch (\Exception) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

$overlap = $this->getOverlapping(self::eidAlFitr, $year, $totalDays);

if ($overlap) {
$date = [$date, $overlap];
}

if (! is_array($date)) {
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay();
$end = $start->addDays($totalDays - 1)->startOfDay();
Expand All @@ -40,32 +57,25 @@ public function eidAlFitr(int $year, int $totalDays = 3): CarbonPeriod|array
return $periods;
}

/** @return CarbonPeriod|array<CarbonPeriod> */
public function eidAlAdha(int $year, int $totalDays = 4): CarbonPeriod|array
protected function getOverlapping(array $collection, int $year, $totalDays): ?string
{
try {
$date = self::eidAlAdha[$year];
$date = $collection[$year-1];
} catch (\Exception) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

if (! is_array($date)) {
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay();
$end = $start->addDays($totalDays - 1)->startOfDay();

return CarbonPeriod::create($start, '1 day', $end);
if (is_array($date)) {
$date = end($date);
}

// Twice a year
$periods = [];
$dates = $date;
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay();
$end = $start->addDays($totalDays-1)->startOfDay();

foreach ($dates as $date) {
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay();
$end = $start->addDays($totalDays-1)->startOfDay();
$periods[] = CarbonPeriod::create($start, '1 day', $end);
if ($end->year !== $year) {
return $date;
}

return $periods;
return null;
}
}
2 changes: 1 addition & 1 deletion src/Countries/Bahrain.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected function variableHolidays(int $year): array
'Birthday of the Prophet Muhammad' => self::prophetMuhammadBirthday[$year],
];

return $this->convertPeriods($holidays);
return $this->convertPeriods($holidays, $year);
}

protected function eidAlAdha(int $year): CarbonPeriod
Expand Down
7 changes: 7 additions & 0 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Exceptions\InvalidCountry;
Expand Down Expand Up @@ -124,14 +125,20 @@ protected function ensureYearCanBeCalculated(int $year): void

protected function convertPeriods(
array $holidays,
int $year,
string $suffix = 'Day',
string $prefix = ''
): array {
$result = [];

foreach ($holidays as $name => $holiday) {
if ($holiday instanceof CarbonPeriod) {
/** @var CarbonInterface $day */
foreach ($holiday as $index => $day) {
if ($day->year !== $year) {
continue;
}

if ($index === 0) {
$formattedSuffix = '';
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Countries/Turkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public function islamicHolidays(int $year): array
]);

$holidays = array_merge($holidays, [
'Eid al-Fitr Eve' => $eidAlFitr[1]->first()?->subDay()->toImmutable(),
'Eid al-Fitr' => $eidAlFitr[1],
'2. Eid al-Fitr Eve' => $eidAlFitr[1]->first()?->subDay()->toImmutable(),
'2. Eid al-Fitr' => $eidAlFitr[1],
]);
} else {
$holidays = array_merge($holidays, [
Expand All @@ -246,6 +246,6 @@ public function islamicHolidays(int $year): array
]);
}

return $this->convertPeriods($holidays);
return $this->convertPeriods($holidays, $year);
}
}

0 comments on commit 683e399

Please sign in to comment.