Calendar Holidays
Available versions:
Installation
composer require aeon-php/calendar-holidays
Description
This library provides simple but really flexible abstraction representing holidays of any type.
Abstraction
<?php
use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\Holidays\HolidayName;
interface Holidays
{
public function isHoliday(Day $day) : bool;
/**
* @return array
*/
public function holidaysAt(Day $day) : array;
}
final class Holiday
{
public function __construct(Day $day, HolidayName $name) {}
public function day() : Day {}
public function name(?string $locale = null) : string {}
}
Holiday Provider - Chain
<?php
use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendar\CountryCodes;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendarRegionalHolidays;
use Aeon\Calendar\Gregorian\Holidays\HolidaysChain;
$holidays = new HolidaysChain(
new GoogleCalendarRegionalHolidays(CountryCodes::US),
new CustomHolidays()
);
if ($holidays->isHoliday(Day::fromString('2020-01-01'))) {
echo $holidays->holidaysAt(Day::fromString('2020-01-01'))[0]->name(); // New Year's Day
}
Heads Up it returns holidays from all chained implementations so results might be duplicated.
Holiday Provider - Google Calendar Regional Holidays
<?php
use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendar\CountryCodes;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendarRegionalHolidays;
$holidays = new GoogleCalendarRegionalHolidays(CountryCodes::US);
if ($holidays->isHoliday(Day::fromString('2020-01-01'))) {
echo $holidays->holidaysAt(Day::fromString('2020-01-01'))[0]->name(); // New Year's Day
}
GoogleCalendarRegionalHolidays reads data from static json commited to the repository and it only reads the file matching country code passed to the constructor.
Lazy initialization - when GoogleCalendarRegionalHolidays
is initialized it does not read the json files yet,
it only validates that country code is valid. It goes to the file first time you use holidaysAt
or isHoliday
and
even then the file is memoized,
and stored as GoogleCalendarRegionalHolidays
instance state so another usage of
any above method will not trigger parsing json files again.
Holiday Provider - Empty
<?php
use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\Holidays\EmptyHolidays;
$holidays = new EmptyHolidays();
if ($holidays->isHoliday(Day::fromString('2020-01-01'))) {
// code dead as your brain after working without holidays
}
This implementation does not provide any holidays, use it when you are a robot that works whole year with any break.