Skip to content

Commit

Permalink
use Observable trait where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Feb 9, 2024
1 parent 073efe7 commit df9d4d6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/Concerns/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
12 changes: 7 additions & 5 deletions src/Countries/Jamaica.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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');
}
Expand Down
28 changes: 17 additions & 11 deletions src/Countries/Latvia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,20 +48,23 @@ protected function variableHolidays(int $year): array
/** @return array<string, string> */
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;
Expand Down
17 changes: 4 additions & 13 deletions src/Countries/SouthAfrica.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
}

0 comments on commit df9d4d6

Please sign in to comment.