Skip to content

Commit

Permalink
Merge branch 'spatie:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaliskan authored Jan 18, 2024
2 parents 609e391 + 40e12a4 commit 430cdaf
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 46 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Fix PHP code style issues

on:
push:
paths:
- '**.php'
on: [push]

permissions:
contents: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PHPStan

on: [push]
on: [push, pull_request]

jobs:
phpstan:
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: Tests

on:
push:
paths:
- '**.php'
- '.github/workflows/run-tests-pest.yml'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
on: [push, pull_request]

jobs:
test:
Expand Down
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,21 +40,21 @@ 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.

```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
Expand Down Expand Up @@ -87,6 +87,40 @@ 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.
3. Run the tests so a snapshot gets created.
4. Verify the result in the newly created snapshot is correct.

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(region: 'de-bw'))->get();
```

The value, `de-bw`, will be passed to the region parameter of the contructor of a country.

```php
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
Expand All @@ -97,10 +131,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.
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"ext-calendar": "*"
},
"require-dev": {
"laravel/prompts": "^0.1.15",
"pestphp/pest": "^2.31",
"phpstan/phpstan": "^1.10.56",
"spatie/ray": "^1.40.1"
"spatie/ray": "^1.40.1",
"symfony/var-dumper": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 8 additions & 8 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
parameters:
ignoreErrors:
-
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Belgium\\:\\:allHolidays\\(\\) should return array\\<string, Carbon\\\\CarbonImmutable\\> but returns array\\<string, Carbon\\\\CarbonImmutable\\|string\\>\\.$#"
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: src/Countries/Belgium.php
path: src/Countries/Brazil.php

-
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/Countries/Country.php

-
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Country\\:\\:get\\(\\) should return array\\<string, Carbon\\\\CarbonImmutable\\> but returns array\\<string, Carbon\\\\CarbonImmutable\\|false\\>\\.$#"
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Country\\:\\:get\\(\\) should return array\\<string, Carbon\\\\CarbonImmutable\\|string\\> but returns array\\<string, Carbon\\\\CarbonImmutable\\|false\\>\\.$#"
count: 1
path: src/Countries/Country.php

Expand All @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/Countries/Country.php

-
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Hungary\\:\\:allHolidays\\(\\) should return array\\<string, Carbon\\\\CarbonImmutable\\> but returns array\\<string, Carbon\\\\CarbonImmutable\\|string\\>\\.$#"
count: 1
path: src/Countries/Hungary.php

-
message: "#^Cannot call method isSunday\\(\\) on Carbon\\\\CarbonImmutable\\|false\\.$#"
count: 1
Expand All @@ -44,3 +39,8 @@ parameters:
message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Carbon\\\\CarbonImmutable\\)\\: mixed\\)\\|null, Closure\\(string\\)\\: non\\-falsy\\-string given\\.$#"
count: 1
path: src/Holidays.php

-
message: "#^Property Spatie\\\\Holidays\\\\Holidays\\:\\:\\$holidays \\(array\\<string, Carbon\\\\CarbonImmutable\\>\\) does not accept array\\<string, Carbon\\\\CarbonImmutable\\|string\\>\\.$#"
count: 1
path: src/Holidays.php
31 changes: 31 additions & 0 deletions playground.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Holidays;

use function Laravel\Prompts\select;

require __DIR__.'/vendor/autoload.php';

$countries = [];

foreach (glob(__DIR__.'/src/Countries/*.php') as $filename) { // @phpstan-ignore-line
$countryClass = '\\Spatie\\Holidays\\Countries\\'.basename($filename, '.php');

if (basename($filename) === 'Country.php') {
continue;
}

$countries[$countryClass] = str_replace('\\Spatie\\Holidays\\Countries\\', '', $countryClass);
}

/** @var Country $class */
$class = select('Select a country', $countries);

$result = collect(Holidays::for($class::make())->get())
->map(fn (array $holiday) => [
'name' => $holiday['name'],
'date' => $holiday['date']->format('Y-m-d'), // @phpstan-ignore-line
])->toArray();

dd($result);
1 change: 0 additions & 1 deletion src/Countries/Andorra.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function countryCode(): string
return 'ad';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
8 changes: 6 additions & 2 deletions src/Countries/Austria.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

class Austria extends Country
{
protected function __construct(
public ?string $region = null
) {
}

public function countryCode(): string
{
return 'at';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge([
Expand All @@ -34,7 +38,7 @@ protected function variableHolidays(int $year): array
->setTimezone('Europe/Vienna');

return [
'Ostermontag' => $easter->addDay(1),
'Ostermontag' => $easter->addDay(),
'Christi Himmelfahrt' => $easter->addDays(39),
'Pfingstmontag' => $easter->addDays(50),
'Fronleichnam' => $easter->addDays(60),
Expand Down
1 change: 0 additions & 1 deletion src/Countries/Belgium.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function countryCode(): string
return 'be';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
3 changes: 2 additions & 1 deletion src/Countries/Brazil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public function countryCode(): string
return 'br';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
throw new \Exception('Not implemented yet.');

return array_merge([
'Dia de Ano Novo' => '01-01',
'Dia de Tiradentes' => '04-21',
Expand Down
4 changes: 2 additions & 2 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract public function countryCode(): string;
/** @return array<string, string|CarbonImmutable> */
abstract protected function allHolidays(int $year): array;

/** @return array<string, CarbonImmutable> */
/** @return array<string, CarbonImmutable|string> */
public function get(int $year): array
{
$this->ensureYearCanBeCalculated($year);
Expand All @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/Countries/Denmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

class Denmark extends Country
{
public function countryCode(): string
{
return 'da';
}

protected function allHolidays(int $year): array
{
return array_merge([
'Nytår' => '01-01',
'Juleaften' => '12-24',
'Juledag' => '12-25',
'Anden Juledag' => '12-26',
], $this->variableHolidays($year));
}

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

$holidays = [
'Påskedag' => $easter->addDay(),
'Skærtorsdag' => $easter->subDays(3),
'Langfredag' => $easter->subDays(2),
'Anden Påskedag' => $easter->addDays(2),
'Kristi Himmelfartsdag' => $easter->addDays(39),
'Pinse' => $easter->addDays(49),
'Anden Pinsedag' => $easter->addDays(50),
];

if ($year < 2024) {
$holidays['Store Bededag'] = $easter->addDays(26);
}

return $holidays;
}
}
1 change: 0 additions & 1 deletion src/Countries/Hungary.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function countryCode(): string
return 'hu';
}

/** @return array<string, CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
2 changes: 0 additions & 2 deletions src/Countries/Netherlands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public function countryCode(): string
return 'nl';
}

/** @return array<string, string|CarbonImmutable> */
protected function allHolidays(int $year): array
{
return array_merge([
'Nieuwjaarsdag' => '01-01',
'Bevrijdingsdag' => '05-05',
'1e Kerstdag' => '12-25',
'2e Kerstdag' => '12-26',
'Oudejaarsdag' => '12-31',
], $this->variableHolidays($year));
}

Expand Down
42 changes: 42 additions & 0 deletions src/Countries/Portugal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;

class Portugal extends Country
{
public function countryCode(): string
{
return 'pt';
}

protected function allHolidays(int $year): array
{
return array_merge([
'Dia de Ano Novo' => '01-01',
'Dia da Liberdade' => '04-25',
'Dia do Trabalhador' => '05-01',
'Dia de Portugal' => '06-10',
'Assunção da Nossa Senhora' => '08-15',
'Implantação da República' => '10-05',
'Dia de Todos os Santos' => '11-01',
'Restauração da Independência' => '12-01',
'Imaculada Conceição' => '12-08',
'Natal' => '12-25',
], $this->variableHolidays($year));
}

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

return [
'Páscoa' => $easter,
'Sexta-feira Santa' => $easter->subDays(2),
'Corpo de Deus' => $easter->addDays(60),
];
}
}
Loading

0 comments on commit 430cdaf

Please sign in to comment.