giggsey/libphonenumber-for-php, a mature PHP port of Google’s library, offering robust phone number parsing, validation, and formatting. This is a high-value technical fit for any application requiring phone number handling.AppKernel). Laravel’s composer-based autoloading and service providers offer more flexibility, but the bundle’s monolithic design may complicate adoption.composer require, but Laravel’s autoloader may need explicit namespace mapping or a custom autoload-dev entry.services.yml or annotations must be translated to Laravel’s service providers or bindings in the container. Example:
$this->app->bind('phone_number', function ($app) {
return new \libphonenumber\PhoneNumberUtil();
});
Kernel classes).EventDispatcher).phpstan) or a proof-of-concept wrapper layer.libphonenumber is resource-intensive for high-throughput systems. Benchmark parsing/formatting under expected load.giggsey/libphonenumber-for-php) directly to avoid maintenance gaps.HttpFoundation, DependencyInjection) that would require rewrites?giggsey/libphonenumber-for-php (without Symfony baggage) suffice?libphonenumber) is language-agnostic and can be integrated into Laravel via:
composer require giggsey/libphonenumber-for-php.PhoneNumberUtil as a singleton in Laravel’s container.PhoneNumber facade for cleaner syntax:
use Facades\PhoneNumber;
$number = PhoneNumber::parse('+1234567890');
MisdPhoneNumberBundle) must be extracted or replaced. Prioritize:
giggsey/libphonenumber-for-php directly in Laravel.$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$number = $phoneUtil->parse('+442071838750', 'GB');
echo $phoneUtil->format($number, \libphonenumber\PhoneNumberFormat::E164);
libphonenumber calls:
class LaravelPhoneNumber {
public function parse(string $number, string $region = null) { ... }
public function format(string $number, string $format) { ... }
}
AppServiceProvider.libphonenumber-for-php supports PHP 7.2+.// Model
protected $casts = ['phone_number' => 'string'];
// Accessor
public function getFormattedPhoneNumberAttribute() {
return (new LaravelPhoneNumber())->format($this->phone_number);
}
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
'phone' => ['required', function ($attribute, $value, $fail) {
if (!\libphonenumber\PhoneNumberUtil::getInstance()->isValidNumber(
\libphonenumber\PhoneNumberUtil::getInstance()->parse($value)
)) {
$fail('The phone number is invalid.');
}
}]
]);
libphonenumber’s impact.misd/phone-number-bundle) introduces technical debt.giggsey/libphonenumber-for-php or fork the bundle with Laravel-specific updates.composer why-not and depfu to monitor dependency updates.composer.json to avoid breaking changes.libphonenumber-for-php for major version updates (e.g., breaking changes in v9+).Container or EventDispatcher. Document Laravel-specific quirks.try {
$phoneUtil->parse($input);
} catch (\libphonenumber\NumberParseException $e) {
\Log::error("Phone number parse failed: {$e->getMessage()}");
}
libphonenumber-for-php’s GitHub issues.libphonenumber is CPU-intensive. Mitigate with:
k6 or Artillery.libphonenumber version across instances.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid phone number input | Application errors or data corruption | Input validation + graceful degradation (e.g., return null or default format). |
libphonenumber parsing errors |
Service downtime or incorrect data | Retry logic + circuit breaker (e.g., spatie/fork). |
How can I help you explore Laravel packages today?