Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 12, 2024
1 parent fb25f1f commit a2a467e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 58 deletions.
11 changes: 0 additions & 11 deletions src/Actions/Executable.php

This file was deleted.

24 changes: 9 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

Check failure on line 14 in src/Countries/Belgium.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Holidays\Countries\Belgium::get() return type has no value type specified in iterable type 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 Expand Up @@ -60,4 +53,5 @@ protected function variableHolidays(int $year): array
'Pinkstermaandag' => $easter->addDays(50),
];
}

}
47 changes: 47 additions & 0 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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) {

Check failure on line 13 in src/Countries/Country.php

View workflow job for this annotation

GitHub Actions / phpstan

Argument of an invalid type array<int, string>|false supplied for foreach, only iterables are supported.
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 function countryCode(): string;

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

Check failure on line 35 in src/Countries/Country.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Holidays\Countries\Country::get() return type has no value type specified in iterable type 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');

Check failure on line 23 in src/Holidays.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Spatie\Holidays\Holidays::$country (Spatie\Holidays\Countries\Country) does not accept Spatie\Holidays\Countries\Country|null.
}

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)

Check failure on line 44 in src/Holidays.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Holidays\Holidays::country() has no return type specified.
{
return new static(country: Country::from($countryCode));
$country = Country::find($countryCode);

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

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

Check failure on line 52 in src/Holidays.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Spatie\Holidays\Holidays::$country (Spatie\Holidays\Countries\Country) does not accept Spatie\Holidays\Countries\Country|null.

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');

0 comments on commit a2a467e

Please sign in to comment.