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.
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
use function in_array; | ||
|
||
class SouthAfrica extends Country | ||
{ | ||
public function countryCode(): string | ||
{ | ||
return 'za'; | ||
} | ||
|
||
protected function allHolidays(int $year): array | ||
{ | ||
// https://en.wikipedia.org/wiki/Public_holidays_in_South_Africa | ||
// https://www.gov.za/about-sa/public-holidays | ||
$holidays = [ | ||
'New Year\'s Day' => '01-01', | ||
'Human Rights Day' => '03-21', | ||
'Freedom Day' => '04-27', | ||
'Workers\' Day' => '05-01', | ||
'Youth Day' => '06-16', | ||
'National Women\'s Day' => '08-09', | ||
'Heritage Day' => '09-24', | ||
'Day of Reconciliation' => '12-16', | ||
'Christmas Day' => '12-25', | ||
'Day of Goodwill' => '12-26', | ||
]; | ||
|
||
foreach ($holidays as $name => $date) { | ||
$holidayDate = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}"); | ||
assert($holidayDate instanceof CarbonImmutable); | ||
|
||
// The Public Holidays Act (Act No 36 of 1994) states that whenever a public holiday falls on a Sunday, the Monday following it will be a public holiday. | ||
// https://www.gov.za/documents/public-holidays-act | ||
if ( | ||
$holidayDate->isSunday() && | ||
!in_array($holidayDate->addDay()->format('m-d'), $holidays, true) // Check that the Monday is not already a holiday | ||
) { | ||
$holidays[$name . ' Observed'] = $holidayDate->addDay(); | ||
} | ||
} | ||
|
||
return array_merge($holidays, $this->variableHolidays($year)); | ||
} | ||
|
||
/** @return array<string, CarbonImmutable> */ | ||
protected function variableHolidays(int $year): array | ||
{ | ||
$easter = $this->easter($year); | ||
|
||
return [ | ||
'Good Friday' => $easter->subDays(2), | ||
'Family Day' => $easter->addDay(), | ||
]; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/.pest/snapshots/Countries/SouthAfricaTest/it_can_calculate_south_africa_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": "Human Rights Day", | ||
"date": "2024-03-21" | ||
}, | ||
{ | ||
"name": "Good Friday", | ||
"date": "2024-03-29" | ||
}, | ||
{ | ||
"name": "Family Day", | ||
"date": "2024-04-01" | ||
}, | ||
{ | ||
"name": "Freedom Day", | ||
"date": "2024-04-27" | ||
}, | ||
{ | ||
"name": "Workers' Day", | ||
"date": "2024-05-01" | ||
}, | ||
{ | ||
"name": "Youth Day", | ||
"date": "2024-06-16" | ||
}, | ||
{ | ||
"name": "Youth Day Observed", | ||
"date": "2024-06-17" | ||
}, | ||
{ | ||
"name": "National Women's Day", | ||
"date": "2024-08-09" | ||
}, | ||
{ | ||
"name": "Heritage Day", | ||
"date": "2024-09-24" | ||
}, | ||
{ | ||
"name": "Day of Reconciliation", | ||
"date": "2024-12-16" | ||
}, | ||
{ | ||
"name": "Christmas Day", | ||
"date": "2024-12-25" | ||
}, | ||
{ | ||
"name": "Day of Goodwill", | ||
"date": "2024-12-26" | ||
} | ||
] |
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 south africa holidays', function () { | ||
CarbonImmutable::setTestNowAndTimezone('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'za')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
}); |