From d588e864548b93cb1a0e2b73b0162f940289fa7f Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:38:15 +0100 Subject: [PATCH] add docs for contributing + add example for regions --- README.md | 45 ++++++++++++---- src/Countries/Austria.php | 5 ++ src/Countries/Country.php | 2 +- ...ustrian_holidays_with_region_holidays.snap | 54 +++++++++++++++++++ tests/Countries/AustriaTest.php | 13 +++++ 5 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 tests/.pest/snapshots/Countries/AustriaTest/it_can_calculate_austrian_holidays_with_region_holidays.snap diff --git a/README.md b/README.md index 12f5b0ff3..e41ea2ffd 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ use Spatie\Holidays\Holidays; // returns an array of Belgian holidays // for the current year -$holidays = Holidays::for('be')->get(); +$holidays = Holidays::for('be')->get(); ``` ## Support us @@ -40,21 +40,22 @@ You can get all holidays for a country by using the `get` method. ```php use Spatie\Holidays\Holidays; +use Spatie\Holidays\Countries\Belgium; // returns an array of Belgian holidays // for the current year -$holidays = Holidays::for('be')->get(); +$holidays = Holidays::for(Belgium::make())->get(); ``` -Alternatively, you could also pass an instance of `Country` to the `for` method. +Alternatively, you could also pass an ISO code to the `for` method. +But region specific holidays will not be included. ```php use Spatie\Holidays\Holidays; -use Spatie\Holidays\Countries\Belgium; // returns an array of Belgian holidays // for the current year -$holidays = Holidays::for(Belgium::make())->get(); +$holidays = Holidays::for('be')->get(); ``` ### Getting holidays for a specific year @@ -87,6 +88,36 @@ use Spatie\Holidays\Holidays; Holidays::for('be')->getName('2024-01-01'); // Nieuwjaar ``` +## Contributing a new country + +If you want to add a new country, you can create a pull request. + +1. Create a new class in the `Countries` directory. It should extend the `Country` class. +2. Add a test for the new country in the `tests` directory. + +In case your country has specific rules for calculating holidays, +for example region specific holidays, you can pass this to the constructor of your country class. + +```php +$holidays = Holidays::for(Austria::make('de-bw'))->get(); + +class Austria extends Country +{ + protected function __construct( + protected ?string $region = null, + ) { + } + + protected function allHolidays(int $year): array + { + // Here you can use $this->region (or other variables) to calculate holidays + } +``` + +Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for more details. + + + ## Testing ```bash @@ -97,10 +128,6 @@ composer test Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. -## Contributing - -Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. - ## Security Vulnerabilities Please review [our security policy](../../security/policy) on how to report security vulnerabilities. diff --git a/src/Countries/Austria.php b/src/Countries/Austria.php index c8eea5d62..b03a22e52 100644 --- a/src/Countries/Austria.php +++ b/src/Countries/Austria.php @@ -6,6 +6,11 @@ class Austria extends Country { + protected function __construct( + public ?string $region = null + ) { + } + public function countryCode(): string { return 'at'; diff --git a/src/Countries/Country.php b/src/Countries/Country.php index b99262a5c..2c875aa7f 100644 --- a/src/Countries/Country.php +++ b/src/Countries/Country.php @@ -37,7 +37,7 @@ public function get(int $year): array public static function make(): static { - return new static(); + return new static(...func_get_args()); } public static function find(string $countryCode): ?Country diff --git a/tests/.pest/snapshots/Countries/AustriaTest/it_can_calculate_austrian_holidays_with_region_holidays.snap b/tests/.pest/snapshots/Countries/AustriaTest/it_can_calculate_austrian_holidays_with_region_holidays.snap new file mode 100644 index 000000000..67cda42e4 --- /dev/null +++ b/tests/.pest/snapshots/Countries/AustriaTest/it_can_calculate_austrian_holidays_with_region_holidays.snap @@ -0,0 +1,54 @@ +[ + { + "name": "Neujahr", + "date": "2024-01-01" + }, + { + "name": "Heilige Drei K\u00f6nige", + "date": "2024-01-06" + }, + { + "name": "Ostermontag", + "date": "2024-04-01" + }, + { + "name": "Staatsfeiertag", + "date": "2024-05-01" + }, + { + "name": "Christi Himmelfahrt", + "date": "2024-05-09" + }, + { + "name": "Pfingstmontag", + "date": "2024-05-20" + }, + { + "name": "Fronleichnam", + "date": "2024-05-30" + }, + { + "name": "Mari\u00e4 Himmelfahrt", + "date": "2024-08-15" + }, + { + "name": "Nationalfeiertag", + "date": "2024-10-26" + }, + { + "name": "Allerheiligen", + "date": "2024-11-01" + }, + { + "name": "Mari\u00e4 Empf\u00e4ngnis", + "date": "2024-12-08" + }, + { + "name": "Christtag", + "date": "2024-12-25" + }, + { + "name": "Stefanitag", + "date": "2024-12-26" + } +] \ No newline at end of file diff --git a/tests/Countries/AustriaTest.php b/tests/Countries/AustriaTest.php index 9018307f2..3473abef1 100644 --- a/tests/Countries/AustriaTest.php +++ b/tests/Countries/AustriaTest.php @@ -3,6 +3,7 @@ namespace Spatie\Holidays\Tests\Countries; use Carbon\CarbonImmutable; +use Spatie\Holidays\Countries\Austria; use Spatie\Holidays\Holidays; it('can calculate austrian holidays', function () { @@ -16,3 +17,15 @@ expect(formatDates($holidays))->toMatchSnapshot(); }); + +it('can calculate austrian holidays with region holidays', function () { + CarbonImmutable::setTestNowAndTimezone('2024-01-01'); + + $holidays = Holidays::for(Austria::make('bg'))->get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); +})->skip('Austria class has to be extended with regions first.');