public isValidShortNumberForRegion ( |
||
$number | The Short number for which we want to test the validity | |
$regionDialingFrom | string | the region from which the number is dialed |
return | boolean | whether the short number matches a valid pattern |
public function isValidShortNumberForRegion($number, $regionDialingFrom)
{
if ($number instanceof PhoneNumber) {
if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) {
return false;
}
}
$phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
if ($phoneMetadata === null) {
return false;
}
if ($number instanceof PhoneNumber) {
$shortNumber = $this->getNationalSignificantNumber($number);
} else {
/**
* @deprecated Anyone who was using it and passing in a string with whitespace (or other
* formatting characters) would have been getting the wrong result. You should parse
* the string to PhoneNumber and use the method
* {@code #isValidShortNumberForRegion(PhoneNumber, String)}. This method will be
* removed in the next release.
*/
$shortNumber = $number;
}
$generalDesc = $phoneMetadata->getGeneralDesc();
if (!$this->matchesPossibleNumberAndNationalNumber($shortNumber, $generalDesc)) {
return false;
}
$shortNumberDesc = $phoneMetadata->getShortCode();
return $this->matchesPossibleNumberAndNationalNumber($shortNumber, $shortNumberDesc);
}
public function testCountryCallingCodeIsNotIgnored() { // +46 is the country calling code for Sweden (SE), and 40404 is a valid short number in the US. $this->assertFalse($this->shortInfo->isPossibleShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US)); $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US)); $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCostForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US)); }