generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Algerian holidays #90
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,123 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
use GeniusTS\HijriDate\Hijri; | ||
|
||
class Algeria extends Country | ||
{ | ||
protected int $adjustmentDays = 1; | ||
|
||
public function countryCode(): string | ||
{ | ||
return 'dz'; | ||
} | ||
|
||
protected function allHolidays(int $year): array | ||
{ | ||
return array_merge($this->fixedHolidays($year), $this->variableHolidays($year)); | ||
} | ||
|
||
/** @return array<string, CarbonImmutable> */ | ||
protected function fixedHolidays(int $year): array | ||
{ | ||
return [ | ||
'New Year\'s Day' => CarbonImmutable::createFromDate($year, 1, 1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the format like in other classes? |
||
'Amazigh New Year' => CarbonImmutable::createFromDate($year, 1, 12), | ||
'Labour day' => CarbonImmutable::createFromDate($year, 5, 1), | ||
'Independence day' => CarbonImmutable::createFromDate($year, 7, 5), | ||
'Revolution Day' => CarbonImmutable::createFromDate($year, 11, 1), | ||
]; | ||
} | ||
|
||
/** @return array<string, CarbonImmutable> */ | ||
protected function variableHolidays(int $year): array | ||
{ | ||
return [ | ||
'Islamic new year' => $this->getHijriDateAsGregorian(1, 1, $year + 1), | ||
'Ashura' => $this->getHijriDateAsGregorian(10, 1, $year + 1), | ||
'Mawlid' => $this->getHijriDateAsGregorian(12, 3, $year + 1), | ||
'Eid al-Fitr' => $this->getHijriDateAsGregorian(1, 10, $year, $this->adjustmentDays), | ||
'Eid al-Fitr - 2nd day' => $this->getHijriDateAsGregorian(2, 10, $year, $this->adjustmentDays), | ||
'Eid al-Adha' => $this->getHijriDateAsGregorian(10, 12, $year, $this->adjustmentDays), | ||
'Eid al-Adha - 2nd day' => $this->getHijriDateAsGregorian(11, 12, $year, $this->adjustmentDays), | ||
'Eid al-Adha - 3rd day' => $this->getHijriDateAsGregorian(12, 12, $year, $this->adjustmentDays), | ||
]; | ||
} | ||
|
||
/** | ||
* @param int $year | ||
* @return int | ||
*/ | ||
protected function convertToHijriYear(int $year): int | ||
{ | ||
$gregorianNewYear = CarbonImmutable::create($year, 1, 1); | ||
$hijriNewYear = Hijri::convertToHijri($gregorianNewYear); | ||
$hijriYear = $hijriNewYear->year; | ||
|
||
return $hijriYear; | ||
} | ||
|
||
/** | ||
* Converts a Hijri date to a Gregorian date with a possible adjustment. | ||
* | ||
* @param int $hijriDay | ||
* @param int $hijriMonth | ||
* @param int $hijriYear | ||
* @param int $adjustmentDays Number of days to adjust the Hijri date by. | ||
* @return CarbonImmutable | ||
*/ | ||
protected function getHijriDateAsGregorian(int $hijriDay, int $hijriMonth, int $hijriYear, int $adjustmentDays = 0): CarbonImmutable | ||
{ | ||
$hijriYear = $this->convertToHijriYear($hijriYear); | ||
$gregorianDate = Hijri::convertToGregorian($hijriDay, $hijriMonth, $hijriYear); | ||
return CarbonImmutable::instance($gregorianDate)->addDays($adjustmentDays); | ||
} | ||
|
||
/** | ||
* Calculates the last 10 days of Ramadan for a given year. | ||
* | ||
* @param int $year | ||
* @return array<string, CarbonImmutable> | ||
*/ | ||
protected function getLastTenDaysOfRamadan(int $year): array | ||
{ | ||
$last10DaysOfRamadan = []; | ||
|
||
$eidAlFitr = $this->getHijriDateAsGregorian(1, 10, $year, $this->adjustmentDays); // Shawwal 1 | ||
|
||
for ($i = 9; $i >= 0; $i--) { | ||
$day = $eidAlFitr->subDays($i + 1); // Counting back from the day before Eid al-Fitr | ||
$last10DaysOfRamadan["Last 10 Days of Ramadan - Day " . (10 - $i)] = $day; | ||
} | ||
return $last10DaysOfRamadan; | ||
} | ||
|
||
/** | ||
* Calculates the length of Ramadan for a given year. | ||
* | ||
* @param int $year | ||
* @return int | ||
*/ | ||
protected function getLengthOfRamadan(int $year): int | ||
{ | ||
$firstDayOfRamadan = $this->getHijriDateAsGregorian(1, 9, $year); // Ramadan 1 | ||
$lastDayOfRamadan = $this->getHijriDateAsGregorian(1, 10, $year); // Shawwal 1 | ||
$lengthOfRamadan = $firstDayOfRamadan->diffInDays($lastDayOfRamadan); | ||
return $lengthOfRamadan; | ||
} | ||
|
||
/** | ||
* Get the two days following an Eid day. | ||
* | ||
* @param CarbonImmutable $eidDay | ||
* @return array<int, CarbonImmutable> | ||
*/ | ||
protected function getDaysFollowingEid(CarbonImmutable $eidDay): array | ||
{ | ||
$secondDay = $eidDay->addDay(); | ||
$thirdDay = $eidDay->addDays(2); | ||
return [$secondDay, $thirdDay]; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/.pest/snapshots/Countries/AlgeriaTest/it_can_calculate_algerian_holidays.snap
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,54 @@ | ||
[ | ||
{ | ||
"name": "New Year's Day", | ||
"date": "2024-01-01" | ||
}, | ||
{ | ||
"name": "Amazigh New Year", | ||
"date": "2024-01-12" | ||
}, | ||
{ | ||
"name": "Eid al-Fitr", | ||
"date": "2024-04-10" | ||
}, | ||
{ | ||
"name": "Eid al-Fitr - 2nd day", | ||
"date": "2024-04-11" | ||
}, | ||
{ | ||
"name": "Labour day", | ||
"date": "2024-05-01" | ||
}, | ||
{ | ||
"name": "Eid al-Adha", | ||
"date": "2024-06-17" | ||
}, | ||
{ | ||
"name": "Eid al-Adha - 2nd day", | ||
"date": "2024-06-18" | ||
}, | ||
{ | ||
"name": "Eid al-Adha - 3rd day", | ||
"date": "2024-06-19" | ||
}, | ||
{ | ||
"name": "Independence day", | ||
"date": "2024-07-05" | ||
}, | ||
{ | ||
"name": "Islamic new year", | ||
"date": "2024-07-07" | ||
}, | ||
{ | ||
"name": "Ashura", | ||
"date": "2024-07-16" | ||
}, | ||
{ | ||
"name": "Mawlid", | ||
"date": "2024-09-15" | ||
}, | ||
{ | ||
"name": "Revolution Day", | ||
"date": "2024-11-01" | ||
} | ||
] |
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,18 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Tests\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Spatie\Holidays\Holidays; | ||
|
||
it('can calculate algerian holidays', function () { | ||
CarbonImmutable::setTestNowAndTimezone('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'dz')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using this library for a couple of reasons: