/**
* Create a new holiday provider instance.
*
* 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.
*
* @param string $class holiday provider name
* @param int $year year for which the country provider needs to be created. Year needs to be a valid integer
* between 1000 and 9999.
* @param string $locale The locale to use. If empty we'll use the default locale (en_US)
*
* @throws RuntimeException If no such holiday provider is found
* @throws InvalidArgumentException if the year parameter is not between 1000 and 9999
* @throws UnknownLocaleException if the locale parameter is invalid
* @throws InvalidArgumentException if the holiday provider for the given country does not exist
*
* @return 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);
}