1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\Validator\Phone; |
4
|
|
|
|
5
|
|
|
class Validator |
6
|
|
|
{ |
7
|
18 |
|
public function __call($a, $b) |
8
|
|
|
{ |
9
|
18 |
|
return empty($b[0]) || $this->{$a}($b[0]); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Checks through all validation methods to verify it is in a |
14
|
|
|
* phone number format of some type |
15
|
|
|
* @param string $value The phone number to check |
16
|
|
|
* @return boolean is it correct format? |
17
|
|
|
*/ |
18
|
3 |
|
protected function isPhone($value) |
19
|
|
|
{ |
20
|
3 |
|
return $this->isE164($value) || $this->isNANP($value) || $this->isDigits($value); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Format example 5555555555, 15555555555 |
25
|
|
|
* @param [type] $value [description] |
|
|
|
|
26
|
|
|
* @return boolean [description] |
27
|
|
|
*/ |
28
|
6 |
|
protected function isDigits($value) |
29
|
|
|
{ |
30
|
6 |
|
$conditions = []; |
31
|
6 |
|
$conditions[] = strlen($value) >= 10; |
32
|
6 |
|
$conditions[] = strlen($value) <= 16; |
33
|
6 |
|
$conditions[] = preg_match("/[^\d]/i", $value) === 0; |
34
|
6 |
|
return (bool) array_product($conditions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Format example +15555555555 |
39
|
|
|
* @param string $value The phone number to check |
40
|
|
|
* @return boolean is it correct format? |
41
|
|
|
*/ |
42
|
9 |
|
protected function isE164($value) |
43
|
|
|
{ |
44
|
9 |
|
$conditions = []; |
45
|
9 |
|
$conditions[] = strpos($value, "+") === 0; |
46
|
9 |
|
$conditions[] = strlen($value) >= 9; |
47
|
9 |
|
$conditions[] = strlen($value) <= 16; |
48
|
9 |
|
$conditions[] = preg_match("/[^\d+]/i", $value) === 0; |
49
|
9 |
|
return (bool) array_product($conditions); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Format examples: (555) 555-5555, 1 (555) 555-5555, 1-555-555-5555, 555-555-5555, 1 555 555-5555 |
54
|
|
|
* https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries |
55
|
|
|
* @param string $value The phone number to check |
56
|
|
|
* @return boolean is it correct format? |
57
|
|
|
*/ |
58
|
6 |
|
protected function isNANP($value) |
59
|
|
|
{ |
60
|
6 |
|
$conditions = []; |
61
|
6 |
|
$conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0; |
62
|
6 |
|
return (bool) array_product($conditions); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.