isValidNumberForRegion() public method

Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for the region "GB" (United Kingdom), since it has its own region code, "IM", which may be undesirable.
public isValidNumberForRegion ( PhoneNumber $number, string $regionCode ) : boolean
$number PhoneNumber the phone number that we want to validate
$regionCode string the region that we want to validate the phone number for
return boolean that indicates whether the number is of a valid pattern
示例#1
0
 public function testIsValidNumberForRegion()
 {
     $number = "+33 6 76 83 51 85";
     $region = "DE";
     $phoneNumber = $this->phoneUtil->parse($number, $region);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($phoneNumber, "DE"));
 }
示例#2
0
 /**
  * Performs the actual validation of the phone number.
  *
  * @param mixed $number
  * @param null  $country
  * @return bool
  */
 protected function isValidNumber($number, $country = null)
 {
     try {
         // Throws NumberParseException if not parsed correctly against the supplied country.
         // If no country was given, tries to discover the country code from the number itself.
         $phoneNumber = $this->lib->parse($number, $country);
         // Lenient validation; doesn't need a country code.
         if ($this->lenient) {
             return $this->lib->isPossibleNumber($phoneNumber);
         }
         // For automatic detection, the number should have a country code.
         // Check if type is allowed.
         if ($phoneNumber->hasCountryCode() && (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types))) {
             // Automatic detection:
             if ($this->autodetect) {
                 // Validate if the international phone number is valid for its contained country.
                 return $this->lib->isValidNumber($phoneNumber);
             }
             // Validate number against the specified country.
             return $this->lib->isValidNumberForRegion($phoneNumber, $country);
         }
     } catch (NumberParseException $e) {
         // Proceed to default validation error.
     }
     return false;
 }
 /**
  * @dataProvider regionList
  * @param string $regionCode
  */
 public function testEveryRegionHasExampleNumber($regionCode)
 {
     $exampleNumber = $this->phoneNumberUtil->getExampleNumber($regionCode);
     $this->assertNotNull($exampleNumber, "None found for region " . $regionCode);
     /*
      * Check the number is valid
      */
     $e164 = $this->phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::E164);
     $phoneObject = $this->phoneNumberUtil->parse($e164, 'ZZ');
     $this->assertEquals($phoneObject, $exampleNumber);
     $this->assertTrue($this->phoneNumberUtil->isValidNumber($phoneObject));
     $this->assertTrue($this->phoneNumberUtil->isValidNumberForRegion($phoneObject, $regionCode));
 }
 /**
  * 
  */
 public function testIsValidNumberForRegion()
 {
     // This number is valid for the Bahamas, but is not a valid US number.
     $this->assertTrue($this->phoneUtil->isValidNumber(self::$bsNumber));
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion(self::$bsNumber, RegionCode::BS));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion(self::$bsNumber, RegionCode::US));
     $bsInvalidNumber = new PhoneNumber();
     $bsInvalidNumber->setCountryCode(1)->setNationalNumber(2421232345);
     // This number is no longer valid.
     $this->assertFalse($this->phoneUtil->isValidNumber($bsInvalidNumber));
     // La Mayotte and Reunion use 'leadingDigits' to differentiate them.
     $reNumber = new PhoneNumber();
     $reNumber->setCountryCode(262)->setNationalNumber(262123456);
     $this->assertTrue($this->phoneUtil->isValidNumber($reNumber));
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::RE));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::YT));
     // Now change the number to be a number for La Mayotte.
     $reNumber->setNationalNumber(269601234);
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::YT));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::RE));
     // This number is no longer valid for La Reunion.
     $reNumber->setNationalNumber(269123456);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::YT));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::RE));
     $this->assertFalse($this->phoneUtil->isValidNumber($reNumber));
     // However, it should be recognised as from La Mayotte, since it is valid for this region.
     $this->assertEquals(RegionCode::YT, $this->phoneUtil->getRegionCodeForNumber($reNumber));
     // This number is valid in both places.
     $reNumber->setNationalNumber(800123456);
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::YT));
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion($reNumber, RegionCode::RE));
     $this->assertTrue($this->phoneUtil->isValidNumberForRegion(self::$internationalTollFree, RegionCode::UN001));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion(self::$internationalTollFree, RegionCode::US));
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion(self::$internationalTollFree, RegionCode::ZZ));
     $invalidNumber = new PhoneNumber();
     // Invalid country calling codes.
     $invalidNumber->setCountryCode(3923)->setNationalNumber(2366);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($invalidNumber, RegionCode::ZZ));
     $invalidNumber->setCountryCode(3923)->setNationalNumber(2366);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($invalidNumber, RegionCode::UN001));
     $invalidNumber->setCountryCode(0)->setNationalNumber(2366);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($invalidNumber, RegionCode::UN001));
     $invalidNumber->setCountryCode(0);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($invalidNumber, RegionCode::ZZ));
 }
 /**
  * Returns the customary display name in the given language for the given territory the phone
  * number is from. If it could be from many territories, nothing is returned.
  *
  * @param PhoneNumber $number
  * @param string $locale
  * @return string
  */
 protected function getCountryNameForNumber(PhoneNumber $number, $locale)
 {
     $regionCodes = $this->phoneUtil->getRegionCodesForCountryCode($number->getCountryCode());
     if (count($regionCodes) === 1) {
         return $this->getRegionDisplayName($regionCodes[0], $locale);
     } else {
         $regionWhereNumberIsValid = 'ZZ';
         foreach ($regionCodes as $regionCode) {
             if ($this->phoneUtil->isValidNumberForRegion($number, $regionCode)) {
                 if ($regionWhereNumberIsValid !== 'ZZ') {
                     // If we can't assign the phone number as definitely belonging to only one territory,
                     // then we return nothing.
                     return "";
                 }
                 $regionWhereNumberIsValid = $regionCode;
             }
         }
         return $this->getRegionDisplayName($regionWhereNumberIsValid, $locale);
     }
 }
