1 | <?php |
||
2 | |||
3 | namespace Milwad\LaravelValidate\Rules; |
||
4 | |||
5 | use Illuminate\Contracts\Validation\Rule; |
||
6 | use Milwad\LaravelValidate\Utils\CountryPhoneCallback; |
||
7 | |||
8 | class ValidPhoneNumber implements Rule |
||
9 | { |
||
10 | public function __construct(protected ?string $code = null) {} |
||
11 | |||
12 | /** |
||
13 | * Check phone number is valid. |
||
14 | */ |
||
15 | public function passes($attribute, $value): bool |
||
16 | { |
||
17 | if (is_string($this->code)) { |
||
18 | $passes = CountryPhoneCallback::callPhoneValidator($this->code, $value); |
||
19 | |||
20 | return collect($passes)->some(fn ($passe) => $passe); |
||
21 | } |
||
22 | |||
23 | return preg_match('/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/', $value); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Get the validation error message. |
||
28 | */ |
||
29 | public function message(): string |
||
30 | { |
||
31 | return __('validate.phone-number'); |
||
32 | } |
||
33 | } |
||
34 |