Skip to content

Commit

Permalink
use an enum for supported countries with the related ISO code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Jan 12, 2024
1 parent 29ce469 commit 5029d6b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
28 changes: 12 additions & 16 deletions src/Actions/Belgium.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@

class Belgium implements Executable
{
protected int $year;

public function execute(int $year): array
{
$this->year = $year;

$this->ensureYearCanBeCalculated();
$this->ensureYearCanBeCalculated($year);

$fixedHolidays = $this->fixedHolidays();
$variableHolidays = $this->variableHolidays();
$fixedHolidays = $this->fixedHolidays($year);
$variableHolidays = $this->variableHolidays($year);

return array_merge($fixedHolidays, $variableHolidays);
}

protected function ensureYearCanBeCalculated(): void
protected function ensureYearCanBeCalculated(int $year): void
{
if ($this->year < 1970) {
throw HolidaysException::yearTooLow($this->year);
if ($year < 1970) {
throw HolidaysException::yearTooLow($year);
}

if ($this->year > 2037) {
throw HolidaysException::yearTooHigh($this->year);
if ($year > 2037) {
throw HolidaysException::yearTooHigh($year);
}
}

/** @return array<string, CarbonImmutable> */
protected function fixedHolidays(): array
protected function fixedHolidays(int $year): array
{
$dates = [
'Nieuwjaar' => '01-01',
Expand All @@ -46,16 +42,16 @@ protected function fixedHolidays(): array
];

foreach ($dates as $name => $date) {
$dates[$name] = CarbonImmutable::createFromFormat('d-m-Y', "{$date}-{$this->year}");
$dates[$name] = CarbonImmutable::createFromFormat('d-m-Y', "{$date}-{$year}");
}

return $dates;
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(): array
protected function variableHolidays(int $year): array
{
$easter = CarbonImmutable::createFromTimestamp(easter_date($this->year))
$easter = CarbonImmutable::createFromTimestamp(easter_date($year))
->setTimezone('Europe/Brussels');

return [
Expand Down
8 changes: 8 additions & 0 deletions src/Enums/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Spatie\Holidays\Enums;

enum Country: string
{
case Belgium = 'BE';
}
5 changes: 0 additions & 5 deletions src/Exceptions/HolidaysException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class HolidaysException extends RuntimeException
{
public static function unknownCountryCode(string $countryCode): self
{
return new self("Country code `{$countryCode}` is not supported");
}

public static function noCountryCode(): self
{
return new self("Please provide a country code.");
Expand Down
13 changes: 8 additions & 5 deletions src/Holidays.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Spatie\Holidays\Actions\Belgium;
use Spatie\Holidays\Enums\Country;
use Spatie\Holidays\Exceptions\HolidaysException;

class Holidays
Expand All @@ -13,11 +14,14 @@ class Holidays

protected int $year;

protected Country $country;

private function __construct(
?int $year = null,
protected ?string $countryCode = 'BE' // @todo make this configurable ?
?Country $country = null,
) {
$this->year = $year ?? CarbonImmutable::now()->year;
$this->country = $country ?? Country::Belgium; // @todo make configurable ?
}

public static function new(): static
Expand All @@ -40,7 +44,7 @@ public function year(int $year): static

public function country(string $countryCode): static
{
return new static(countryCode: $countryCode);
return new static(country: Country::from($countryCode));
}

/** @return array<array{name: string, date: string}> */
Expand All @@ -55,10 +59,9 @@ public function get(): array

protected function calculate(): self
{
$action = match ($this->countryCode) {
'BE' => new Belgium(),
$action = match ($this->country) {
Country::Belgium => new Belgium(),
null => throw HolidaysException::noCountryCode(),

Check failure on line 64 in src/Holidays.php

View workflow job for this annotation

GitHub Actions / phpstan

Match arm is unreachable because previous comparison is always true.
default => throw HolidaysException::unknownCountryCode($this->countryCode),
};

$this->holidays = $action->execute($this->year);
Expand Down
2 changes: 1 addition & 1 deletion tests/HolidaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

it('cannot get all holidays of an unknown country code', function () {
Holidays::new()->country('unknown')->get();
})->throws(HolidaysException::class, 'Country code `unknown` is not supported');
})->throws(ValueError::class, 'Country code `unknown` is not supported');

it('cannot get holidays for years before 1970', function () {
Holidays::new()->year(1969)->get();
Expand Down

0 comments on commit 5029d6b

Please sign in to comment.