public void TestCanBeInternationallyDialled()
 {
     foreach (var regionCode in phoneNumberUtil.GetSupportedRegions())
     {
         PhoneNumber     exampleNumber = null;
         PhoneNumberDesc desc          =
             phoneNumberUtil.GetMetadataForRegion(regionCode).NoInternationalDialling;
         try
         {
             if (desc.HasExampleNumber)
             {
                 exampleNumber = phoneNumberUtil.Parse(desc.ExampleNumber, regionCode);
             }
         }
         catch (NumberParseException)
         {
         }
         if (exampleNumber != null && phoneNumberUtil.CanBeInternationallyDialled(exampleNumber))
         {
             wrongTypeCases.Add(exampleNumber);
             // LOGGER.log(Level.SEVERE, "Number " + exampleNumber.toString()
             //   + " should not be internationally diallable");
         }
     }
     Assert.Equal(0, wrongTypeCases.Count);
 }
Exemple #2
0
        private bool MatchesEmergencyNumberHelper(String number, String regionCode,
                                                  bool allowPrefixMatch)
        {
            number = PhoneNumberUtil.ExtractPossibleNumber(number);
            if (PhoneNumberUtil.PlusCharsPattern.MatchBeginning(number).Success)
            {
                // Returns false if the number starts with a plus sign. We don't believe dialing the country
                // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can
                // add additional logic here to handle it.
                return(false);
            }
            var metadata = phoneUtil.GetMetadataForRegion(regionCode);

            if (metadata == null || !metadata.HasEmergency)
            {
                return(false);
            }
            var emergencyNumberPattern =
                new PhoneRegex(metadata.Emergency.NationalNumberPattern);
            var normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(number);

            // In Brazil, it is impossible to append additional digits to an emergency number to dial the
            // number.
            return((!allowPrefixMatch || regionCode.Equals("BR"))
                ? emergencyNumberPattern.MatchAll(normalizedNumber).Success
                : emergencyNumberPattern.MatchBeginning(normalizedNumber).Success);
        }
        // The metadata needed by this class is the same for all regions sharing the same country calling
        // code. Therefore, we return the metadata for "main" region for this country calling code.
        private PhoneMetadata GetMetadataForRegion(string regionCode)
        {
            var countryCallingCode = phoneUtil.GetCountryCodeForRegion(regionCode);
            var mainCountry        = phoneUtil.GetRegionCodeForCountryCode(countryCallingCode);
            var metadata           = phoneUtil.GetMetadataForRegion(mainCountry);

            return(metadata ?? EmptyMetadata);
            // Set to a default instance of the metadata. This allows us to function with an incorrect
            // region code, even if formatting only works for numbers specified with "+".
        }
        public static bool IsNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util)
        {
            // First, check how we deduced the country code. If it was written in international format, then
            // the national prefix is not required.
            if (number.CountryCodeSource != PhoneNumber.Types.CountryCodeSource.FROM_DEFAULT_COUNTRY)
            {
                return(true);
            }
            String phoneNumberRegion =
                util.GetRegionCodeForCountryCode(number.CountryCode);
            PhoneMetadata metadata = util.GetMetadataForRegion(phoneNumberRegion);

            if (metadata == null)
            {
                return(true);
            }
            // Check if a national prefix should be present when formatting this number.
            String       nationalNumber = util.GetNationalSignificantNumber(number);
            NumberFormat formatRule     =
                util.ChooseFormattingPatternForNumber(metadata.NumberFormatList, nationalNumber);

            // To do this, we check that a national prefix formatting rule was present and that it wasn't
            // just the first-group symbol ($1) with punctuation.
            if ((formatRule != null) && formatRule.NationalPrefixFormattingRule.Length > 0)
            {
                if (formatRule.NationalPrefixOptionalWhenFormatting)
                {
                    // The national-prefix is optional in these cases, so we don't need to check if it was
                    // present.
                    return(true);
                }
                // Remove the first-group symbol.
                String candidateNationalPrefixRule = formatRule.NationalPrefixFormattingRule;
                // We assume that the first-group symbol will never be _before_ the national prefix.
                candidateNationalPrefixRule =
                    candidateNationalPrefixRule.Substring(0, candidateNationalPrefixRule.IndexOf("${1}"));
                candidateNationalPrefixRule =
                    PhoneNumberUtil.NormalizeDigitsOnly(candidateNationalPrefixRule);
                if (candidateNationalPrefixRule.Length == 0)
                {
                    // National Prefix not needed for this number.
                    return(true);
                }
                // Normalize the remainder.
                String        rawInputCopy = PhoneNumberUtil.NormalizeDigitsOnly(number.RawInput);
                StringBuilder rawInput     = new StringBuilder(rawInputCopy);
                // Check if we found a national prefix and/or carrier code at the start of the raw input, and
                // return the result.
                return(util.MaybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null));
            }
            return(true);
        }
        // The metadata needed by this class is the same for all regions sharing the same country calling
        // code. Therefore, we return the metadata for "main" region for this country calling code.
        private PhoneMetadata GetMetadataForRegion(String regionCode)
        {
            int           countryCallingCode = phoneUtil.GetCountryCodeForRegion(regionCode);
            String        mainCountry        = phoneUtil.GetRegionCodeForCountryCode(countryCallingCode);
            PhoneMetadata metadata           = phoneUtil.GetMetadataForRegion(mainCountry);

            if (metadata != null)
            {
                return(metadata);
            }
            // Set to a default instance of the metadata. This allows us to function with an incorrect
            // region code, even if formatting only works for numbers specified with "+".
            return(EMPTY_METADATA);
        }