Skip to content

Commit

Permalink
Add logic to check if HolidayProvider available
Browse files Browse the repository at this point in the history
After NoHolidaysHolidayProvider is internal class need a new function to check
  • Loading branch information
tinohager committed Mar 8, 2024
1 parent 052a66f commit 4cfa383
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Nager.Date/HolidaySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public static IHolidayProvider GetHolidayProvider(string countryCode)
throw new ArgumentException(string.Format(CountryCodeParsingError, countryCode));
}

return GetHolidayProvider(parsedCountryCode);
TryGetHolidayProvider(parsedCountryCode, out var holidayProvider);
return holidayProvider;
}

/// <summary>
Expand All @@ -193,6 +194,18 @@ public static IHolidayProvider GetHolidayProvider(string countryCode)
/// <param name="countryCode">Country Code (ISO 3166-1 ALPHA-2)</param>
/// <returns>Holiday provider for given country</returns>
public static IHolidayProvider GetHolidayProvider(CountryCode countryCode)
{
TryGetHolidayProvider(countryCode, out var holidayProvider);
return holidayProvider;
}

/// <summary>
/// Try get the holiday provider for the specified country
/// </summary>
/// <param name="countryCode"></param>
/// <param name="holidayProvider"></param>
/// <returns></returns>
public static bool TryGetHolidayProvider(CountryCode countryCode, out IHolidayProvider holidayProvider)
{
if (_licenseValid is null)
{
Expand All @@ -201,15 +214,18 @@ public static IHolidayProvider GetHolidayProvider(CountryCode countryCode)

if (!_licenseValid.Value)
{
return NoHolidaysHolidayProvider.Instance;
holidayProvider = NoHolidaysHolidayProvider.Instance;
return false;
}

if (_holidaysProviders.TryGetValue(countryCode, out var provider))
{
return provider.Value;
holidayProvider = provider.Value;
return true;
}

return NoHolidaysHolidayProvider.Instance;
holidayProvider = NoHolidaysHolidayProvider.Instance;
return false;
}

#region Holidays for a given year
Expand Down

0 comments on commit 4cfa383

Please sign in to comment.