Skip to content

Commit

Permalink
add getInRange function
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Katerinak committed Jan 20, 2024
1 parent 60b73ed commit 447203b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ use Spatie\Holidays\Holidays;
$holidays = Holidays::for(country: 'be', year: 2024))->get();
```

### Getting holidays between two dates

You can also get all holidays between two dates (inclusive).

```php
use Spatie\Holidays\Holidays;

$holidays = Holidays::for('be')->getInRange('2023-06-01', '2024-05-31');
```

### Determining if a date is a holiday

If you need to see if a date is a holiday, you can use the `isHoliday` method.
Expand Down
35 changes: 35 additions & 0 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,41 @@ public function get(int $year): array
return $allHolidays;
}

/** @return array<string, string> date => name */
public function getInRange(?CarbonImmutable $from, ?CarbonImmutable $to): array
{
$from ??= CarbonImmutable::now()->startOfYear();
$to ??= CarbonImmutable::now()->endOfYear();

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

$allHolidays = [];

for ($year = $from->year; $year <= $to->year; $year++) {
$yearHolidays = $this->get($year);
/**
* @var string $name
* @var CarbonImmutable $date
*/
foreach ($yearHolidays as $name => $date) {
if ($date->between($from, $to)) {
$allHolidays[] = ['date' => $date, 'name' => $name];
}
}
}

usort($allHolidays, static fn (array $a, array $b) => $a['date'] <=> $b['date']);

$mappedHolidays = [];
/** @var array{date: CarbonImmutable, name: string} $holiday */
foreach ($allHolidays as $holiday) {
$mappedHolidays[$holiday['date']->toDateString()] = $holiday['name'];
}

return $mappedHolidays;
}

public static function make(): static
{
return new static(...func_get_args());
Expand Down
39 changes: 39 additions & 0 deletions src/Holidays.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Holidays
protected function __construct(
protected Country $country,
protected int $year,
protected ?CarbonImmutable $from = null,
protected ?CarbonImmutable $to = null
) {
}

Expand All @@ -39,6 +41,43 @@ public function get(Country|string|null $country = null, ?int $year = null): arr
->toArray();
}

/**
* getInRange method allows you to pick holidays in a range of dates,
* - dates are inclusive.
* - dates are swappable, lower date could be passed as second argument.
* - dates could be a CarbonInterface or a string.
* - acceptable strings formats are 'Y-m-d' or 'Y-m' or 'Y'
* - if passed string is 'Y-m' or 'Y' it will be converted to first(from) / last{to} day of the month(from) / year(to)
* E.g. to retrieve all holidays in between
* - 2020-01-01 and 2024-12-31, you could use: getInRange('2020-01-01', '2024-12-31'), getInRange('2020-01', '2024-12') or getInRange('2020', '2024')
* - 2024-06-01 and 2025-05-30, you could use: getInRange('2024-06-01', '2025-05-30'), getInRange('2024-06', '2025-05')
* @return array<string, string> date => name
*/
public function getInRange(CarbonInterface|string $from, CarbonInterface|string $to): array
{
if (! $from instanceof CarbonImmutable) {
$from = match (strlen($from)) {
4 => CarbonImmutable::parse($from . '-01-01'),
7 => CarbonImmutable::parse($from . '-01'),
default => CarbonImmutable::parse($from),
};
}

if (! $to instanceof CarbonImmutable) {
$to = match (strlen($to)) {
4 => CarbonImmutable::parse($to . '-12-31'),
7 => CarbonImmutable::parse($to)->endOfMonth(),
default => CarbonImmutable::parse($to),
};
}

if ($from->gt($to)) {
[$from, $to] = [$to, $from];
}

return $this->country->getInRange($from, $to);
}

public function isHoliday(CarbonInterface|string $date, Country|string|null $country = null): bool
{
if (! $date instanceof CarbonImmutable) {
Expand Down
16 changes: 16 additions & 0 deletions tests/HolidaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@
$result = Holidays::for('be')->getName(CarbonImmutable::parse('2024-01-02'));
expect($result)->toBeNull();
});

it('can get all holidays between two dates', function (string|CarbonImmutable $from, string|CarbonImmutable $to, int $expectedCount, string $firstName, string $lastName) {
$holidays = Holidays::for('be')->getInRange($from, $to);

expect($holidays)->toBeArray();
expect($holidays)->toHaveCount($expectedCount);
expect(reset($holidays))->toBe($firstName);
expect(end($holidays))->toBe($lastName);
})->with([
['2020', '2024', 50, 'Nieuwjaar', 'Kerstmis'],
['2024-06', '2025-05', 9, 'Nationale Feestdag', 'OLH Hemelvaart'],
['2023-06-01', '2024-05-30', 10, 'Nationale Feestdag', 'Pinkstermaandag'],
['2024-05-30', '2023-06-01', 10, 'Nationale Feestdag', 'Pinkstermaandag'],
[CarbonImmutable::parse('2023-06-01'), CarbonImmutable::parse('2024-05-30'), 10, 'Nationale Feestdag', 'Pinkstermaandag'],
[CarbonImmutable::parse('2023-06-01'), '2024-05', 10, 'Nationale Feestdag', 'Pinkstermaandag'],
]);

0 comments on commit 447203b

Please sign in to comment.