From b0a0368aac45536ff4822a02bc92aa945fba9961 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Wed, 17 Jan 2024 21:18:04 +0200 Subject: [PATCH] Add holidays for South Africa --- src/Countries/SouthAfrica.php | 59 +++++++++++++++++++ ...t_can_calculate_south_africa_holidays.snap | 54 +++++++++++++++++ tests/Countries/SouthAfricaTest.php | 18 ++++++ 3 files changed, 131 insertions(+) create mode 100644 src/Countries/SouthAfrica.php create mode 100644 tests/.pest/snapshots/Countries/SouthAfricaTest/it_can_calculate_south_africa_holidays.snap create mode 100644 tests/Countries/SouthAfricaTest.php diff --git a/src/Countries/SouthAfrica.php b/src/Countries/SouthAfrica.php new file mode 100644 index 000000000..79e72d223 --- /dev/null +++ b/src/Countries/SouthAfrica.php @@ -0,0 +1,59 @@ + '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 */ + protected function variableHolidays(int $year): array + { + $easter = $this->easter($year); + + return [ + 'Good Friday' => $easter->subDays(2), + 'Family Day' => $easter->addDay(), + ]; + } +} diff --git a/tests/.pest/snapshots/Countries/SouthAfricaTest/it_can_calculate_south_africa_holidays.snap b/tests/.pest/snapshots/Countries/SouthAfricaTest/it_can_calculate_south_africa_holidays.snap new file mode 100644 index 000000000..a77953f69 --- /dev/null +++ b/tests/.pest/snapshots/Countries/SouthAfricaTest/it_can_calculate_south_africa_holidays.snap @@ -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" + } +] \ No newline at end of file diff --git a/tests/Countries/SouthAfricaTest.php b/tests/Countries/SouthAfricaTest.php new file mode 100644 index 000000000..fe98169c5 --- /dev/null +++ b/tests/Countries/SouthAfricaTest.php @@ -0,0 +1,18 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); +});