20steps/libphonenumber-for-php
PHP port/wrapper of Google’s libphonenumber, providing tools to parse, validate, and format international phone numbers, detect regions and number types, and handle country calling codes for consistent phone handling in your apps.
user.created) to validate phone numbers in real-time via observers/listeners (Laravel Events).app('phone')->parse($number)).ext-intl for full functionality (international phone number support). Must document this in composer.json and CI/CD pipelines.$this->app->singleton('phone', function () {
return new \libphonenumber\PhoneNumberUtil();
});
use libphonenumber\PhoneNumberUtil;
Validator::extend('valid_phone', function ($attribute, $value, $parameters, $validator) {
$phoneUtil = app('phone');
try {
return $phoneUtil->parse($value, $parameters[0] ?? null);
} catch (\Exception $e) {
return false;
}
});
creating/updating:
class UserObserver {
public function creating(User $user) {
$phoneUtil = app('phone');
$parsed = $phoneUtil->parse($user->phone);
$user->phone = $phoneUtil->format($parsed, \libphonenumber\PhoneNumberFormat::E164);
}
}
Route::post('/validate-phone', function (Request $request) {
$phoneUtil = app('phone');
$parsed = $phoneUtil->parse($request->phone);
return response()->json([
'valid' => true,
'e164' => $phoneUtil->format($parsed, \libphonenumber\PhoneNumberFormat::E164),
'country' => $phoneUtil->getRegionCodeForNumber($parsed),
]);
});
composer.json constraints). Align with Laravel’s LTS support (e.g., Laravel 9+).+12125551234) for consistency.country_code, national_number) if needed.ext-intl extension is enabled in production (php -m | grep intl).composer require 20steps/libphonenumber-for-php.ext-intl in php.ini and Dockerfiles.PHPUnit).logger()->debug('Parsed:', $parsed->toString())).try-catch blocks to handle NumberParseException gracefully.+44 20 1234 5678 vs. 02012345678).PhoneNumberUtil instance in a singleton if thread safety is a concern (though PHP’s process model mitigates this).country_code) if queried frequently.| Failure Scenario
How can I help you explore Laravel packages today?