generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from spatie/refactor/islamic-calendar
Implement Islamic calendar
- Loading branch information
Showing
18 changed files
with
485 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"New Year's Day": "Yılbaşı", | ||
"Eid al-Fitr Eve": "Ramazan Bayramı Arifesi", | ||
"Eid al-Fitr": "Ramazan Bayramı 1. Gün", | ||
"Eid al-Fitr Day 2": "Ramazan Bayramı 2. Gün", | ||
"Eid al-Fitr Day 3": "Ramazan Bayramı 3. Gün", | ||
"National Sovereignty and Children's Day": "Ulusal Egemenlik ve Çocuk Bayramı", | ||
"Labor and Solidarity Day": "Emek ve Dayanışma Günü", | ||
"Commemoration of Atatürk, Youth and Sports Day": "Atatürk'ü Anma, Gençlik ve Spor Bayramı", | ||
"Eid al-Adha Eve": "Kurban Bayramı Arifesi", | ||
"Eid al-Adha": "Kurban Bayramı 1. Gün", | ||
"Eid al-Adha Day 2": "Kurban Bayramı 2. Gün", | ||
"Eid al-Adha Day 3": "Kurban Bayramı 3. Gün", | ||
"Eid al-Adha Day 4": "Kurban Bayramı 4. Gün", | ||
"Democracy and National Unity Day": "Demokrasi ve Millî Birlik Günü", | ||
"Victory Day": "Zafer Bayramı", | ||
"Republic Day Eve": "Cumhuriyet Bayramı Arifesi", | ||
"Republic Day": "Cumhuriyet Bayramı", | ||
"2. Eid al-Fitr Eve": "2. Ramazan Bayramı Arifesi", | ||
"2. Eid al-Fitr": "2. Ramazan Bayramı 1. Gün", | ||
"2. Eid al-Fitr Day 2": "2. Ramazan Bayramı 2. Gün", | ||
"2. Eid al-Fitr Day 3": "2. Ramazan Bayramı 3. Gün", | ||
"2. Eid al-Adha Eve": "2. Kurban Bayramı Arifesi", | ||
"2. Eid al-Adha": "2. Kurban Bayramı 1. Gün", | ||
"2. Eid al-Adha Day 2": "2. Kurban Bayramı 2. Gün", | ||
"2. Eid al-Adha Day 3": "2. Kurban Bayramı 3. Gün", | ||
"2. Eid al-Adha Day 4": "2. Kurban Bayramı 4. Gün" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Calendars; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Carbon\CarbonPeriod; | ||
use Carbon\Exceptions\InvalidFormatException; | ||
use Spatie\Holidays\Countries\Country; | ||
use Spatie\Holidays\Exceptions\InvalidYear; | ||
|
||
/** @mixin Country */ | ||
trait IslamicCalendar | ||
{ | ||
/** @return array<CarbonPeriod> */ | ||
public function eidAlFitr(int $year, int $totalDays = 3): array | ||
{ | ||
return $this->getMultiDayHoliday(self::eidAlFitr, $year, $totalDays); | ||
} | ||
|
||
/** @return array<CarbonPeriod> */ | ||
public function eidAlAdha(int $year, int $totalDays = 4): array | ||
{ | ||
return $this->getMultiDayHoliday(self::eidAlAdha, $year, $totalDays); | ||
} | ||
|
||
/** @return array<CarbonPeriod> */ | ||
protected function ashura(int $year, int $totalDays = 2): array | ||
{ | ||
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 getMultiDayHoliday(array $collection, int $year, int $totalDays): array | ||
{ | ||
$date = $collection[$year] ?? null; | ||
|
||
if ($date === null) { | ||
throw InvalidYear::range($this->countryCode(), 1970, 2037); | ||
} | ||
|
||
$overlap = $this->getOverlapping($collection, $year, $totalDays); | ||
|
||
if ($overlap) { | ||
$period = $this->createPeriod($overlap, $year - 1, $totalDays); | ||
|
||
$date = [$period, $date]; | ||
} | ||
|
||
if (! is_array($date)) { | ||
return [$this->createPeriod($date, $year, $totalDays)]; | ||
} | ||
|
||
// Twice a year | ||
$periods = []; | ||
$dates = $date; | ||
|
||
/** @var CarbonPeriod|string $date */ | ||
foreach ($dates as $date) { | ||
if ($date instanceof CarbonPeriod) { | ||
$periods[] = $date; | ||
|
||
continue; | ||
} | ||
|
||
$periods[] = $this->createPeriod($date, $year, $totalDays); | ||
} | ||
|
||
return $periods; | ||
} | ||
|
||
protected function createPeriod(string $date, int $year, int $totalDays): CarbonPeriod | ||
{ | ||
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay(); | ||
$end = $start->addDays($totalDays - 1)->startOfDay(); | ||
|
||
return CarbonPeriod::create($start, '1 day', $end); | ||
} | ||
|
||
/** @param array<string|array<string>> $collection */ | ||
protected function getOverlapping(array $collection, int $year, int $totalDays): ?string | ||
{ | ||
if ($year === 1970) { | ||
return null; | ||
} | ||
|
||
$date = $collection[$year - 1] ?? null; | ||
|
||
if ($date === null) { | ||
throw InvalidYear::range($this->countryCode(), 1970, 2037); | ||
} | ||
|
||
if (is_array($date)) { | ||
$date = end($date); | ||
} | ||
|
||
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay(); | ||
$end = $start->addDays($totalDays - 1)->startOfDay(); | ||
|
||
if ($end->year !== $year) { | ||
return (string) $date; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Contracts; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Carbon\CarbonPeriod; | ||
|
||
interface Islamic | ||
{ | ||
/** @return array<string, string|CarbonImmutable|CarbonPeriod> */ | ||
public function islamicHolidays(int $year): array; | ||
} |
Oops, something went wrong.