Laravel for Go Alpha

laravel-go-phone

From a local phone number to a consistent E.164 value

Accept numbers in the formats people use locally. With a Belgian country hint, 0470 12 34 56 becomes +32470123456 for storage and lookup.

Read the docs Source on GitHub

go get github.com/bowerbird-si/laravel-go-phone@v0.1.0
Latest release
0.1.0, 2026-12-04
Maintenance
Active
Framework
go.laravel.com v0.2.0 or later
Go
Go 1.27 or later
Licence
MIT

PHP parity

Follows propaganistas/laravel-phone 6.1.0 (commit 7433d79ea4b1). Last verified .

Parity level
Full
Abilities
51
Covered
30
Diverged
21
Planned
0
PHP fixtures
166
Coverage by ability
  • number.construct · Diverged · Core
    PhoneNumber::__construct()
  • number.country · Covered · Core · 9 tests
    PhoneNumber::getCountry()
  • number.is-of-country · Covered · Core · 4 tests
    PhoneNumber::isOfCountry()
  • number.valid-country · Covered · 6 tests
    PhoneNumber::isValidCountry()
  • number.type · Covered · 5 tests
    PhoneNumber::getType()
  • number.is-of-type · Covered · 3 tests
    PhoneNumber::isOfType()
  • number.normalize-type · Covered · 5 tests
    PhoneNumber::normalizeType()
  • number.format · Covered · 2 tests
    PhoneNumber::format()
  • number.format-international · Covered · 1 test
    PhoneNumber::formatInternational()
  • number.format-national · Covered · Core · 1 test
    PhoneNumber::formatNational()
  • number.format-e164 · Covered · Core · 10 tests
    PhoneNumber::formatE164()
  • number.format-rfc3966 · Covered · 1 test
    PhoneNumber::formatRFC3966()
  • number.normalize-format · Covered · 2 tests
    PhoneNumber::normalizeFormat()
  • number.format-from-country · Covered · 5 tests
    PhoneNumber::formatForCountry()
  • number.format-mobile-dialing · Covered · 7 tests
    PhoneNumber::formatForMobileDialingInCountry()
  • number.valid · Covered · Core · 7 tests
    PhoneNumber::isValid()
  • number.lenient · Diverged · Core
    PhoneNumber::lenient()
  • number.equals · Covered · 3 tests
    PhoneNumber::equals()
  • number.not-equals · Covered · 3 tests
    PhoneNumber::notEquals()
  • number.raw · Covered · 1 test
    PhoneNumber::getRawNumber()
  • number.libphone-object · Diverged
    PhoneNumber::toLibPhoneObject()
  • number.to-json · Diverged
    PhoneNumber::toJson()
  • number.json-serialize · Covered · 1 test
    PhoneNumber::jsonSerialize()
  • number.serialize · Diverged
    PhoneNumber::__serialize()
  • number.unserialize · Diverged
    PhoneNumber::__unserialize()
  • number.display · Covered · 2 tests
    PhoneNumber::__toString()
  • number.macros · Diverged
    PhoneNumber::macro()
  • helper.phone · Diverged
    phone()
  • rule.data · Diverged
    Rules\Phone::setData()
  • rule.validate · Diverged · Core
    Rules\Phone::validate()
  • rule.country · Covered · 3 tests
    Rules\Phone::country()
  • rule.country-field · Covered · 4 tests
    Rules\Phone::countryField()
  • rule.type · Covered · 3 tests
    Rules\Phone::type()
  • rule.not-type · Covered · 2 tests
    Rules\Phone::notType()
  • rule.mobile · Covered · 1 test
    Rules\Phone::mobile()
  • rule.fixed-line · Covered · 1 test
    Rules\Phone::fixed_line()
  • rule.lenient · Covered · 1 test
    Rules\Phone::lenient()
  • rule.international · Covered · 2 tests
    Rules\Phone::international()
  • rule.parameters · Diverged
    Rules\Phone::setParameters()
  • rule.default-country · Diverged
    Rules\Phone::setDefaultCountry()
  • rule.alias · Diverged
    phone validation alias
  • rule.factory · Covered · Core · 1 test
    Rule::phone()
  • rule.translation · Diverged
    validation.phone message
  • provider.discovery · Diverged
    PhoneServiceProvider registration
  • cast.country-hints · Diverged
    PhoneNumberCast::getPossibleCountries()
  • cast.e164.get · Diverged
    E164PhoneNumberCast::get()
  • cast.e164.set · Diverged · Core
    E164PhoneNumberCast::set()
  • cast.e164.serialize · Covered · 2 tests
    E164PhoneNumberCast::serialize()
  • cast.raw.get · Diverged
    RawPhoneNumberCast::get()
  • cast.raw.set · Diverged
    RawPhoneNumberCast::set()
  • cast.raw.serialize · Diverged
    RawPhoneNumberCast::serialize()
