diff --git a/src/Concerns/Observable.php b/src/Concerns/Observable.php index 52342c6fe..686c11b6d 100644 --- a/src/Concerns/Observable.php +++ b/src/Concerns/Observable.php @@ -43,8 +43,12 @@ protected function weekendToNextMonday(string|CarbonInterface $date, int $year): return null; } - protected function sundayToNextMonday(CarbonInterface $date): ?CarbonInterface + protected function sundayToNextMonday(string|CarbonInterface $date, int $year): ?CarbonInterface { + if (is_string($date)) { + $date = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay(); + } + if ($date->isSunday()) { return $date->next('monday'); } diff --git a/src/Countries/Jamaica.php b/src/Countries/Jamaica.php index 801217120..96472e60f 100644 --- a/src/Countries/Jamaica.php +++ b/src/Countries/Jamaica.php @@ -4,9 +4,12 @@ use Carbon\CarbonImmutable; use Carbon\CarbonInterface; +use Spatie\Holidays\Concerns\Observable; class Jamaica extends Country { + use Observable; + public function countryCode(): string { return 'jm'; @@ -33,7 +36,10 @@ protected function fixedHolidays(int $year): array ]; foreach ($holidays as $name => $date) { - $observedDay = $this->observed($name, $date, $year); + $observedDay = match ($name) { + 'Labour Day', 'Boxing Day' => $this->observed($name, $date, $year), + default => $this->sundayToNextMonday($date, $year), + }; if ($observedDay) { $holidays[$name.' Observed'] = $observedDay; @@ -60,10 +66,6 @@ protected function observed(string $name, string $date, int $year): ?CarbonInter { $holiday = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")->startOfDay(); - if ($holiday->isSunday()) { - return $holiday->next('monday'); - } - if ($name === 'Labour Day' && $holiday->isSaturday()) { return $holiday->next('monday'); } diff --git a/src/Countries/Latvia.php b/src/Countries/Latvia.php index fb26ede73..ff6ac9346 100644 --- a/src/Countries/Latvia.php +++ b/src/Countries/Latvia.php @@ -3,9 +3,12 @@ namespace Spatie\Holidays\Countries; use Carbon\CarbonImmutable; +use Spatie\Holidays\Concerns\Observable; class Latvia extends Country { + use Observable; + public function countryCode(): string { return 'lv'; @@ -45,20 +48,23 @@ protected function variableHolidays(int $year): array /** @return array */ protected function observedHolidays(int $year): array { - // If the holidays - May 4 and November 18 - fall on a Saturday or Sunday, - // the next working day is designated as a holiday. - $holidays = []; + $holidays = [ + 'Latvijas Republikas Neatkarības deklarācijas pasludināšanas diena' => '05-04', + 'Latvijas Republikas proklamēšanas diena' => '11-18', + ]; - $date = new CarbonImmutable(); + foreach ($holidays as $name => $date) { + $observedDay = $this->weekendToNextMonday($date, $year); - $date = $date->setDate($year, 5, 4); - if ($date->isWeekend()) { - $holidays['Pārceltā 4. maija brīvdiena'] = $date->nextWeekday()->format('m-d'); - } + if ($observedDay) { + if ($name === 'Latvijas Republikas Neatkarības deklarācijas pasludināšanas diena') { + $holidays['Pārceltā 4. maija brīvdiena'] = $observedDay; + } - $date = $date->setDate($year, 11, 18); - if ($date->isWeekend()) { - $holidays['Pārceltā 18. novembra brīvdiena'] = $date->nextWeekday()->format('m-d'); + if ($name === 'Latvijas Republikas proklamēšanas diena') { + $holidays['Pārceltā 18. novembra brīvdiena'] = $observedDay; + } + } } return $holidays; diff --git a/src/Countries/SouthAfrica.php b/src/Countries/SouthAfrica.php index f85b14255..8ef21ac51 100644 --- a/src/Countries/SouthAfrica.php +++ b/src/Countries/SouthAfrica.php @@ -4,9 +4,12 @@ use Carbon\CarbonImmutable; use Carbon\CarbonInterface; +use Spatie\Holidays\Concerns\Observable; class SouthAfrica extends Country { + use Observable; + public function countryCode(): string { return 'za'; @@ -30,7 +33,7 @@ protected function allHolidays(int $year): array ]; foreach ($holidays as $name => $date) { - $observedDay = $this->observed($date, $year); + $observedDay = $this->sundayToNextMonday($date, $year); if ($observedDay) { $holidays[$name.' Observed'] = $observedDay; @@ -50,16 +53,4 @@ 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; - } }