예제 #1
0
        /// <summary>
        /// Gets the phone format from registry.
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <param name="distanceRule">The distance rule.</param>
        /// <returns>The string representation of the country's selected phone number format or <value>String.Empty</value>, if an error occured.</returns>
        public string GetPhoneFormat(int countryCode, DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
            {
                return(PhoneNumberConstants.CANONICAL_FORMAT);
            }

            IRegistryKey hklmCountry = _registryProxy.LocalMachine.OpenSubKey(PhoneNumberConstants.REGISTRY_COUNTRYLIST_LEGACY);

            if (hklmCountry == null)
            {
                hklmCountry = _registryProxy.LocalMachine.OpenSubKey(PhoneNumberConstants.REGISTRY_COUNTRYLIST_MODERN);
            }

            try
            {
                hklmCountry = hklmCountry.OpenSubKey(countryCode.ToString());
                object curFormat = hklmCountry.GetValue(distanceRule.ToString());
                if (curFormat != null)
                {
                    return(curFormat.ToString());
                }
            }
            catch { }

            return(String.Empty);
        }
예제 #2
0
        /// <summary>
        /// Gets the phone format from the XML resource.
        /// </summary>
        /// <param name="countryID">The country ID.</param>
        /// <param name="distanceRule">The distance rule.</param>
        /// <returns>The string representation of the country's selected phone number format or <value>String.Empty</value>, if an error occured.</returns>
        public string GetPhoneFormat(int countryID, DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
            {
                return(PhoneNumberConstants.CANONICAL_FORMAT);
            }

            if (initialized == false)
            {
                Initialize();
            }

            string result = "";

            XmlDocument countries = new XmlDocument();

            countries.LoadXml(Properties.Resources.Countries);
            XmlNode country = countries.SelectSingleNode(@"/countries/country[id=" + countryID + "]");

            if (country == null)
            {
                return(String.Empty);
            }

            try { result = country.SelectSingleNode(distanceRuleXmlNameMap[distanceRule]).InnerText; }
            catch { result = String.Empty; }

            return(result);
        }
예제 #3
0
        public string FormatPhoneNumber(string number, DISTANCE_RULE distanceRule)
        {
            if (String.IsNullOrEmpty(number))
                return String.Empty;
            if (Regex.IsMatch(number, PhoneNumberConstants.PHONE_NUMBER_PATTERN) == false)
                return String.Empty;

            string result = "";
            if (distanceRule == DISTANCE_RULE.CANONICAL)
                result = PhoneNumberConstants.CANONICAL_FORMAT;
            else
                result = GetUserPhoneFormat(distanceRule);

            string countryCode = GetCountryCode(number);
            if (String.IsNullOrEmpty(countryCode))
                return String.Empty;

            string areaCode = GetAreaCode(number, countryCode);
            if (String.IsNullOrEmpty(areaCode))
                return String.Empty;

            string localNumber = GetLocalNumber(number, countryCode, areaCode);
            if (String.IsNullOrEmpty(localNumber))
                return String.Empty;

            result = result.Replace("E", countryCode);
            result = result.Replace("F", areaCode);
            result = result.Replace("G", localNumber);

            return result;
        }
예제 #4
0
        public void FormatPhoneNumber_PhoneNumberInDresdenGermany_ReturnsFormattedNumber(
            [Values(DISTANCE_RULE.CANONICAL, DISTANCE_RULE.InternationalRule, DISTANCE_RULE.LongDistanceRule,
                    DISTANCE_RULE.SameAreaRule)] DISTANCE_RULE distanceRule,
            [Values("+49 (351) 1234567", "00493511234567", "03511234567", "1234567")] string expected)
        {
            MockRepository           mocks                = new MockRepository();
            IPhoneNumberDataXml      xmlDataProvider      = mocks.Stub <IPhoneNumberDataXml>();
            IPhoneNumberDataRegistry registryDataProvider = mocks.Stub <IPhoneNumberDataRegistry>();

            string number = "+493511234567";

            using (mocks.Record())
            {
                registryDataProvider.GetUserCountryID();
                LastCall.Return("49");
                registryDataProvider.GetUserAreaCode();
                LastCall.Return("351");
                registryDataProvider.GetPhoneFormat(49, DISTANCE_RULE.InternationalRule);
                LastCall.Return("00EFG");
                registryDataProvider.GetPhoneFormat(49, DISTANCE_RULE.LongDistanceRule);
                LastCall.Return("0FG");
                registryDataProvider.GetPhoneFormat(49, DISTANCE_RULE.SameAreaRule);
                LastCall.Return("G");

                xmlDataProvider.GetCountryCode("4935");
                LastCall.Return("49");
                xmlDataProvider.GetAreaCode("+493511234567");
                LastCall.Return("+49351");
            }

            PhoneNumbers phoneNumberConverter = new PhoneNumbers(xmlDataProvider, registryDataProvider);
            string       actual = phoneNumberConverter.FormatPhoneNumber(number, distanceRule);

            Assert.AreEqual(expected, actual);
        }
