C# International Mobile Phone Number Class Encapsulation - Use nuget Global Phone to parse international mobile phone numbers

黄舟
Release: 2017-02-28 11:16:33
Original
1644 people have browsed it

1. package-install GlobalPhone

2. Phone.csListen

Listen

 public class Phone
    {
        public string CountryCode { get; set; }
        public string InternationalNumber { get; set; }
        public string NationalNumber { get; set; }
        public string Territory { get; set; }
        public bool IsValid { get; set; }


        public Phone()
        {


        }


        private Phone(global::GlobalPhone.Number number)
            : this()
        {
            CountryCode = number.CountryCode;
            InternationalNumber = number.InternationalString;
            NationalNumber = number.NationalFormat;
            Territory = number.Territory.Name;
            IsValid = number.IsValid;
        }


        private static bool __libraryLoaded;
        private static global::GlobalPhone.Database __databse;
        private static void LoadStaticContent()
        {
            if (!__libraryLoaded)
            {
                try
                {
                    var assembly = Assembly.GetExecutingAssembly();
                    var dataStream = assembly.GetManifestResourceStream("Common.GlobalPhone.data.json");
                    if (dataStream != null)
                    {
                        var reader = new StreamReader(dataStream);
                        var text = reader.ReadToEnd();
                        global::GlobalPhone.GlobalPhone.DbText = text;


                        __databse = global::GlobalPhone.Database.Load(text);
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }


                __libraryLoaded = true;
            }
        }




        private static readonly object __globalPhoneLock = new object();
        private static global::GlobalPhone.Region TrnaslateRegion(String countryCode)
        {
            if (string.IsNullOrEmpty(countryCode))
            {
                return null;
            }


            lock (__globalPhoneLock)
            {
                return __databse.TryGetRegion(countryCode);
            }
        }


        private static global::GlobalPhone.Territory TranslateTerritory(String territoryName)
        {
            if (string.IsNullOrEmpty(territoryName))
            {
                return null;
            }


            lock (__globalPhoneLock)
            {
                return __databse.TryGetTerritory(territoryName);
            }
        }


        public static Phone TryParseWithCountryCode(string number, string countryCode)
        {
            LoadStaticContent();


            var region = TrnaslateRegion(countryCode);
            if (region != null)
            {
                var territory = region.Territories.FirstOrDefault();
                if (territory != null)
                {
                    return TryParse(number, territory.Name);
                }
            }


            return null;
        }


        public static Phone TryParse(string number, string territoryName = null)
        {
            LoadStaticContent();


            if (string.IsNullOrEmpty(number))
            {
                return null;
            }




            var globalNumber = global::GlobalPhone.GlobalPhone.TryParse(number, territoryName);


            if (globalNumber != null && globalNumber.IsValid)
            {
                return new Phone(globalNumber);
            }


            //In scenario such as 6597531150 we need to help the library to understand that there is already the country code as a prefix
            var territory = TranslateTerritory(territoryName);
            if (territory != null)
            {
                //If the number start with same country code, we will try to add the plus and parse it again
                if (number.StartsWith(territory.CountryCode))
                {
                    globalNumber = global::GlobalPhone.GlobalPhone.TryParse(string.Format("+{0}", number), territoryName);
                    if (globalNumber != null && globalNumber.IsValid)
                    {
                        return new Phone(globalNumber);
                    }
                }


            }


            return null;
        }


    }
Copy after login

The above is the C# international mobile phone number class package - Use nuget Global Phone to parse the content of international mobile phone numbers. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!