示例#6
0
文件: Phone.php 项目: iPublikuj/phone
 /**
  * @param string|Entities\IPhone $number
  * @param string $country
  * @param string|NULL $type
  *
  * @return bool
  *
  * @throws Exceptions\NoValidCountryException
  * @throws Exceptions\NoValidTypeException
  */
 public function isValid($number, $country = 'AUTO', $type = NULL)
 {
     // Check if country is valid
     $country = $this->validateCountry($country);
     // Check if phone type is valid
     $type = $type !== NULL ? $this->validateType($type) : NULL;
     try {
         // Parse string into phone number
         $phoneNumber = $this->phoneNumberUtil->parse((string) $number, $country);
         if ($type !== NULL && $this->phoneNumberUtil->getNumberType($phoneNumber) !== $type) {
             return FALSE;
         }
         // Automatic detection:
         if ($country == 'AUTO') {
             // Validate if the international phone number is valid for its contained country
             return (bool) $this->phoneNumberUtil->isValidNumber($phoneNumber);
         }
         // Validate number against the specified country
         return (bool) $this->phoneNumberUtil->isValidNumberForRegion($phoneNumber, $country);
     } catch (libphonenumber\NumberParseException $ex) {
         return FALSE;
     }
 }
 public function isValidNumberForRegion($country_code)
 {
     return $this->phoneUtil->isValidNumberForRegion($this->phoneNumber, $country_code);
 }
示例#8
-1
 public function isValid($value)
 {
     if (!is_scalar($value)) {
         $this->error(self::INVALID);
         return false;
     }
     $country = $this->getCountry();
     $supportedCountries = $this->libPhoneNumber->getSupportedRegions();
     if (!in_array($country, $supportedCountries)) {
         $this->error(self::UNSUPPORTED);
         return false;
     }
     try {
         $numberProto = $this->libPhoneNumber->parse($value, $country);
     } catch (NumberParseException $e) {
         $this->error(self::INVALID_NUMBER);
         return false;
     }
     if (!$this->libPhoneNumber->isValidNumber($numberProto)) {
         $this->error(self::INVALID_NUMBER);
         return false;
     }
     $region = $this->libPhoneNumber->getRegionCodeForNumber($numberProto);
     if ($this->libPhoneNumber->isValidNumberForRegion($numberProto, $region)) {
         return true;
     }
     $this->error(self::NO_MATCH);
     return false;
 }