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