Yasumi\Yasumi::create PHP Method

create() public static method

A new holiday provider instance can be created using this function. You can use one of the providers included already with Yasumi, or your own provider by giving the name of your class in the first parameter. Your provider class needs to implement the 'ProviderInterface' class.
public static create ( string $class, integer $year = null, string $locale = self::DEFAULT_LOCALE ) : AbstractProvider
$class string holiday provider name
$year integer year for which the country provider needs to be created. Year needs to be a valid integer between 1000 and 9999.
$locale string The locale to use. If empty we'll use the default locale (en_US)
return Yasumi\Provider\AbstractProvider An instance of class $class is created and returned
    public static function create($class, $year = null, $locale = self::DEFAULT_LOCALE)
    {
        // Find and return holiday provider instance
        $providerClass = sprintf('Yasumi\\Provider\\%s', str_replace('/', '\\', $class));
        if (class_exists($class) && (new ReflectionClass($class))->implementsInterface(ProviderInterface::class)) {
            $providerClass = $class;
        }
        if (!class_exists($providerClass) || $class === 'AbstractProvider') {
            throw new InvalidArgumentException(sprintf('Unable to find holiday provider "%s".', $class));
        }
        // Assert year input
        if ($year < 1000 || $year > 9999) {
            throw new InvalidArgumentException(sprintf('Year needs to be between 1000 and 9999 (%s given).', $year));
        }
        // Load internal locales variable
        if (!isset(static::$locales)) {
            static::$locales = self::getAvailableLocales();
        }
        // Load internal translations variable
        if (!isset(static::$globalTranslations)) {
            static::$globalTranslations = new Translations(static::$locales);
            static::$globalTranslations->loadTranslations(__DIR__ . '/data/translations');
        }
        // Assert locale input
        if (!in_array($locale, static::$locales)) {
            throw new UnknownLocaleException(sprintf('Locale "%s" is not a valid locale.', $locale));
        }
        return new $providerClass($year, $locale, self::$globalTranslations);
    }

Usage Example

Esempio n. 1
0
 /**
  * Tests name of Veterans Day after 1954. Veterans Day was named 'Armistice Day' before 1954.
  */
 public function testVeteransDayNameAfter1954()
 {
     $year = $this->generateRandomYear(1954);
     $holidays = Yasumi::create(self::REGION, $year);
     $holiday = $holidays->getHoliday(self::HOLIDAY);
     $this->assertEquals('Veterans Day', $holiday->getName());
 }
All Usage Examples Of Yasumi\Yasumi::create