illuminate/support compatibility layers or Symfony bridge packages). Laravel’s service container and dependency injection differ from Symfony’s, requiring abstraction or wrapper logic.AppKernel, Twig extensions tied to Symfony’s Twig environment). Laravel’s Twig integration (via laravelcollective/html) would need adaptation.PhoneFormatterHelper using the underlying giggsey/libphonenumber-for-php library (direct dependency).symfony/http-foundation or symfony/dependency-injection in Laravel (e.g., via spatie/laravel-symfony-support), but this adds complexity.Str::of($number)->phoneFormat() or a PhoneFormatter facade).libphonenumber-for-php@7.7 (now at v8+). Potential breaking changes if upgrading dependencies.ContainerInterface ≠ Laravel’s Container. Requires manual binding or a facade.libphonenumber-for-php is robust but may introduce overhead for high-throughput APIs.libphonenumber-for-php v8+ compatible? If not, can we fork or patch the bundle?Illuminate\Validation\Rule for phone numbers).egulias/email-validator for phone numbers, or a custom regex-based solution).giggsey/libphonenumber-for-php dependency is the most valuable part. Laravel can use this directly without the Symfony bundle.laravelcollective/html, the Twig extension would need a Laravel-compatible rewrite (e.g., via a custom TwigExtension service).@phoneFormat($number, 'INTERNATIONAL')) or helper functions (phone_format($number)).PhoneFormatter::format($number)).Phase 1: Extract Core Logic
libphonenumber-for-php usage:
use libphonenumber\PhoneNumberUtil;
use libphonenumber\PhoneNumberFormat;
$util = PhoneNumberUtil::getInstance();
$number = $util->parse($rawNumber, $countryCode);
echo $util->format($number, PhoneNumberFormat::NATIONAL);
app/Services/PhoneFormatter.php) to wrap this logic.Phase 2: Laravel Integration
$this->app->singleton(PhoneFormatter::class, function ($app) {
return new PhoneFormatter();
});
Phone facade for easy access.{{ phone_format($number, 'INTERNATIONAL') }} or @phone($number)).Phase 3: Deprecate Bundle
AppKernel, bundle registration).libphonenumber-for-php.symfony/symfony; use only giggsey/libphonenumber-for-php.DE, FR). Test edge cases like invalid inputs.libphonenumber-for-php@7.7 compatibility with Laravel’s PHP version.libphonenumber-for-php for breaking changes (e.g., v8+). May require periodic updates.PhoneFormatter service.InvalidPhoneNumberException).libphonenumber-for-php is CPU-intensive for parsing/validation. Cache parsed numbers in Laravel’s cache (e.g., Cache::remember()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid country code | Formatting fails or returns garbage | Validate country codes upfront; use defaults. |
| Malformed phone number | Exception or incorrect output | Sanitize input; use regex pre-validation. |
libphonenumber-for-php update |
Breaking changes | Test in staging; use semantic versioning. |
| High traffic | Slow parsing times | Cache parsed numbers; offload to queues. |
| Twig/Blade helper errors | Frontend rendering fails | Graceful fallbacks (e.g., show raw number). |
PhoneFormatter service in Laravel’s internal wiki.PhoneFormatter service (cover edge cases).How can I help you explore Laravel packages today?