Yasumi PHP Library

repository·develop·Indexed 22 days ago

https://github.com/azuyalabs/yasumi

A lightweight, rule-driven PHP library for retrieving names and dates of holidays and special celebrations across various countries and states. It supports custom holiday providers, ISO3166 codes, and localized names. Requires PHP 8.2+ and the ext-intl extension.

Tokens
7K
Snippets
29
Records
49
Agent score
77%

What's inside Yasumi

  1. Implementing Custom Holiday Providers

    develop

    If you are creating a custom holiday provider by extending the AbstractProvider class, note the following:

    • Method Visibility: Many methods in the provider hierarchy are protected. Do not rely on them being public if you are extending the class.
    • Namespace Resolution: When using ReflectionClass via the anotherTime() method in AbstractProvider, the library uses getName() instead of getShortName(). This ensures that custom holiday providers with full namespaces do not trigger a ProviderNotFoundException.
  2. Note on Summer/Winter Time and Historical Dates

    develop
    As of version 2.7.0, Yasumi no longer includes Summertime and Wintertime for the Netherlands and Denmark. These were removed because they cannot be reliably established for historical dates and are not considered true holidays within the context of the Yasumi library.
  3. Requirements for Yasumi: PHP and Extensions

    develop

    To use Yasumi, ensure your environment meets the following requirements:

    • PHP Version: As of version 2.7.0, PHP 7.4 support has been removed. Ensure you are using PHP 8.0 or higher (PHP 8.3 is supported).
    • Required Extension: The ext-intl PHP extension is a required dependency as of version 2.6.0.
  4. Quick Start: Retrieve and iterate through holidays

    develop

    To use Yasumi, use the Yasumi\Yasumi::create() method to instantiate a holiday provider for a specific country (or state) and year. The resulting object implements ArrayIterator, allowing you to loop through all holidays for that period. Each holiday object provides methods like getName() and format() to retrieve details.

    <?php
    
    require 'vendor/autoload.php';
    
    // Create a holiday provider for a specific country and year
    $holidays = Yasumi\Yasumi::create('USA', 2026);
    
    // Get all holidays for the year
    foreach ($holidays as $holiday) {
        echo $holiday->getName() . ': ' . $holiday->format('Y-m-d') . PHP_EOL;
    }
  5. Understand the Holiday class

    develop

    The Yasumi\Holiday class represents a specific holiday instance. It extends PHP's native \DateTime, meaning you can use it anywhere a DateTime object is expected (e.g., for date arithmetic or formatting). It carries additional metadata such as the holiday's unique key, its type (official, observance, etc.), and localized names via a translation system.

    use Yasumi\Holiday;
    
    // A Holiday instance behaves like a DateTime object
    $holiday = new Holiday('newYearsDay', [], new \DateTime('2026-01-01'));
    echo $holiday->format('Y-m-d'); // 2026-01-01
  6. Load translations from a directory

    develop

    To load translations from a file system, use loadTranslations(string $directoryPath).

    Requirements for translation files:

    1. Files must have a .php extension.
    2. Each file must return an associative array where the keys are valid locales (matching the locales provided to the Translations constructor) and the values are the translation strings.
    3. The filename (excluding the .php extension) is used as the holiday identifier (the $key).

    Example file structure: /path/to/translations/christmas.php contains:

    <?php
    return [
        'en' => 'Christmas',
        'de' => 'Weihnachten',
    ];

    Errors:

    • Throws \InvalidArgumentException if the directory does not exist.
    • Throws Yasumi\Exception\UnknownLocaleException if a file contains a locale not present in the initial $availableLocales list.
    $translations = new Yasumi\Translations(['en', 'de']);
    $translations->loadTranslations('/path/to/translations');
  7. Retrieve specific holidays and check existence

    develop

    You can retrieve a specific holiday from a provider using the getHoliday() method by passing its identifier (e.g., 'independenceDay'). If the holiday exists, it returns the holiday object; otherwise, it returns null. This allows you to easily check if a specific date is recognized as a holiday.

    <?php
    
    require 'vendor/autoload.php';
    
    $holidays = Yasumi\Yasumi::create('USA', 2026);
    
    // Get a specific holiday
    $independenceDay = $holidays->getHoliday('independenceDay');
    if ($independenceDay !== null) {
        echo $independenceDay->getName() . ' is on ' . $independenceDay->format('F j, Y') . PHP_EOL;
    }
    
    // Check if a specific holiday identifier exists
    $newYearsDay = $holidays->getHoliday('newYearsDay');
    if ($newYearsDay !== null) {
        echo 'New Year\'s Day is a holiday!' . PHP_EOL;
    }
  8. Retrieve external holiday sources from providers

    develop
    As of version 2.5.0, all holiday providers include a method that returns a list of external sources (such as websites, books, or scientific papers) used to determine the calculation logic for that provider's holidays. This is useful for verifying the legal or official basis of a calculated holiday.