コード例 #1
0
        public static PhoneNumber TryParse(string text, PhoneNumber location)
        {
            var countryCode = location.CountryCode;
            var localParser = NumberingPlanFactory.GetNumberingPlan(location.CountryName);

            if (localParser == null)
            {
                throw new NotSupportedException($"Numbering plan for {location.CountryName} not supported.");
            }

            text = localParser.ResolveInternationalDialling(text);

            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var numberingPlanEntry in localParser.CodeList)
            {
                var areaCode = numberingPlanEntry.Code;
                var pattern  = new Regex($@"^+?{countryCode}{areaCode}([0-9]+)$");
                var match    = pattern.Match(text);
                if (match.Success)
                {
                    return(new PhoneNumber(countryCode, areaCode, match.Groups[1].Value, "", false));
                }

                pattern = new Regex($@"^{areaCode}([0-9]+)$");
                match   = pattern.Match(text);
                if (match.Success)
                {
                    return(new PhoneNumber(countryCode, areaCode, match.Groups[1].Value, "", false));
                }
            }

            return(Parse(text, location));
        }
コード例 #2
0
        public static PhoneNumber Parse(string text, PhoneNumber defaultLocation)
        {
            var parsedNumber = new PhoneNumber();

            text = ValidateSeparators(text);

            var localParser = NumberingPlanFactory
                              .GetNumberingPlan(InternationalNumberingPlan
                                                .GetNameByCountryCode(defaultLocation.CountryCode));

            if (localParser != null)
            {
                text = localParser.ResolveInternationalDialling(text);
            }

            var intl = InternationalNumberingPlan.Parse(ref text);

            if (intl != null)
            {
                parsedNumber.CountryCode = intl.CountryCode;
                parsedNumber.CountryName = intl.CountryName;
                localParser = NumberingPlanFactory.GetNumberingPlan(intl.CountryName);
            }

            if (string.IsNullOrEmpty(parsedNumber.AreaCode))
            {
                localParser?.Parse(ref parsedNumber, ref text);
            }

            // area code by extracting numbers in braces
            if (string.IsNullOrEmpty(parsedNumber.AreaCode))
            {
                var foundAreaCode = new Regex(@"^\(([0-9 ]+)\)").Match(text);
                if (foundAreaCode.Success)
                {
                    if (foundAreaCode.Groups[1].Value == "0")
                    {
                        text = text.Replace("(0)", "").Trim();
                    }
                    else
                    {
                        var explicitAreaCode = foundAreaCode.Groups[1].Value;
                        explicitAreaCode      = explicitAreaCode.Replace(" ", "");
                        text                  = text.Substring(foundAreaCode.Groups[0].Value.Length);
                        parsedNumber.AreaCode = explicitAreaCode;

                        localParser?.Parse(ref parsedNumber, ref explicitAreaCode);
                    }

                    text = Trim(text);
                }
            }

            var findExtDelimiter = new Regex(".*([^0-9])[0-9]+$").Match(text);
            var extDelimiter     = '-';

            if (findExtDelimiter.Success)
            {
                extDelimiter = findExtDelimiter.Groups[1].Value[0];
            }

            // count all delimiters
            var delimiters = new Dictionary <char, int>();

            foreach (var ch in text)
            {
                if (char.IsDigit(ch))
                {
                    continue;
                }

                if (delimiters.ContainsKey(ch))
                {
                    delimiters[ch]++;
                }
                else
                {
                    delimiters.Add(ch, 1);
                }
            }

            if ((extDelimiter != ' ') && delimiters.ContainsKey(extDelimiter) && (delimiters[extDelimiter] == 1))
            {
                var extDelimiterPos = text.LastIndexOf(extDelimiter);
                parsedNumber.Extension = text.Substring(extDelimiterPos + 1);
                text = text.Substring(0, extDelimiterPos);
                text = Trim(text);
            }

            if (string.IsNullOrEmpty(parsedNumber.AreaCode))
            {
                foreach (var pair in delimiters)
                {
                    if (pair.Value != 1)
                    {
                        continue;
                    }

                    var areaDelimiter = text.IndexOf(pair.Key);
                    if (areaDelimiter < 0)
                    {
                        continue;
                    }

                    parsedNumber.AreaCode = text.Substring(0, areaDelimiter);
                    if (localParser != null)
                    {
                        var code = parsedNumber.AreaCode;
                        localParser.Parse(ref parsedNumber, ref code);
                    }
                    else
                    {
                        foreach (var pair2 in delimiters)
                        {
                            parsedNumber.AreaCode = parsedNumber.AreaCode.Replace(pair2.Key.ToString(), "");
                        }
                    }

                    text = text.Substring(areaDelimiter + 1);
                    text = Trim(text);
                    break;
                }
            }

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var pair in delimiters)
            {
                text = text.Replace(pair.Key.ToString(), "");
            }

            parsedNumber.Number       = text;
            parsedNumber.DialInternal = string.IsNullOrEmpty(parsedNumber.CountryCode) &&
                                        string.IsNullOrEmpty(parsedNumber.AreaCode) &&
                                        string.IsNullOrEmpty(parsedNumber.Number) &&
                                        !string.IsNullOrEmpty(parsedNumber.Extension);

            if (!parsedNumber.DialInternal)
            {
                if (string.IsNullOrEmpty(parsedNumber.AreaCode) && !string.IsNullOrEmpty(defaultLocation.AreaCode))
                {
                    parsedNumber.AreaCode = defaultLocation.AreaCode;
                    parsedNumber.AreaName = defaultLocation.AreaName;
                }

                if (string.IsNullOrEmpty(parsedNumber.CountryCode) && !string.IsNullOrEmpty(parsedNumber.AreaCode))
                {
                    parsedNumber.CountryCode = defaultLocation.CountryCode;
                    parsedNumber.CountryName = defaultLocation.CountryName;
                }
            }

            return(parsedNumber);
        }