From bf1c6a52902746b6576bdb5e9e703bad3daa348e Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:29:14 +0100 Subject: [PATCH] cleanup SouthAfrica --- src/Countries/SouthAfrica.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Countries/SouthAfrica.php b/src/Countries/SouthAfrica.php index 64d404127..1d9cb1379 100644 --- a/src/Countries/SouthAfrica.php +++ b/src/Countries/SouthAfrica.php @@ -4,6 +4,7 @@ use Carbon\CarbonImmutable; +use Carbon\CarbonInterface; use function in_array; class SouthAfrica extends Country @@ -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; } } @@ -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; + } }