예제 #5
0
        private string GetUserPhoneFormat(DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
            {
                return(PhoneNumberConstants.CANONICAL_FORMAT);
            }

            string result = "";
            string id     = GetUserCountryID();

            if (String.IsNullOrEmpty(id))
            {
                return(String.Empty);
            }

            int userCountryID = int.Parse(id);

            result = registryDataProvider.GetPhoneFormat(userCountryID, distanceRule);
            if (String.IsNullOrEmpty(result))
            {
                result = xmlDataProvider.GetPhoneFormat(userCountryID, distanceRule);
            }

            return(result);
        }
예제 #6
0
        public string FormatPhoneNumber(string number, DISTANCE_RULE distanceRule)
        {
            if (String.IsNullOrEmpty(number))
            {
                return(String.Empty);
            }
            if (Regex.IsMatch(number, PhoneNumberConstants.PHONE_NUMBER_PATTERN) == false)
            {
                return(String.Empty);
            }

            string result = "";

            if (distanceRule == DISTANCE_RULE.CANONICAL)
            {
                result = PhoneNumberConstants.CANONICAL_FORMAT;
            }
            else
            {
                result = GetUserPhoneFormat(distanceRule);
            }

            string countryCode = GetCountryCode(number);

            if (String.IsNullOrEmpty(countryCode))
            {
                return(String.Empty);
            }

            string areaCode = GetAreaCode(number, countryCode);

            if (String.IsNullOrEmpty(areaCode))
            {
                return(String.Empty);
            }

            string localNumber = GetLocalNumber(number, countryCode, areaCode);

            if (String.IsNullOrEmpty(localNumber))
            {
                return(String.Empty);
            }

            result = result.Replace("E", countryCode);
            result = result.Replace("F", areaCode);
            result = result.Replace("G", localNumber);

            return(result);
        }
예제 #7
0
        /// <summary>
        /// Gets the phone format from registry.
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <param name="distanceRule">The distance rule.</param>
        /// <returns>The string representation of the country's selected phone number format or <value>String.Empty</value>, if an error occured.</returns>
        public string GetPhoneFormat(int countryCode, DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
                return PhoneNumberConstants.CANONICAL_FORMAT;

            IRegistryKey hklmCountry = _registryProxy.LocalMachine.OpenSubKey(PhoneNumberConstants.REGISTRY_COUNTRYLIST_LEGACY);
            if (hklmCountry == null)
                hklmCountry = _registryProxy.LocalMachine.OpenSubKey(PhoneNumberConstants.REGISTRY_COUNTRYLIST_MODERN);

            try
            {
                hklmCountry = hklmCountry.OpenSubKey(countryCode.ToString());
                object curFormat = hklmCountry.GetValue(distanceRule.ToString());
                if (curFormat != null)
                    return curFormat.ToString();
            }
            catch { }

            return String.Empty;
        }
예제 #8
0
        private string GetUserPhoneFormat(DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
                return PhoneNumberConstants.CANONICAL_FORMAT;

            string result = "";
            string id = GetUserCountryID();
            if (String.IsNullOrEmpty(id))
                return String.Empty;

            int userCountryID = int.Parse(id);

            result = registryDataProvider.GetPhoneFormat(userCountryID, distanceRule);
            if (String.IsNullOrEmpty(result))
                result = xmlDataProvider.GetPhoneFormat(userCountryID, distanceRule);

            return result;
        }
예제 #9
0
        /// <summary>
        /// Gets the phone format from the XML resource.
        /// </summary>
        /// <param name="countryID">The country ID.</param>
        /// <param name="distanceRule">The distance rule.</param>
        /// <returns>The string representation of the country's selected phone number format or <value>String.Empty</value>, if an error occured.</returns>
        public string GetPhoneFormat(int countryID, DISTANCE_RULE distanceRule)
        {
            if (distanceRule == DISTANCE_RULE.CANONICAL)
                return PhoneNumberConstants.CANONICAL_FORMAT;

            if (initialized == false)
                Initialize();

            string result = "";

            XmlDocument countries = new XmlDocument();
            countries.LoadXml(Properties.Resources.Countries);
            XmlNode country = countries.SelectSingleNode(@"/countries/country[id=" + countryID + "]");
            if (country == null)
                return String.Empty;

            try { result = country.SelectSingleNode(distanceRuleXmlNameMap[distanceRule]).InnerText; }
            catch { result = String.Empty; }

            return result;
        }