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
|
|
|
public function passes($attribute, $value) |
17
|
|
|
{ |
18
|
|
|
return $this->isPhone($value); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the validation error message. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function message() |
27
|
|
|
{ |
28
|
|
|
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
|
|
|
protected function isPhone($value) |
38
|
|
|
{ |
39
|
|
|
return $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
|
|
|
protected function isDigits($value) |
48
|
|
|
{ |
49
|
|
|
$conditions = []; |
50
|
|
|
$conditions[] = strlen($value) >= 10; |
51
|
|
|
$conditions[] = strlen($value) <= 16; |
52
|
|
|
$conditions[] = preg_match("/[^\d]/i", $value) === 0; |
53
|
|
|
return (bool) array_product($conditions); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Format example +15555555555 |
58
|
|
|
* @param string $value The phone number to check |
59
|
|
|
* @return boolean is it correct format? |
60
|
|
|
*/ |
61
|
|
|
protected function isE164($value) |
62
|
|
|
{ |
63
|
|
|
$conditions = []; |
64
|
|
|
$conditions[] = strpos($value, "+") === 0; |
65
|
|
|
$conditions[] = strlen($value) >= 9; |
66
|
|
|
$conditions[] = strlen($value) <= 16; |
67
|
|
|
$conditions[] = preg_match("/[^\d+]/i", $value) === 0; |
68
|
|
|
return (bool) array_product($conditions); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Format examples: (555) 555-5555, 1 (555) 555-5555, 1-555-555-5555, 555-555-5555, 1 555 555-5555 |
73
|
|
|
* https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries |
74
|
|
|
* @param string $value The phone number to check |
75
|
|
|
* @return boolean is it correct format? |
76
|
|
|
*/ |
77
|
|
|
protected function isNANP($value) |
78
|
|
|
{ |
79
|
|
|
$conditions = []; |
80
|
|
|
$conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0; |
81
|
|
|
return (bool) array_product($conditions); |
82
|
|
|
} |
83
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.