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
124 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,52 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
|
||
class SouthAfrica extends Country | ||
{ | ||
public function countryCode(): string | ||
{ | ||
return 'za'; | ||
} | ||
|
||
protected function allHolidays(int $year): array | ||
{ | ||
$holidays = [ | ||
'Nuwe Jaar' => '01-01', | ||
'Mense Regte Dag' => '03-21', | ||
'Vryhuids Dag' => '04-27', | ||
'Werkers Dag' => '05-01', | ||
'Jeug Dag' => '06-16', | ||
'Vroue Dag' => '08-09', | ||
'Erfenis Dag' => '09-24', | ||
'Versoenings Dag' => '12-16', | ||
'Kersfees' => '12-25', | ||
'Dag van Welwillendheid' => '12-26', | ||
]; | ||
|
||
foreach ($holidays as $name => $date) { | ||
$holidayDate = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}"); | ||
assert($holidayDate instanceof CarbonImmutable); | ||
|
||
if ($holidayDate->isSunday()) { | ||
$holidays[$name . ' (Waarneming)'] = $holidayDate->addDay()->format('m-d'); | ||
} | ||
} | ||
|
||
return array_merge($holidays, $this->variableHolidays($year)); | ||
} | ||
|
||
/** @return array<string, CarbonImmutable> */ | ||
protected function variableHolidays(int $year): array | ||
{ | ||
$easter = CarbonImmutable::createFromTimestamp(easter_date($year)) | ||
->setTimezone('Africa/Johannesburg'); | ||
|
||
return [ | ||
'Goeie Vrydag' => $easter->subDays(2), | ||
'Familie Dag' => $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": "Nuwe Jaar", | ||
"date": "2024-01-01" | ||
}, | ||
{ | ||
"name": "Mense Regte Dag", | ||
"date": "2024-03-21" | ||
}, | ||
{ | ||
"name": "Goeie Vrydag", | ||
"date": "2024-03-29" | ||
}, | ||
{ | ||
"name": "Familie Dag", | ||
"date": "2024-04-01" | ||
}, | ||
{ | ||
"name": "Vryhuidsdag", | ||
"date": "2024-04-27" | ||
}, | ||
{ | ||
"name": "Werkers Dag", | ||
"date": "2024-05-01" | ||
}, | ||
{ | ||
"name": "Jeug Dag", | ||
"date": "2024-06-16" | ||
}, | ||
{ | ||
"name": "Jeug Dag (Waarneming)", | ||
"date": "2024-06-17" | ||
}, | ||
{ | ||
"name": "Vroue Dag", | ||
"date": "2024-08-09" | ||
}, | ||
{ | ||
"name": "Erfenis Dag", | ||
"date": "2024-09-24" | ||
}, | ||
{ | ||
"name": "Versoenings Dag", | ||
"date": "2024-12-16" | ||
}, | ||
{ | ||
"name": "Kersfees", | ||
"date": "2024-12-25" | ||
}, | ||
{ | ||
"name": "Dag van Welwillendheid", | ||
"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(); | ||
}); |