/// <summary> Initializes the country meta data retriever. </summary> /// <remarks> Sander.struijk, 25.02.2014. </remarks> public static async Task InitializeAsync() { if (_hasBeenInitialized || _inProgress) { return; } _inProgress = true; await ReadTextFileAsync("CountryCodes.txt").ContinueWith(task => { foreach (var line in task.Result) { if (line.StartsWith("#") || string.IsNullOrEmpty(line)) { continue; } var countryData = line.Split('|'); var countryMetaData = new CountryMetaData(countryData[0], countryData[1], countryData[2]); for (var i = 3; i < countryData.Length; i++) { var key = countryData[i].ToLower(); if (CountryMetaDatas.ContainsKey(key)) { continue; } CountryMetaDatas.Add(key, countryMetaData); } } }).ContinueWith(task => { _hasBeenInitialized = true; _inProgress = false; }); }
/// <summary> Initializes the country meta data retriever. </summary> /// <remarks> Sander.struijk, 25.02.2014. </remarks> public static void Initialize() { if (_hasBeenInitialized) { return; } var readCountryCodesFile = ReadTextFile("CountryCodes.txt"); foreach (var line in readCountryCodesFile) { if (line.StartsWith("#") || string.IsNullOrEmpty(line)) { continue; } var countryData = line.Split('|'); var countryMetaData = new CountryMetaData(countryData[0], countryData[1], countryData[2]); for (var i = 3; i < countryData.Length; i++) { var key = countryData[i].ToLower(); if (CountryMetaDatas.ContainsKey(key)) { continue; } CountryMetaDatas.Add(key, countryMetaData); } } _hasBeenInitialized = true; }
/// <summary> Constructor. </summary> /// <remarks> Sander.struijk, 26.02.2014. </remarks> /// <param name="countryMetaData"> Information describing the country meta data. </param> /// <param name="unformattedPhoneNumber"> The unformatted phone number. </param> public PhoneNumber(CountryMetaData countryMetaData, string unformattedPhoneNumber) { NumberGroups = new List <string>(); var cleanedPhoneNumber = CleanPhoneNumber(unformattedPhoneNumber); InitializePhoneNumberFields(countryMetaData, cleanedPhoneNumber); InitializePhoneNumberFormatNumberGroups(countryMetaData); }
/// <summary> Initializes the phone number format number groups. </summary> /// <remarks> Sander.struijk, 26.02.2014. </remarks> /// <param name="countryMetaData"> Information describing the country meta data. </param> private void InitializePhoneNumberFormatNumberGroups(CountryMetaData countryMetaData) { var startIndex = 0; foreach (var groupCount in countryMetaData.PhoneNumberFormat) { var numberGroup = Number.Substring(startIndex, groupCount); startIndex += groupCount; NumberGroups.Add(numberGroup); } }
/// <summary> Initializes the phone number fields. </summary> /// <remarks> Sander.struijk, 26.02.2014. </remarks> /// <param name="countryMetaData"> Information describing the country meta data. </param> /// <param name="cleanedPhoneNumber"> The cleaned phone number. </param> private void InitializePhoneNumberFields(CountryMetaData countryMetaData, string cleanedPhoneNumber) { var countryCode = cleanedPhoneNumber.Substring(0, cleanedPhoneNumber.Length - countryMetaData.PhoneNumberDigitCount); var phoneNumber = cleanedPhoneNumber.Substring(cleanedPhoneNumber.Length - countryMetaData.PhoneNumberDigitCount); if (PhoneNumberAlreadyHasCountryCode(countryMetaData, cleanedPhoneNumber)) { CountryCode = !string.IsNullOrEmpty(countryMetaData.CountryCode) ? countryMetaData.CountryCode : countryCode; Number = phoneNumber; } else if (CountryMetaDataHasCountryCode(countryMetaData)) { CountryCode = countryMetaData.CountryCode; Number = cleanedPhoneNumber; } else { CountryCode = string.Empty; Number = cleanedPhoneNumber; } }
/// <summary> Phone number already has country code. </summary> /// <remarks> Sander.struijk, 26.02.2014. </remarks> /// <param name="countryMetaData"> Information describing the country meta data. </param> /// <param name="cleanedPhoneNumber"> The cleaned phone number. </param> /// <returns> true if it succeeds, false if it fails. </returns> private static bool PhoneNumberAlreadyHasCountryCode(CountryMetaData countryMetaData, string cleanedPhoneNumber) { return(countryMetaData.PhoneNumberDigitCount > 0 && countryMetaData.PhoneNumberDigitCount < cleanedPhoneNumber.Length); }
/// <summary> Country meta data has country code. </summary> /// <remarks> Sander.struijk, 26.02.2014. </remarks> /// <param name="countryMetaData"> Information describing the country meta data. </param> /// <returns> true if it succeeds, false if it fails. </returns> private static bool CountryMetaDataHasCountryCode(CountryMetaData countryMetaData) { return(!string.IsNullOrEmpty(countryMetaData.CountryCode)); }