public function getInvalidExampleNumber($regionCode)
{
if (!$this->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.
$desc = $this->getNumberDescByType($this->getMetadataForRegion($regionCode), PhoneNumberType::FIXED_LINE);
if ($desc->getExampleNumber() == '') {
// This shouldn't happen; we have a test for this.
return null;
}
$exampleNumber = $desc->getExampleNumber();
// 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
// MIN_LENGTH_FOR_NSN. 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 ($phoneNumberLength = mb_strlen($exampleNumber) - 1; $phoneNumberLength >= static::MIN_LENGTH_FOR_NSN; $phoneNumberLength--) {
$numberToTry = mb_substr($exampleNumber, 0, $phoneNumberLength);
try {
$possiblyValidNumber = $this->parse($numberToTry, $regionCode);
if (!$this->isValidNumber($possiblyValidNumber)) {
return $possiblyValidNumber;
}
} catch (NumberParseException $e) {
// 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;
}