Skip to content

Commit

Permalink
cleanup SouthAfrica
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Feb 8, 2024
1 parent bdcaa57 commit bf1c6a5
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Countries/SouthAfrica.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;

use Carbon\CarbonInterface;
use function in_array;

class SouthAfrica extends Country
Expand Down Expand Up @@ -31,16 +32,10 @@ protected function allHolidays(int $year): array
];

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();
$observedDay = $this->observed($date, $year);

if ($observedDay) {
$holidays[$name . ' Observed'] = $observedDay;
}
}

Expand All @@ -57,4 +52,16 @@ protected function variableHolidays(int $year): array
'Family Day' => $easter->addDay(),
];
}

protected function observed(string $date, int $year): ?CarbonInterface
{
$holiday = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay();

// https://www.gov.za/documents/public-holidays-act
if ($holiday->isSunday()) {
return $holiday->next('monday');
}

return null;
}
}

0 comments on commit bf1c6a5

Please sign in to comment.