getInvalidExampleNumber method

PhoneNumber? getInvalidExampleNumber(
  1. String regionCode
)

Gets an invalid number for the specified region. This is useful for unit-testing purposes, where you want to test what will happen with an invalid number. Note that the number that is returned will always be able to be parsed and will have the correct country code. It may also be a valid short number/code for this region. Validity checking such numbers is handled with ShortNumberInfo.

regionCode the region for which an example number is needed returns an invalid number for the specified region. Returns null when an unsupported region or the region 001 (Earth) is passed in.

Implementation

PhoneNumber? getInvalidExampleNumber(String regionCode) {
  if (!_isValidRegionCode(regionCode)) {
    return null;
  }
  // We start off with a valid fixed-line number since every country supports
  // this. Alternatively
  // we could start with a different number type, since fixed-line numbers
  // typically have a wide
  // breadth of valid number lengths and we may have to make it very short before
  // we get an
  // invalid number.
  PhoneNumberDesc desc = _getNumberDescByType(
    getMetadataForRegion(regionCode: regionCode)!,
    PhoneNumberType.fixedLine,
  );
  if (!desc.hasExampleNumber()) {
    // This shouldn't happen; we have a test for this.
    return null;
  }

  String exampleNumber = desc.exampleNumber;
  // Try and make the number invalid. We do this by changing the length. We try
  // reducing the length of the number, since currently no region has a number that is the same
  // length as minLengthForNsn. This is probably quicker than making the number longer,
  // which is another alternative. We could also use the possible number pattern to extract the
  // possible lengths of the number to make this faster, but this method is only for unit-testing so
  // simplicity is preferred to performance. We don't want to return a number that can't be
  // parsed, so we check the number is long enough. We try all possible lengths because phone number
  // plans often have overlapping prefixes so the number 123456 might be valid as a fixed-line
  // number, and 12345 as a mobile number. It would be faster to loop in a different order, but we
  // prefer numbers that look closer to real numbers (and it gives us a variety of different lengths
  // for the resulting phone numbers - otherwise they would all be MIN_LENGTH_FOR_NSN digits long.)
  for (int phoneNumberLength = exampleNumber.length - 1;
      phoneNumberLength >= _minLengthForNsn;
      phoneNumberLength--) {
    String numberToTry = exampleNumber.substring(0, phoneNumberLength);
    try {
      PhoneNumber possiblyValidNumber = parse(numberToTry, regionCode);
      if (!isValidNumber(possiblyValidNumber)) {
        return possiblyValidNumber;
      }
    } catch (_) {
      // Shouldn't happen: we have already checked the length, we know example numbers
      // have
      // only valid digits, and we know the region code is fine.
    }
  }
  // We have a test to check that this doesn't happen for any of our supported
  // regions.
  return null;
}