Skip to content

Commit

Permalink
Merge pull request #6 from xificurk/feature/is-possible
Browse files Browse the repository at this point in the history
Add PhoneNumber::isPossibleNumber() for more lenient validation
  • Loading branch information
BenMorel authored Nov 12, 2018
2 parents f574709 + 1788811 commit 8351528
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
62 changes: 47 additions & 15 deletions tests/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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],
Expand All @@ -281,29 +313,29 @@ 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],
['+3923661830000'],
['+44791234567'],
['+491234'],
['+643316005'],
['+39232366'],
[self::INTERNATIONAL_TOLL_FREE_TOO_LONG]
['+39232366']
];
}

/**
* @return array
*/
public function providerNotPossibleNumbers()
{
return [
[self::INTERNATIONAL_TOLL_FREE_TOO_LONG],
['+4912']
];
}

Expand Down

0 comments on commit 8351528

Please sign in to comment.