| | 1 | | using Common.Core.Models; |
| | 2 | |
|
| | 3 | | namespace Common.Core.Classes; |
| | 4 | |
|
| | 5 | | /// <summary>Contains static data used for Addresses.</summary> |
| | 6 | | public abstract class AddressFactoryBase |
| | 7 | | { |
| | 8 | | /// <summary>Standard string comparison for ordinal ignore case.</summary> |
| 1 | 9 | | protected static readonly StringComparison sCompare = StringComparison.OrdinalIgnoreCase; |
| | 10 | |
|
| | 11 | | private static IList<Province>? _provinces; |
| | 12 | | private static IList<CountryCode>? _countries; |
| | 13 | | private static CountryCode? _default; |
| | 14 | |
|
| | 15 | | #region Public Properties |
| | 16 | |
|
| | 17 | | /// <summary>Indicates whether to use Alpha-2 ISO-3166 Country codes.<br/> |
| | 18 | | /// The default is to use Alpha-3 codes.</summary> |
| | 19 | | /// <remarks>If required, true must be passed in the constructor of a derived class.</remarks> |
| 55 | 20 | | public static bool UseAlpha2 { get; protected set; } |
| | 21 | |
|
| | 22 | | /// <summary>The ISO-3166 Country code of the Address data.<br/> |
| | 23 | | /// The default is <c>USA</c>.</summary> |
| | 24 | | /// <remarks>If required, a different Country code must be passed in the constructor of a derived class.<br/> |
| | 25 | | /// If using Alpha2 then pass the alpha-2 code, otherwise pass the alpha-3 code.</remarks> |
| | 26 | | public static string DefaultCountry |
| | 27 | | { |
| 48 | 28 | | get { return _default is null ? UseAlpha2 ? "US" : "USA" : _default.Code; } |
| | 29 | | protected set |
| 11 | 30 | | { |
| 11 | 31 | | if( _countries is not null && !string.IsNullOrWhiteSpace( value ) ) |
| 10 | 32 | | { |
| 30 | 33 | | _default = _countries.FirstOrDefault( x => x.Code.Equals( value, sCompare ) ); |
| 10 | 34 | | } |
| 11 | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary>Gets a sorted list of ISO-3166 Countries.</summary> |
| | 39 | | public static IList<CountryCode> Countries |
| | 40 | | { |
| 3 | 41 | | get => _countries is null ? new List<CountryCode>() : _countries; |
| 14 | 42 | | protected set => _countries = value; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary>Gets a list of Provinces.</summary> |
| | 46 | | public static IList<Province> Provinces |
| | 47 | | { |
| 7 | 48 | | get => _provinces is null ? new List<Province>() : _provinces; |
| 11 | 49 | | protected set => _provinces = value; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary>Gets a list of Postcodes.</summary> |
| 25 | 53 | | public static IList<Postcode> Postcodes { get; set; } = new List<Postcode>(); |
| | 54 | |
|
| | 55 | | /// <summary>Gets the total number of Postcodes available.</summary> |
| 11 | 56 | | public static int PostcodeCount { get; protected set; } |
| | 57 | |
|
| | 58 | | #endregion |
| | 59 | |
|
| | 60 | | #region Protected Methods |
| | 61 | |
|
| | 62 | | /// <summary>Sets the list of Countries.</summary> |
| | 63 | | /// <param name="list">Collection of ISO-3166 Countries.</param> |
| | 64 | | protected static void SetCountries( IList<ISOCountry> list ) |
| 14 | 65 | | { |
| 42 | 66 | | Countries = list.OrderBy( c => c.Name ) |
| 42 | 67 | | .Select( c => new CountryCode( UseAlpha2 ? c.Alpha2 : c.Alpha3, c.Name ) ).ToList(); |
| 14 | 68 | | } |
| | 69 | |
|
| | 70 | | #endregion |
| | 71 | |
|
| | 72 | | #region Public Methods |
| | 73 | |
|
| | 74 | | /// <summary>Checks whether a Country code is valid.</summary> |
| | 75 | | /// <param name="code">ISO-3166 Country code.</param> |
| | 76 | | /// <returns><see langword="true"/> if the Country code was found.</returns> |
| | 77 | | public static bool CheckCountryCode( string? code ) |
| 4 | 78 | | { |
| 10 | 79 | | if( code is null || code.Length != ( UseAlpha2 ? 2 : 3 ) ) { return false; } |
| 3 | 80 | | CountryCode? country = Countries.FirstOrDefault( x => x.Code.Equals( code, sCompare ) ); |
| 1 | 81 | | return country is not null; |
| 4 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary>Checks whether a Province code is valid.</summary> |
| | 85 | | /// <param name="code">Province code.</param> |
| | 86 | | /// <returns><see langword="true"/> if the Province code was found.</returns> |
| | 87 | | public static bool CheckProvinceCode( string? code ) |
| 4 | 88 | | { |
| 6 | 89 | | if( code is not null && code.Length > 10 ) { return false; } |
| 3 | 90 | | Province? province = Provinces.FirstOrDefault( x => |
| 5 | 91 | | { |
| 5 | 92 | | return code == x.Code; |
| 8 | 93 | | } ); |
| 3 | 94 | | return province is not null; |
| 4 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary>Gets the name for a Province code.</summary> |
| | 98 | | /// <param name="code">Province code.</param> |
| | 99 | | /// <returns>An empty string is returned if the Province code was not found.</returns> |
| | 100 | | public static string GetProvinceName( string? code ) |
| 5 | 101 | | { |
| 9 | 102 | | if( code is not null && code.Length > 10 ) { return string.Empty; } |
| 3 | 103 | | Province? province = Provinces.FirstOrDefault( x => |
| 2 | 104 | | { |
| 2 | 105 | | return code == x.Code; |
| 5 | 106 | | } ); |
| 3 | 107 | | return province is not null ? province.Name : string.Empty; |
| 5 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary>Gets the information for a Postcode.</summary> |
| | 111 | | /// <param name="code">Postal Service code.</param> |
| | 112 | | /// <returns><see langword="null"/> is returned if the Postcode is not found.</returns> |
| | 113 | | /// <remarks>If the postcodes are cached and the code has not been referenced this will return null.</remarks> |
| | 114 | | public static Postcode? GetPostcode( string? code ) |
| 3 | 115 | | { |
| 7 | 116 | | if( code is null || ( DefaultCountry.StartsWith( "US" ) & code.Length < 5 ) ) { return null; } |
| 4 | 117 | | if( code.Length > 5 & DefaultCountry.StartsWith( "US" ) ) { code = code[..5]; } |
| 3 | 118 | | return Postcodes.FirstOrDefault( z => z.Code == code ); |
| 3 | 119 | | } |
| | 120 | |
|
| | 121 | | #endregion |
| | 122 | | } |