예제 #1
0
        public static PhoneNumber Create(CountryInfo country, string phoneNumberWithoutPrefix)
        {
            if (country == null)
            {
                throw new ArgumentNullException(nameof(country), $"Country info cannot be null when creating a phone number.");
            }

            if (string.IsNullOrWhiteSpace(phoneNumberWithoutPrefix))
            {
                throw new PhoneNumberNotValidException(nameof(phoneNumberWithoutPrefix), phoneNumberWithoutPrefix);
            }

            phoneNumberWithoutPrefix = phoneNumberWithoutPrefix.Trim();

            if (!long.TryParse(phoneNumberWithoutPrefix, out var _))
            {
                throw new PhoneNumberNotValidException(nameof(phoneNumberWithoutPrefix), phoneNumberWithoutPrefix);
            }

            return(new PhoneNumber(country, phoneNumberWithoutPrefix));
        }
예제 #2
0
        public static CountryInfo CreateFromPhoneNumber(long phoneNumberWithCountryPrefix, out string phoneNumberWithoutCountryPrefix)
        {
            if (phoneNumberWithCountryPrefix < 1000)
            {
                throw new PhoneNumberNotValidException(nameof(phoneNumberWithCountryPrefix), phoneNumberWithCountryPrefix);
            }

            var phoneNumberPrefixString = phoneNumberWithCountryPrefix.ToString(CultureInfo.InvariantCulture).Substring(0, 4);

            do
            {
                var phoneNumberPrefix = int.Parse(phoneNumberPrefixString);
                var validCountries    = CountryInfos.All.Where(x => x.PhonePrefix == phoneNumberPrefix).ToArray();

                CountryInfo result = null;
                if (validCountries.Length == 1)
                {
                    result = validCountries[0];
                }
                if (validCountries.Length > 1 && validCountries.All(x => x.PhonePrefix == CountryInfos.NORTHAMERICA.PhonePrefix))
                {
                    result = CountryInfos.NORTHAMERICA;
                }

                if (result != null)
                {
                    phoneNumberWithoutCountryPrefix = phoneNumberWithCountryPrefix.ToString(CultureInfo.InvariantCulture).Substring(phoneNumberPrefixString.Length);
                    if (string.IsNullOrWhiteSpace(phoneNumberWithoutCountryPrefix))
                    {
                        throw new PhoneNumberNotValidException(nameof(phoneNumberWithCountryPrefix), phoneNumberWithCountryPrefix);
                    }
                    return(result);
                }

                phoneNumberPrefixString = phoneNumberPrefixString.Substring(0, phoneNumberPrefixString.Length - 1);
            } while (phoneNumberPrefixString.Length > 0);

            throw new PhoneNumberNotValidException(nameof(phoneNumberWithCountryPrefix), phoneNumberWithCountryPrefix);
        }
예제 #3
0
 private PhoneNumber(CountryInfo country, string phoneNumberWithoutPrefix)
 {
     Country = country;
     PhoneNumberWithoutPrefix = phoneNumberWithoutPrefix;
 }