respect-validation/library/Rules/Phone.php
Henrique Moody 788939e359
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 <henriquemoody@gmail.com>
2024-03-11 22:14:03 +01:00

83 lines
2.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use Respect\Validation\Exceptions\ComponentException;
use function class_exists;
use function is_scalar;
use function preg_match;
use function sprintf;
/**
* Validates whether the input is a valid phone number.
*
* Validates an international or country-specific telephone number
*
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Graham Campbell <graham@mineuk.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Phone extends AbstractRule
{
/**
* @var ?string
*/
private $countryCode;
public function __construct(?string $countryCode = null)
{
$this->countryCode = $countryCode;
if ($countryCode === null) {
return;
}
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');
}
}
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) {
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})'
);
}
}