1 | <?php |
||
2 | namespace LVR\Phone; |
||
3 | |||
4 | use Illuminate\Contracts\Validation\Rule; |
||
5 | |||
6 | class Phone implements Rule |
||
7 | { |
||
8 | /** |
||
9 | * Determine if the validation rule passes. |
||
10 | * |
||
11 | * @param string $attribute |
||
12 | * @param mixed $value |
||
13 | * |
||
14 | * @return bool |
||
15 | */ |
||
16 | 6 | public function passes($attribute, $value) |
|
17 | { |
||
18 | 6 | return $this->isPhone($value); |
|
19 | } |
||
20 | |||
21 | /** |
||
22 | * Get the validation error message. |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | 3 | public function message() |
|
27 | { |
||
28 | 3 | return 'Incorrect phone format for :attribute.'; |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Checks through all validation methods to verify it is in a |
||
33 | * phone number format of some type |
||
34 | * @param string $value The phone number to check |
||
35 | * @return boolean is it correct format? |
||
36 | */ |
||
37 | 6 | protected function isPhone($value) |
|
38 | { |
||
39 | 6 | return $this->isE123($value) || $this->isE164($value) || $this->isNANP($value) || $this->isDigits($value); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Format example 5555555555, 15555555555 |
||
44 | * @param [type] $value [description] |
||
45 | * @return boolean [description] |
||
46 | */ |
||
47 | 6 | protected function isDigits($value) |
|
48 | { |
||
49 | 6 | $conditions = []; |
|
50 | 6 | $conditions[] = strlen($value) >= 10; |
|
51 | 6 | $conditions[] = strlen($value) <= 16; |
|
52 | 6 | $conditions[] = preg_match("/[^\d]/i", $value) === 0; |
|
53 | 6 | return (bool) array_product($conditions); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Format example +22 555 555 1234, (607) 555 1234, (022607) 555 1234 |
||
58 | * @param $value |
||
59 | * @return bool |
||
60 | */ |
||
61 | 9 | protected function isE123($value) |
|
62 | { |
||
63 | 9 | return preg_match('/^(?:\((\+?\d+)?\)|\+?\d+) ?\d*(-?\d{2,3} ?){0,4}$/', $value) === 1; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Format example +15555555555 |
||
68 | * @param string $value The phone number to check |
||
69 | * @return boolean is it correct format? |
||
70 | */ |
||
71 | 6 | protected function isE164($value) |
|
72 | { |
||
73 | 6 | $conditions = []; |
|
74 | 6 | $conditions[] = strpos($value, "+") === 0; |
|
75 | 6 | $conditions[] = strlen($value) >= 9; |
|
76 | 6 | $conditions[] = strlen($value) <= 16; |
|
77 | 6 | $conditions[] = preg_match("/[^\d+]/i", $value) === 0; |
|
78 | 6 | return (bool) array_product($conditions); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Format examples: (555) 555-5555, 1 (555) 555-5555, 1-555-555-5555, 555-555-5555, 1 555 555-5555 |
||
83 | * https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries |
||
84 | * @param string $value The phone number to check |
||
85 | * @return boolean is it correct format? |
||
86 | */ |
||
87 | 6 | protected function isNANP($value) |
|
88 | { |
||
89 | 6 | $conditions = []; |
|
90 | 6 | $conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0; |
|
91 | 6 | return (bool) array_product($conditions); |
|
92 | } |
||
93 | } |