PhoneNumber.php
2.8 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
<?php
namespace Faker\Provider\en_ZA;
class PhoneNumber extends \Faker\Provider\PhoneNumber
{
protected static $formats = [
'+27({{areaCode}})#######',
'+27{{areaCode}}#######',
'0{{areaCode}}#######',
'0{{areaCode}} ### ####',
'0{{areaCode}}-###-####',
];
protected static $cellphoneFormats = [
'+27{{cellphoneCode}}#######',
'0{{cellphoneCode}}#######',
'0{{cellphoneCode}} ### ####',
'0{{cellphoneCode}}-###-####',
];
protected static $specialFormats = [
'{{specialCode}}#######',
'{{specialCode}} ### ####',
'{{specialCode}}-###-####',
'({{specialCode}})###-####',
];
protected static $tollFreeAreaCodes = [
'0800', '0860', '0861', '0862',
];
/**
* @see https://en.wikipedia.org/wiki/Telephone_numbers_in_South_Africa
*/
public static function areaCode()
{
$digits[] = self::numberBetween(1, 5);
switch ($digits[0]) {
case 1:
$digits[] = self::numberBetween(1, 8);
break;
case 2:
$number = self::numberBetween(1, 8);
$digits[] = in_array($number, [5, 6], false) ? $number + 2 : $number;
break;
case 3:
$number = self::numberBetween(1, 8);
$digits[] = in_array($number, [7, 8], false) ? $number - 2 : $number;
break;
case 4:
$digits[] = self::numberBetween(1, 9);
break;
case 5:
$number = self::numberBetween(1, 8);
$digits[] = in_array($number, [2, 5], false) ? $number + 2 : $number;
break;
}
return implode('', $digits);
}
public static function cellphoneCode()
{
$digits[] = self::numberBetween(6, 8);
switch ($digits[0]) {
case 6:
$digits[] = self::numberBetween(0, 2);
break;
case 7:
$number = self::numberBetween(1, 9);
$digits[] = in_array($number, [5, 7], false) ? $number + 1 : $number;
break;
case 8:
$digits[] = self::numberBetween(1, 9);
break;
}
return implode('', $digits);
}
public static function specialCode()
{
return static::randomElement(static::$tollFreeAreaCodes);
}
public function mobileNumber()
{
$format = static::randomElement(static::$cellphoneFormats);
return self::numerify($this->generator->parse($format));
}
public function tollFreeNumber()
{
$format = static::randomElement(static::$specialFormats);
return self::numerify($this->generator->parse($format));
}
}