-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove backwards compatibility break from Phone rule
In version 2.3, the Phone rule started to require "giggly/libphonenumber-for-php" as a dependency. That was a backward compatibility break, but the validation also became stricter, and phone numbers without country codes would not be considered valid. This commit will revert the backward compatibility break. That way, when validating a phone number without a country code (the behaviour from version 2.2), the Phone will not use an external library. Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
fb322df
commit 788939e
Showing
3 changed files
with
109 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
use Respect\Validation\Exceptions\ComponentException; | ||
|
||
use function class_exists; | ||
use function is_null; | ||
use function is_scalar; | ||
use function preg_match; | ||
use function sprintf; | ||
|
||
/** | ||
|
@@ -24,6 +24,9 @@ | |
* Validates an international or country-specific telephone number | ||
* | ||
* @author Alexandre Gomes Gaigalas <[email protected]> | ||
* @author Danilo Correa <[email protected]> | ||
* @author Graham Campbell <[email protected]> | ||
* @author Henrique Moody <[email protected]> | ||
*/ | ||
final class Phone extends AbstractRule | ||
{ | ||
|
@@ -32,42 +35,48 @@ final class Phone extends AbstractRule | |
*/ | ||
private $countryCode; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct(?string $countryCode = null) | ||
{ | ||
$this->countryCode = $countryCode; | ||
if ($countryCode === null) { | ||
return; | ||
} | ||
|
||
if (!is_null($countryCode) && !(new CountryCode())->validate($countryCode)) { | ||
throw new ComponentException( | ||
sprintf( | ||
'Invalid country code %s', | ||
$countryCode | ||
) | ||
); | ||
if (!(new CountryCode())->validate($countryCode)) { | ||
throw new ComponentException(sprintf('Invalid country code %s', $countryCode)); | ||
} | ||
|
||
if (!class_exists(PhoneNumberUtil::class)) { | ||
throw new ComponentException('The phone validator requires giggsey/libphonenumber-for-php'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validate($input): bool | ||
{ | ||
if (!is_scalar($input)) { | ||
return false; | ||
} | ||
|
||
if ($this->countryCode === null) { | ||
return preg_match($this->getPregFormat(), (string) $input) > 0; | ||
} | ||
|
||
try { | ||
return PhoneNumberUtil::getInstance()->isValidNumber( | ||
PhoneNumberUtil::getInstance()->parse((string) $input, $this->countryCode) | ||
); | ||
} catch (NumberParseException $e) { | ||
} catch (NumberParseException) { | ||
return false; | ||
} | ||
} | ||
|
||
private function getPregFormat(): string | ||
{ | ||
return sprintf( | ||
'/^\+?(%1$s)? ?(?(?=\()(\(%2$s\) ?%3$s)|([. -]?(%2$s[. -]*)?%3$s))$/', | ||
'\d{0,3}', | ||
'\d{1,3}', | ||
'((\d{3,5})[. -]?(\d{2}[. -]?\d{2})|(\d{2}[. -]?){4})' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters