Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify countries #1

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/Actions/Executable.php

This file was deleted.

23 changes: 8 additions & 15 deletions src/Actions/Belgium.php → src/Countries/Belgium.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

namespace Spatie\Holidays\Actions;
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Exceptions\HolidaysException;

class Belgium implements Executable
class Belgium extends Country
{
public function execute(int $year): array
public function countryCode(): string
{
return 'be';
}

public function get(int $year): array
{
$this->ensureYearCanBeCalculated($year);

Expand All @@ -17,17 +21,6 @@ public function execute(int $year): array
return array_merge($fixedHolidays, $variableHolidays);
}

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

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

/** @return array<string, CarbonImmutable> */
protected function fixedHolidays(int $year): array
{
Expand Down
46 changes: 46 additions & 0 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Spatie\Holidays\Countries;

use Spatie\Holidays\Exceptions\InvalidYear;

abstract class Country
{
public static function find(string $countryCode): ?Country
{
$countryCode = strtolower($countryCode);

foreach (glob(__DIR__.'/../Countries/*.php') as $filename) {
if (basename($filename) === 'Country.php') {
continue;
}

// determine class name from file name
$countryClass = '\\Spatie\\Holidays\\Countries\\'.basename($filename, '.php');

/** @var \Spatie\Holidays\Countries\Country $country */
$country = new $countryClass;

if (strtolower($country->countryCode()) === $countryCode) {
return $country;
}
}

return null;
}

abstract public function countryCode(): string;

abstract public function get(int $year): array;

protected static function ensureYearCanBeCalculated(int $year): void
{
if ($year < 1970) {
throw InvalidYear::yearTooLow();
}

if ($year > 2037) {
throw InvalidYear::yearTooHigh();
}
}
}
18 changes: 0 additions & 18 deletions src/Enums/Country.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use RuntimeException;

class HolidaysException extends RuntimeException
class InvalidYear extends RuntimeException
{
public static function yearTooLow(): self
{
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/UnsupportedCountry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Holidays\Exceptions;

use RuntimeException;

class UnsupportedCountry extends RuntimeException
{
public static function make(string $countryCode): self
{
return new self("Country code `{$countryCode}` is not supported.");
}
}
23 changes: 15 additions & 8 deletions src/Holidays.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Spatie\Holidays;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Enums\Country;
use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Exceptions\UnsupportedCountry;

class Holidays
{
Expand All @@ -19,7 +20,7 @@ private function __construct(
?Country $country = null,
) {
$this->year = $year ?? CarbonImmutable::now()->year;
$this->country = $country ?? Country::Belgium; // @todo make configurable ?
$this->country = $country ?? Country::find('be');
}

public static function new(): static
Expand All @@ -37,12 +38,20 @@ public static function all(): array

public function year(int $year): static
{
return new static(year: $year);
return new static(year: $year, country: $this->country);
}

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

if (! $country) {
throw UnsupportedCountry::make($countryCode);
}

$this->country = Country::find($countryCode);

return $this;
}

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

protected function calculate(): self
{
$this->holidays = $this->country
->action()
->execute($this->year);
$this->holidays = $this->country->get($this->year);

uasort($this->holidays,
fn (CarbonImmutable $a, CarbonImmutable $b) => $a->timestamp <=> $b->timestamp
Expand Down
11 changes: 6 additions & 5 deletions tests/HolidaysTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

use Carbon\CarbonImmutable;
use Spatie\Holidays\Exceptions\HolidaysException;
use Spatie\Holidays\Exceptions\InvalidYear;
use Spatie\Holidays\Exceptions\UnsupportedCountry;
use Spatie\Holidays\Holidays;

it('can get all holidays of the current year', function () {
Expand Down Expand Up @@ -30,7 +31,7 @@

it('can get all holidays of another year and a specific country', function () {
$holidays = Holidays::new()
->year(2023)
->year(2024)
->country('BE')
->get();

Expand All @@ -39,12 +40,12 @@

it('cannot get all holidays of an unknown country code', function () {
Holidays::new()->country('unknown')->get();
})->throws(ValueError::class);
})->throws(UnsupportedCountry::class);

it('cannot get holidays for years before 1970', function () {
Holidays::new()->year(1969)->get();
})->throws(HolidaysException::class, 'Holidays can only be calculated for years after 1970.');
})->throws(InvalidYear::class, 'Holidays can only be calculated for years after 1970.');

it('cannot get holidays for years after 2037', function () {
Holidays::new()->year(2038)->get();
})->throws(HolidaysException::class, 'Holidays can only be calculated for years before 2038');
})->throws(InvalidYear::class, 'Holidays can only be calculated for years before 2038');