Skip to content

Commit

Permalink
Added exceptions and null region check.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaltez committed Jan 31, 2024
1 parent 1f55b31 commit 4ffe3fa
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Countries/Spain.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use RuntimeException;

class Spain extends Country
{
Expand Down Expand Up @@ -48,11 +49,15 @@ protected function variableHolidays(int $year): array
/** @return array<string, string> */
protected function regionHolidays(int $year): array
{
if ($this->region === null) {
return [];
}

$method = "regionHolidays{$year}";
if (method_exists($this, $method)) {
return $this->$method();
} else {
return [];
throw new RuntimeException("No data for region '{$this->region}' on year {$year}.");
}
}

Expand Down Expand Up @@ -167,7 +172,7 @@ protected function regionHolidays2022(): array
'San José' => '03-19',
] + $juevesSanto + $lunesPascua + $sanJuan,

default => [],
default => throw new RuntimeException("Region '{$this->region}' is not valid."),
};
}

Expand Down Expand Up @@ -278,7 +283,7 @@ protected function regionHolidays2023(): array
'Día de la Comunidad Valenciana' => '10-09',
] + $lunesPascua + $sanJuan,

default => [],
default => throw new RuntimeException("Region '{$this->region}' is not valid."),
};
}

Expand Down Expand Up @@ -382,7 +387,7 @@ protected function regionHolidays2024(): array
'Día de la Comunidad Valenciana' => '10-09',
] + $sanJose + $lunesPascua + $sanJuan,

default => [],
default => throw new RuntimeException("Region '{$this->region}' is not valid."),
};
}
}

0 comments on commit 4ffe3fa

Please sign in to comment.