Differences from PHP (21)
PhoneNumber::__construct()
The shared Go parser accepts more Unicode decimal digits and measures the maximum input length in bytes. PHP rejects the Devanagari example and accepts a padded input that exceeds the Go byte limit. This also affects region detection, validation and formatting of these inputs.
PhoneNumber::lenient()
WithLeniency returns a value copy so changing the mode cannot modify another caller's number.
PhoneNumber::toLibPhoneObject()
The parsed libphonenumber object is private to keep the Go API independent of its backend. Parsing the RFC 3966 output with libphonenumber recovers the calling code, national number and extension.
PhoneNumber::toJson()
Go always emits a JSON string so the E.164 plus sign is preserved. PHP supports JSON_NUMERIC_CHECK, which converts the value to a number.
PhoneNumber::__serialize()
Go text encoding stores the E.164 string. PHP's serialized object format is specific to PHP.
PhoneNumber::__unserialize()
Go restores a number from standard text or JSON encoding. PHP's serialized object format is specific to PHP.
PhoneNumber::macro()
Go has compile-time method sets. Use an ordinary function to add application behavior.
phone()
New and FormatNumber are separate Go functions with distinct return types. PHP selects the return type with an optional format argument.
Rules\Phone::setData()
A non-string dependent country produces a validation failure. PHP throws a TypeError for an array-valued country.
Rules\Phone::validate()
Inputs must be strings. Laravel for Go skips explicit null on a non-implicit rule, so compose the phone rule with Laravel's presence rules when necessary.
Rules\Phone::setParameters()
Use typed builder methods, with field paths distinct from literal countries. Go does not reinterpret options based on a collision with request input keys.
Rules\Phone::setDefaultCountry()
Defaults are configured on an immutable rule value instead of being process-wide mutable state.
phone validation alias
Attach a typed rule with Laravel for Go's rule.Field. It does not evaluate PHP rule strings.
validation.phone message
Pass the application's localized message through Laravel for Go's rule.Message wrapper instead of looking it up in PHP's translator.
PhoneServiceProvider registration
Use the typed rule directly in the form. It does not need Composer discovery or registration with a validator factory.
PhoneNumberCast::getPossibleCountries()
Go model code passes ordered literal hints and sibling country field values explicitly. SQL scanner fields do not have access to other columns.
E164PhoneNumberCast::get()
The stored text 0 is rejected as malformed. PHP treats it as an absent value.
E164PhoneNumberCast::set()
The write rejects malformed or regionless values so that every stored phone can be read back. PHP accepts some parseable regionless writes and treats 0 as absent.
RawPhoneNumberCast::get()
Model code resolves the text with supplied hints after scanning. The stored text 0 raises a conversion error instead of becoming absent.
RawPhoneNumberCast::set()
A nil raw field writes SQL NULL. PHP's raw setter coerces null into empty text.
RawPhoneNumberCast::serialize()
Go marshals the stored raw string or a nil pointer as-is. PHP's raw cast first resolves a phone and treats empty and 0 as absent.

In practice

Convert a Belgian number to E.164

Pass BE as the country hint for the national number.

package phone_test

import (
	"fmt"

	"github.com/bowerbird-si/laravel-go-phone"
)

func Example() {
	number, err := phone.Parse("0470 12 34 56", "BE")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(number)
	// Output: +32470123456
}

Validate a mobile number in a form

The rule reads phone_country next to phone and checks that the number is mobile.

package phonevalidation_test

import (
	"context"
	"fmt"

	phonevalidation "github.com/bowerbird-si/laravel-go-phone/validation"
	"go.laravel.com/validation"
	"go.laravel.com/validation/rule"
)

func Example() {
	form := struct {
		Phone   string `json:"phone"`
		Country string `json:"phone_country"`
	}{Phone: "0470 12 34 56", Country: "BE"}

	errs, err := validation.Validate(context.Background(), &form,
		rule.Field(&form.Phone, rule.Required, phonevalidation.Rule().Mobile()),
	)
	fmt.Println(len(errs), err)
	// Output: 0 <nil>
}

Built on

  • Laravel-Phone: The package this ports: parsing, validation rules, formatting and model storage, following version 6.1.0.
  • phonenumbers: Phone number parsing and the numbering-plan metadata.

Go modules: github.com/bowerbird-si/laravel-go-phone · Upstream notices

Documentation