1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-22 00:36:41 +02:00
crud_generator/vendor/github.com/bxcodec/faker/v3/phone.go
Nikitin Aleksandr a6ea64ee3e новый
2023-10-24 18:03:04 +03:00

109 lines
2.5 KiB
Go

package faker
import (
"fmt"
"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)
}