- How do I integrate this package into Laravel’s form validation?
- Extend Laravel’s validation by creating a custom rule. Use `PhoneNumberUtil::getInstance()` to parse and validate numbers, then return `true` or `false` based on `isValidNumber()`. Example: `return $phoneUtil->isValidNumber($phoneUtil->parse($value, $region));`. This works seamlessly with Laravel’s `FormRequest` validation.
- Will this work with Laravel’s Eloquent models for storing phone numbers?
- Yes. Normalize phone numbers to E.164 format before storing, then use Eloquent accessors/mutators to parse and format them dynamically. Example: `protected $casts = ['phone_number' => 'phone_number:e164'];` ensures consistent storage and retrieval.
- What’s the difference between this package and the full `giggsey/libphonenumber-for-php`?
- This *lite* version focuses on core utilities like parsing, validation, and formatting. The full package includes geolocation, carrier info, and short-number support. Use this if you only need validation/formatting to save space and improve performance.
- Does this package support Laravel’s dependency injection (DI) container?
- Yes, but it’s stateless. Use `PhoneNumberUtil::getInstance()` directly or wrap it in a Laravel service class for DI. Example: `app()->bind(PhoneNumberService::class, fn() => new PhoneNumberService());` for cleaner integration.
- How do I handle invalid phone numbers in production?
- Catch `NumberParseException` during parsing and log errors. Prompt users to correct invalid inputs or store them as `null` in your database. For critical systems, implement fallback logic like default country assumptions or manual review.
- What PHP versions and extensions are required?
- This package requires **PHP 8.1+** and the **mbstring** extension. Check compatibility with your Laravel version (Laravel 9+ supports PHP 8.1). If `mbstring` isn’t enabled, enable it in `php.ini` or configure your hosting provider.
- Can I use this for real-time phone number formatting (e.g., as users type)?
- Yes, but the lite version lacks the `AsYouTypeFormatter`. For real-time formatting, use the full package or implement a client-side library (e.g., Google’s libphonenumber JS) alongside this for validation. Cache formatted results server-side for performance.
- How do I test phone number validation across different regions?
- Use `PhoneNumberUtil::getExampleNumberByType()` to generate test numbers for regions like US, UK, or India. Write PHPUnit tests with assertions like `assertTrue($phoneUtil->isValidNumber($number))`. Include edge cases like toll-free or VoIP numbers.
- Is this package thread-safe for Laravel’s request lifecycle?
- Yes, the singleton `PhoneNumberUtil` is stateless and safe for Laravel’s single-threaded request lifecycle. Avoid multi-threading (e.g., queues) unless you synchronize access, as concurrent modifications could cause issues in rare edge cases.
- How do I migrate from ad-hoc phone validation to this package?
- Replace custom regex or validation logic with `isValidNumber()`. Start by validating new inputs, then update existing records to normalize them in E.164 format. Use Laravel migrations to add columns or alter data types as needed.