giggsey/libphonenumber-for-php
PHP port of Google’s libphonenumber for parsing, formatting, validating, and storing international phone numbers. Supports geocoding, carrier and timezone mapping, plus short-number info. Composer install; requires mbstring.
libphonenumber metadata, ensuring consistency with global telecom standards. Critical for internationalized applications (e.g., SaaS platforms with global users).NumberParseException) and lenient validation (e.g., isValidNumber()), accommodating varying business rules.libphonenumber releases. Mitigate via CI/CD checks for metadata updates.en_US). Ensure your app handles fallback locales gracefully.composer run build or CI triggers.NumberParseException be caught globally (e.g., middleware) or handled per-use-case?giggsey/libphonenumber-for-php-lite if carrier/geocoding features are unused (smaller footprint).AppServiceProvider:
$this->app->singleton(\libphonenumber\PhoneNumberUtil::class, fn() => \libphonenumber\PhoneNumberUtil::getInstance());
PhoneNumber) to simplify usage:
use Illuminate\Support\Facades\Facade;
class PhoneNumber extends Facade { protected static function getFacadeAccessor() { return \libphonenumber\PhoneNumberUtil::class; } }
use libphonenumber\PhoneNumberUtil;
Rule::macro('valid_phone', function ($attribute, $value, $region = null) {
$phoneUtil = PhoneNumberUtil::getInstance();
try {
return $phoneUtil->isValidNumber($phoneUtil->parse($value, $region));
} catch (\libphonenumber\NumberParseException) {
return false;
}
});
+14155552671) in the DB to avoid re-parsing.format($number, E164) for consistency.libphonenumber (e.g., in registration/login forms).+44 20 1234 5678, 011-81-3-1234-5678).composer run build).giggsey/locale is a minor dependency; monitor for breaking changes.NumberParseException details for troubleshooting (e.g., invalid regions).libphonenumber-for-php and libphonenumber (Google’s repo).phone:{e164}).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Stale metadata (e.g., new country codes) | Invalid parsing for unsupported regions | Automate metadata updates via CI/CD; monitor Google’s release notes. |
| Invalid input (e.g., malformed numbers) | App crashes or silent failures | Use try-catch blocks; implement fallback regex for critical paths. |
Locale mismatch (e.g., fr_FR vs fr_CA) |
Incorrect geocoding/carrier names | Default to en_US; log warnings for unsupported locales. |
| PHP version incompatibility | Package fails to load | Pin PHP version in composer.json; align with Laravel’s support matrix. |
| Database schema changes | Migration failures | Use Laravel migrations to add E.164 columns; backfill existing data. |
How can I help you explore Laravel packages today?