diff --git a/README.md b/README.md index bac43dc..a8a54c1 100644 --- a/README.md +++ b/README.md @@ -76,17 +76,22 @@ catch (PhoneNumberParseException $e) { } ``` -In most cases, it is recommended to perform an extra step of validation with `isValidNumber()`: +In most cases, it is recommended to perform an extra step of validation with `isValidNumber()` or `isPossibleNumber()`: ```php +if (! $number->isPossibleNumber()) { + // a more lenient and faster check than `isValidNumber()` +} + if (! $number->isValidNumber()) { - // ... + // strict check relying on up-to-date metadata library } + ``` As a rule of thumb, do the following: -- When the number comes from user input, do a full validation: `parse()` and catch `PhoneNumberParseException`, then call `isValidNumber()` if no exception occurred; +- When the number comes from user input, do a full validation: `parse()` and catch `PhoneNumberParseException`, then call `isValidNumber()` (or `isPossibleNumber()` for a more lenient check) if no exception occurred; - When the number is later retrieved from your database, and has been validated before, you can just perform a blind `parse()`. ### Formatting a number diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index 6751598..18e2813 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -114,6 +114,18 @@ public function getRegionCode() : ?string return $regionCode; } + /** + * Returns whether this phone number is a possible number. + * + * Note this provides a more lenient and faster check than `isValidNumber()`. + * + * @return bool + */ + public function isPossibleNumber() : bool + { + return PhoneNumberUtil::getInstance()->isPossibleNumber($this->phoneNumber); + } + /** * Returns whether this phone number matches a valid pattern. * diff --git a/tests/PhoneNumberTest.php b/tests/PhoneNumberTest.php index cdf8f18..7a24b5a 100644 --- a/tests/PhoneNumberTest.php +++ b/tests/PhoneNumberTest.php @@ -257,7 +257,28 @@ public function providerGetNumberType() } /** - * @dataProvider providerIsValidNumber + * @dataProvider providerValidNumbers + * @dataProvider providerPossibleButNotValidNumbers + * + * @param string $phoneNumber + */ + public function testIsPossibleNumber($phoneNumber) + { + $this->assertTrue(PhoneNumber::parse($phoneNumber)->isPossibleNumber()); + } + + /** + * @dataProvider providerNotPossibleNumbers + * + * @param string $phoneNumber + */ + public function testIsNotPossibleNumber($phoneNumber) + { + $this->assertFalse(PhoneNumber::parse($phoneNumber)->isPossibleNumber()); + } + + /** + * @dataProvider providerValidNumbers * * @param string $phoneNumber */ @@ -266,10 +287,21 @@ public function testIsValidNumber($phoneNumber) $this->assertTrue(PhoneNumber::parse($phoneNumber)->isValidNumber()); } + /** + * @dataProvider providerNotPossibleNumbers + * @dataProvider providerPossibleButNotValidNumbers + * + * @param string $phoneNumber + */ + public function testIsNotValidNumber($phoneNumber) + { + $this->assertFalse(PhoneNumber::parse($phoneNumber)->isValidNumber()); + } + /** * @return array */ - public function providerIsValidNumber() + public function providerValidNumbers() { return [ [self::US_NUMBER], @@ -281,20 +313,10 @@ public function providerIsValidNumber() ]; } - /** - * @dataProvider providerIsNotValidNumber - * - * @param string $phoneNumber - */ - public function testIsNotValidNumber($phoneNumber) - { - $this->assertFalse(PhoneNumber::parse($phoneNumber)->isValidNumber()); - } - /** * @return array */ - public function providerIsNotValidNumber() + public function providerPossibleButNotValidNumbers() { return [ [self::US_LOCAL_NUMBER], @@ -302,8 +324,18 @@ public function providerIsNotValidNumber() ['+44791234567'], ['+491234'], ['+643316005'], - ['+39232366'], - [self::INTERNATIONAL_TOLL_FREE_TOO_LONG] + ['+39232366'] + ]; + } + + /** + * @return array + */ + public function providerNotPossibleNumbers() + { + return [ + [self::INTERNATIONAL_TOLL_FREE_TOO_LONG], + ['+4912'] ]; }