Documentation
¶
Overview ¶
Package phone is the Go port of propaganistas/laravel-phone for phone number parsing, validation, formatting and model storage.
It is a port of propaganistas/laravel-phone (https://github.com/Propaganistas/Laravel-Phone) to Laravel for Go.
Example ¶
number, err := phone.Parse("0470 12 34 56", "BE")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(number)
Output: +32470123456
Index ¶
- Variables
- func FormatNumber(raw string, format Format, countries ...string) (string, error)
- func ValidCountry(country string) bool
- type Format
- type Number
- func (n Number) Country() string
- func (n Number) E164() (string, error)
- func (n Number) Equal(other Number) bool
- func (n Number) Format(format Format) (string, error)
- func (n Number) FormatForCountry(country string) (string, error)
- func (n Number) FormatForMobileDialing(country string, withFormatting bool) (string, error)
- func (n Number) InCountry(countries ...string) bool
- func (n Number) International() (string, error)
- func (n Number) IsType(types ...Type) (bool, error)
- func (n Number) IsZero() bool
- func (n Number) MarshalJSON() ([]byte, error)
- func (n Number) MarshalText() ([]byte, error)
- func (n Number) National() (string, error)
- func (n Number) NotEqual(other Number) bool
- func (n Number) Possible() bool
- func (n Number) RFC3966() (string, error)
- func (n Number) Raw() string
- func (n Number) String() string
- func (n Number) Type() (Type, error)
- func (n *Number) UnmarshalJSON(data []byte) error
- func (n *Number) UnmarshalText(text []byte) error
- func (n Number) Valid() bool
- func (n Number) WithLeniency(enable bool) Number
- type Type
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidCountry = errors.New("phone: unsupported dialing country")
ErrInvalidCountry means a dialing origin is not a supported geographic region.
var ErrInvalidFormat = errors.New("phone: unsupported format")
ErrInvalidFormat means the format is outside PHP's PhoneNumberFormat enumeration.
var ErrInvalidType = errors.New("phone: unsupported number type")
ErrInvalidType means the type is outside PHP's PhoneNumberType enumeration.
var ErrUnparseable = errors.New("phone: cannot parse number")
ErrUnparseable means libphonenumber could not parse the input using the supplied hints.
Functions ¶
func FormatNumber ¶
FormatNumber formats raw input in one call, like PHP's phone helper with a format.
func ValidCountry ¶
ValidCountry accepts the same supported, case-insensitive region codes as PHP's isValidCountry. The non-geographic region "001" is not a geographic hint.
Types ¶
type Format ¶
type Format string
Format is a format from PHP's PhoneNumberFormat enumeration.
const ( // E164 is a plus-prefixed international number without spaces or extension. E164 Format = "E164" // International is a formatted number with its international calling code. International Format = "INTERNATIONAL" // National is a formatted number as dialed domestically. National Format = "NATIONAL" // RFC3966 is a telephone URI, including the extension if present. RFC3966 Format = "RFC3966" )
func ParseFormat ¶
ParseFormat is PHP's normalizeFormat. It accepts a case-insensitive name.
type Number ¶
type Number struct {
// contains filtered or unexported fields
}
Number is the Go equivalent of PHP's PhoneNumber. It keeps the original input, and is safe to copy and share. The zero value represents absence.
func New ¶
New wraps raw text like PHP's PhoneNumber constructor. Use Parse to get a parse error up front, or New to retain the text for display on failure.
func Parse ¶
Parse constructs a Number and reports whether it can be parsed. A parseable value may still fail Valid. Country hints are tried in order for national input.
func (Number) Country ¶
Country is PHP's getCountry. It returns an empty string when the region cannot be determined, or "001" for global services.
func (Number) Equal ¶
Equal is PHP's equals. It compares E.164 values, ignoring extensions. An unparseable value never matches, including another copy of itself.
func (Number) Format ¶
Format is PHP's PhoneNumber::format. Choose a format constant or use ParseFormat to resolve a name such as "national".
func (Number) FormatForCountry ¶
FormatForCountry is PHP's formatForCountry. It formats the number as dialed from the given geographic region.
func (Number) FormatForMobileDialing ¶
FormatForMobileDialing is PHP's formatForMobileDialingInCountry. It may return an empty string without error when the number cannot be dialed.
func (Number) InCountry ¶
InCountry is PHP's isOfCountry. It resolves the original text again with the supplied hints, then checks whether the detected region is among them.
func (Number) International ¶
International is PHP's formatInternational.
func (Number) MarshalJSON ¶
MarshalJSON is PHP's jsonSerialize and toJson. It emits an E.164 string for a parseable number or null for the zero value.
func (Number) MarshalText ¶
MarshalText encodes the E.164 value, the contents of PHP's serialized number.
func (Number) Possible ¶
Possible checks the libphonenumber length rules, as in PHP's lenient isValid.
func (Number) String ¶
String is PHP's __toString. It returns E.164 or the original input on a parse error.
func (Number) Type ¶
Type is PHP's getType. For a parseable value outside a known range, it returns Unknown without an error.
func (*Number) UnmarshalJSON ¶
UnmarshalJSON reads an international number or null. On failure it leaves n unchanged.
func (*Number) UnmarshalText ¶
UnmarshalText reads an international number. On failure it leaves n unchanged.
func (Number) Valid ¶
Valid is PHP's isValid. Strict mode checks the number in its selected region. Lenient mode checks whether its length is possible for the numbering plan.
func (Number) WithLeniency ¶
WithLeniency is the value-copy equivalent of PHP's lenient method. In lenient mode, national region selection and Valid use the possibility test.
type Type ¶
type Type string
Type is a type name from PHP's PhoneNumberType enumeration.
const ( // FixedLine identifies fixed-line numbers. FixedLine Type = "FIXED_LINE" // Mobile identifies mobile numbers. Mobile Type = "MOBILE" // FixedLineOrMobile covers ranges where the two types cannot be distinguished. FixedLineOrMobile Type = "FIXED_LINE_OR_MOBILE" // TollFree identifies freephone numbers. TollFree Type = "TOLL_FREE" // PremiumRate identifies premium-rate numbers. PremiumRate Type = "PREMIUM_RATE" SharedCost Type = "SHARED_COST" // VoIP identifies voice-over-IP numbers. VoIP Type = "VOIP" // PersonalNumber identifies personal numbers that can route calls. PersonalNumber Type = "PERSONAL_NUMBER" // Pager identifies pager numbers. Pager Type = "PAGER" // UAN identifies universal access numbers. UAN Type = "UAN" // Voicemail identifies voice-mail access numbers. Voicemail Type = "VOICEMAIL" // Unknown identifies numbers that match no known type. Unknown Type = "UNKNOWN" // Emergency is a PHP type name for short emergency codes. Emergency Type = "EMERGENCY" // ShortCode is a PHP type name for short numbers. ShortCode Type = "SHORT_CODE" // StandardRate is a PHP type name for short numbers charged at standard rate. StandardRate Type = "STANDARD_RATE" )
Directories
¶
| Path | Synopsis |
|---|---|
|
Package phonemodel provides raw and E.164 model fields for Laravel for Go.
|
Package phonemodel provides raw and E.164 model fields for Laravel for Go. |
|
Package phonevalidation integrates phone number validation with Laravel for Go.
|
Package phonevalidation integrates phone number validation with Laravel for Go. |