Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
],
"require": {
"php": "^8.1",
"ext-calendar": "*",
"ext-intl": "*",
"nesbot/carbon": "^2.72.1|^3.0"
"nesbot/carbon": "^2.72.1|^3.0",
"geniusts/hijri-dates": "^1.1"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
123 changes: 123 additions & 0 deletions src/Countries/Algeria.php
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the format like in other classes?
Look at Belgium.php for example

'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];
}
}
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"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/AlgeriaTest.php
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();
});
Loading