1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Dimtrovich/Validation. |
||
5 | * |
||
6 | * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Dimtrovich\Validation\Rules; |
||
13 | |||
14 | use BadMethodCallException; |
||
15 | use BlitzPHP\Utilities\Helpers; |
||
16 | use Dimtrovich\Validation\Utils\CountryPhoneCallback; |
||
17 | |||
18 | class Phone extends AbstractRule |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $fillableParams = ['code']; |
||
24 | |||
25 | /** |
||
26 | * Check if a given value is valid phone number |
||
27 | * |
||
28 | * @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Rules\ValidPhoneNumber</a> |
||
29 | * |
||
30 | * @param mixed $value |
||
31 | */ |
||
32 | public function check($value): bool |
||
33 | { |
||
34 | if (! empty($code = $this->parameter('code'))) { |
||
35 | try { |
||
36 | $passes = (new CountryPhoneCallback($value, $code))->callPhoneValidator(); |
||
37 | } catch (BadMethodCallException) { |
||
38 | $code = $this->getAttribute()->getValue($code); |
||
39 | $passes = (new CountryPhoneCallback($value, $code))->callPhoneValidator(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
40 | } |
||
41 | |||
42 | return Helpers::collect($passes)->some(fn ($passe) => $passe); |
||
43 | } |
||
44 | |||
45 | return preg_match('/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/', $value); |
||
0 ignored issues
–
show
|
|||
46 | } |
||
47 | } |
||
48 |