isValidNumber() 공개 메소드

Tests whether a phone number matches a valid pattern. Note this doesn't verify the number is actually in use, which is impossible to tell by just looking at a number itself.
public isValidNumber ( PhoneNumber $number ) : boolean
$number PhoneNumber the phone number that we want to validate
리턴 boolean that indicates whether the number is of a valid pattern
예제 #1
0
 public function testKWMobileNumber()
 {
     $number = "51440519";
     $phoneNumber = $this->phoneUtil->parse($number, "KW");
     $this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber));
     $this->assertEquals(PhoneNumberType::MOBILE, $this->phoneUtil->getNumberType($phoneNumber));
 }
예제 #2
0
 public function testFloatNumber()
 {
     $number = "0358112345678987";
     $phoneNumber = $this->phoneUtil->parse($number, "DE");
     $this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber));
     $this->assertEquals('+49358112345678987', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::E164));
     $this->assertEquals('+49 3581 12345678987', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::INTERNATIONAL));
     $this->assertEquals('03581 12345678987', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL));
     $this->assertEquals('011 49 3581 12345678987', $this->phoneUtil->formatOutOfCountryCallingNumber($phoneNumber, 'US'));
     $this->assertEquals('00 49 3581 12345678987', $this->phoneUtil->formatOutOfCountryCallingNumber($phoneNumber, 'CH'));
 }
예제 #3
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;
 }
 public function testInvalidNumber()
 {
     $number = '123401234512345';
     $phoneObject = $this->phoneUtil->parse($number, 'GB');
     $valid = $this->phoneUtil->isValidNumber($phoneObject);
     $this->assertFalse($valid, "Checking phone number is invalid");
 }
 public function testUnknownCountryCallingCode()
 {
     $this->assertFalse($this->phoneUtil->isValidNumber(self::$unknownCountryCodeNoRawInput));
     // It's not very well defined as to what the E164 representation for a number with an invalid
     // country calling code is, but just prefixing the country code and national number is about
     // the best we can do.
     $this->assertEquals("+212345", $this->phoneUtil->format(self::$unknownCountryCodeNoRawInput, PhoneNumberFormat::E164));
 }
 /**
  * @dataProvider supportedGlobalNetworkCallingCodes
  */
 public function testGlobalNetworkNumbers($callingCode)
 {
     $exampleNumber = $this->phoneNumberUtil->getExampleNumberForNonGeoEntity($callingCode);
     $this->assertNotNull($exampleNumber, "No example phone number for calling code " . $callingCode);
     if (!$this->phoneNumberUtil->isValidNumber($exampleNumber)) {
         $this->fail("Failed validation for " . $exampleNumber);
     }
 }
 /**
  * @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));
 }
예제 #8
0
파일: Instance.php 프로젝트: andrazk/msisdn
 /**
  * Parse msisdn
  * @param  string $number
  * @return Instance
  * @author Andraz <*****@*****.**>
  */
 public function parse($number)
 {
     try {
         $phoneNumber = $this->numberUtil->parse($number, null);
     } catch (NumberParseException $e) {
         $this->valid = false;
         return $this;
     }
     if (!($this->valid = $this->numberUtil->isValidNumber($phoneNumber))) {
         return $this;
     }
     $this->countryDiallingCode = (int) $phoneNumber->getCountryCode();
     $this->countryIdentifier = $this->numberUtil->getRegionCodeForNumber($phoneNumber);
     $this->mnoIdentifier = $this->carrierMapper->getNameForNumber($phoneNumber, 'en_US');
     $this->subscriberNumber = $phoneNumber->getNationalNumber();
     return $this;
 }
 /**
  * 
  */
 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));
 }
 private function parse()
 {
     if ($this->phoneNumberProto) {
         // from phoneNumberProto
         $this->country_code = $this->phoneNumberProto->getCountryCode();
         $this->country_code_plus_sign = "+" . $this->country_code;
         $this->national_number = $this->phoneNumberProto->getNationalNumber();
         $this->extension = $this->phoneNumberProto->getExtension();
         $this->country_code_source = $this->phoneNumberProto->getCountryCodeSource();
         if (isset($this->countryCodeSourcesText[$this->country_code_source])) {
             $this->country_code_source_text = $this->countryCodeSourcesText[$this->country_code_source];
         }
         $this->raw_input = $this->phoneNumberProto->getRawInput();
         $this->preferred_domestic_carrier_code = $this->phoneNumberProto->getPreferredDomesticCarrierCode();
         // from validation
         $this->is_possible_number = $this->phoneUtil->isPossibleNumber($this->phoneNumberProto);
         $this->is_valid_number = $this->phoneUtil->isValidNumber($this->phoneNumberProto);
         $this->region_code_for_number = $this->phoneUtil->getRegionCodeForNumber($this->phoneNumberProto);
         $this->number_type = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         $this->number_type_text = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         if (isset($this->phoneNumberTypesText[$this->number_type])) {
             $this->number_type_text = $this->phoneNumberTypesText[$this->number_type];
         } else {
             $this->number_type_text = "OTHER";
         }
         $this->is_mobile_number = in_array($this->number_type, [PhoneNumberType::FIXED_LINE_OR_MOBILE, PhoneNumberType::MOBILE]);
         // from formatting
         $this->format_e164 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::E164);
         $this->format_international = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::INTERNATIONAL);
         $this->format_national = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::NATIONAL);
         $this->format_rfc3966 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::RFC3966);
         // from additional
         $phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder::getInstance();
         $this->description = $phoneNumberOfflineGeocoder->getDescriptionForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToCarrierMapper = PhoneNumberToCarrierMapper::getInstance();
         $this->carrier_name = $phoneNumberToCarrierMapper->getNameForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToTimeZonesMapper = PhoneNumberToTimeZonesMapper::getInstance();
         $this->timezones = $phoneNumberToTimeZonesMapper->getTimeZonesForNumber($this->phoneNumberProto);
     }
 }
예제 #11
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;
     }
 }
예제 #12
0
 /**
  * @param $number
  * @dataProvider validPolishNumbers
  */
 public function testValidPolishNumbers($number)
 {
     $phoneNumber = $this->phoneUtil->parse($number, 'PL');
     $this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber));
     $this->assertEquals($number, $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL));
 }
예제 #13
0
 /**
  * @return bool
  */
 public function isValid() : bool
 {
     return $this->util->isValidNumber($this->number);
 }
예제 #14
-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;
 }