PhoneNumber.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Faker\Provider\en_US;
class PhoneNumber extends \Faker\Provider\PhoneNumber
{
/**
* @var array<int, string>
*/
protected static $areaCodeRegexes = [
2 => '(0[1-35-9]|1[02-9]|2[03-589]|3[149]|4[08]|5[1-46]|6[0279]|7[0269]|8[13])',
3 => '(0[1-57-9]|1[02-9]|2[0135]|3[0-24679]|4[167]|5[12]|6[014]|8[056])',
4 => '(0[124-9]|1[02-579]|2[3-5]|3[0245]|4[0235]|58|6[39]|7[0589]|8[04])',
5 => '(0[1-57-9]|1[0235-8]|20|3[0149]|4[01]|5[19]|6[1-47]|7[013-5]|8[056])',
6 => '(0[1-35-9]|1[024-9]|2[03689]|[34][016]|5[017]|6[0-279]|78|8[0-29])',
7 => '(0[1-46-8]|1[2-9]|2[04-7]|3[1247]|4[037]|5[47]|6[02359]|7[02-59]|8[156])',
8 => '(0[1-68]|1[02-8]|2[08]|3[0-28]|4[3578]|5[046-9]|6[02-5]|7[028])',
9 => '(0[1346-9]|1[02-9]|2[0589]|3[0146-8]|4[0179]|5[12469]|7[0-389]|8[04-69])',
];
/**
* @see https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries
*/
protected static $formats = [
// International format
'+1-{{areaCode}}-{{exchangeCode}}-####',
'+1 ({{areaCode}}) {{exchangeCode}}-####',
'+1-{{areaCode}}-{{exchangeCode}}-####',
'+1.{{areaCode}}.{{exchangeCode}}.####',
'+1{{areaCode}}{{exchangeCode}}####',
// Standard formats
'{{areaCode}}-{{exchangeCode}}-####',
'({{areaCode}}) {{exchangeCode}}-####',
'1-{{areaCode}}-{{exchangeCode}}-####',
'{{areaCode}}.{{exchangeCode}}.####',
'{{areaCode}}-{{exchangeCode}}-####',
'({{areaCode}}) {{exchangeCode}}-####',
'1-{{areaCode}}-{{exchangeCode}}-####',
'{{areaCode}}.{{exchangeCode}}.####',
];
protected static $formatsWithExtension = [
'{{areaCode}}-{{exchangeCode}}-#### x###',
'({{areaCode}}) {{exchangeCode}}-#### x###',
'1-{{areaCode}}-{{exchangeCode}}-#### x###',
'{{areaCode}}.{{exchangeCode}}.#### x###',
'{{areaCode}}-{{exchangeCode}}-#### x####',
'({{areaCode}}) {{exchangeCode}}-#### x####',
'1-{{areaCode}}-{{exchangeCode}}-#### x####',
'{{areaCode}}.{{exchangeCode}}.#### x####',
'{{areaCode}}-{{exchangeCode}}-#### x#####',
'({{areaCode}}) {{exchangeCode}}-#### x#####',
'1-{{areaCode}}-{{exchangeCode}}-#### x#####',
'{{areaCode}}.{{exchangeCode}}.#### x#####',
];
protected static $e164Formats = [
'+1{{areaCode}}{{exchangeCode}}####',
];
/**
* @see https://en.wikipedia.org/wiki/Toll-free_telephone_number#United_States
*/
protected static $tollFreeAreaCodes = [
800, 844, 855, 866, 877, 888,
];
protected static $tollFreeFormats = [
// Standard formats
'{{tollFreeAreaCode}}-{{exchangeCode}}-####',
'({{tollFreeAreaCode}}) {{exchangeCode}}-####',
'1-{{tollFreeAreaCode}}-{{exchangeCode}}-####',
'{{tollFreeAreaCode}}.{{exchangeCode}}.####',
];
public function tollFreeAreaCode()
{
return self::randomElement(static::$tollFreeAreaCodes);
}
public function tollFreePhoneNumber()
{
$format = self::randomElement(static::$tollFreeFormats);
return self::numerify($this->generator->parse($format));
}
/**
* @return string
*
* @example '555-123-546 x123'
*/
public function phoneNumberWithExtension()
{
return static::numerify($this->generator->parse(static::randomElement(static::$formatsWithExtension)));
}
/**
* NPA-format area code
*
* @see https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Numbering_system
*
* @return string
*/
public static function areaCode()
{
$firstDigit = self::numberBetween(2, 9);
return $firstDigit . self::regexify(self::$areaCodeRegexes[$firstDigit]);
}
/**
* NXX-format central office exchange code
*
* @see https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Numbering_system
*
* @return string
*/
public static function exchangeCode()
{
$digits[] = self::numberBetween(2, 9);
$digits[] = self::randomDigit();
if ($digits[1] === 1) {
$digits[] = self::randomDigitNot(1);
} else {
$digits[] = self::randomDigit();
}
return implode('', $digits);
}
}