1 | <?php |
||
2 | |||
3 | namespace SeedsStd\JpValidationRules; |
||
4 | |||
5 | use Illuminate\Contracts\Validation\Rule; |
||
6 | |||
7 | class PhoneNumber implements Rule |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $configs; |
||
13 | |||
14 | /** |
||
15 | * Create a new rule instance. |
||
16 | * |
||
17 | * @param array $configs |
||
18 | * @return void |
||
19 | */ |
||
20 | public function __construct(array $configs = []) |
||
21 | { |
||
22 | $default_configs = [ |
||
23 | 'allow_country_code' => false, |
||
24 | ]; |
||
25 | |||
26 | $this->configs = array_merge($default_configs, $configs); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Determine if the validation rule passes. |
||
31 | * |
||
32 | * @param string $attribute |
||
33 | * @param mixed $value |
||
34 | * @return bool |
||
35 | */ |
||
36 | public function passes($attribute, $value) |
||
37 | { |
||
38 | if ($this->configs['allow_country_code']) { |
||
39 | return (bool)preg_match('/^\+?81\d{1,4}[-\s]?\d{1,4}[-\s]?\d{1,4}$/', $value); |
||
40 | } |
||
41 | |||
42 | return (bool)preg_match('/^0\d{1,4}[-\s]?\d{1,4}[-\s]?\d{1,4}$/', $value); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get the validation error message. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public function message() |
||
51 | { |
||
52 | return __('validation.jp_phone_number'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
53 | } |
||
54 | } |
||
55 |