forked from bxcodec/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.go
109 lines (90 loc) · 2.49 KB
/
phone.go
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
package faker
import (
"fmt"
"math/rand"
"reflect"
"strings"
"github.com/bxcodec/faker/v3/support/slice"
)
var phone Phoner
// GetPhoner serves as a constructor for Phoner interface
func GetPhoner() Phoner {
mu.Lock()
defer mu.Unlock()
if phone == nil {
phone = &Phone{}
}
return phone
}
// SetPhoner sets custom Phoner
func SetPhoner(p Phoner) {
phone = p
}
// Phoner serves overall tele-phonic contact generator
type Phoner interface {
PhoneNumber(v reflect.Value) (interface{}, error)
TollFreePhoneNumber(v reflect.Value) (interface{}, error)
E164PhoneNumber(v reflect.Value) (interface{}, error)
}
// Phone struct
type Phone struct {
}
func (p Phone) phonenumber() string {
randInt, _ := RandomInt(1, 10)
str := strings.Join(slice.IntToString(randInt), "")
return fmt.Sprintf("%s-%s-%s", str[:3], str[3:6], str[6:10])
}
// PhoneNumber generates phone numbers of type: "201-886-0269"
func (p Phone) PhoneNumber(v reflect.Value) (interface{}, error) {
return p.phonenumber(), nil
}
// Phonenumber get fake phone number
func Phonenumber() string {
return singleFakeData(PhoneNumber, func() interface{} {
p := Phone{}
return p.phonenumber()
}).(string)
}
func (p Phone) tollfreephonenumber() string {
out := ""
boxDigitsStart := []string{"777", "888"}
ints, _ := RandomInt(1, 9)
for index, v := range slice.IntToString(ints) {
if index == 3 {
out += "-"
}
out += v
}
return fmt.Sprintf("(%s) %s", boxDigitsStart[rand.Intn(1)], out)
}
// TollFreePhoneNumber generates phone numbers of type: "(888) 937-7238"
func (p Phone) TollFreePhoneNumber(v reflect.Value) (interface{}, error) {
return p.tollfreephonenumber(), nil
}
// TollFreePhoneNumber get fake TollFreePhoneNumber
func TollFreePhoneNumber() string {
return singleFakeData(TollFreeNumber, func() interface{} {
p := Phone{}
return p.tollfreephonenumber()
}).(string)
}
func (p Phone) e164PhoneNumber() string {
out := ""
boxDigitsStart := []string{"7", "8"}
ints, _ := RandomInt(1, 10)
for _, v := range slice.IntToString(ints) {
out += v
}
return fmt.Sprintf("+%s%s", boxDigitsStart[rand.Intn(1)], strings.Join(slice.IntToString(ints), ""))
}
// E164PhoneNumber generates phone numbers of type: "+27113456789"
func (p Phone) E164PhoneNumber(v reflect.Value) (interface{}, error) {
return p.e164PhoneNumber(), nil
}
// E164PhoneNumber get fake E164PhoneNumber
func E164PhoneNumber() string {
return singleFakeData(E164PhoneNumberTag, func() interface{} {
p := Phone{}
return p.e164PhoneNumber()
}).(string)
}