- How do I integrate this package into a Laravel project for user phone validation?
- Use Laravel’s Service Container to bind the `PhoneNumberUtil` singleton in `AppServiceProvider`. Then create a custom validation rule (e.g., `ValidPhoneNumber`) extending Laravel’s `Rule` interface. Parse and validate numbers in your `User` model or registration forms using dependency injection.
- Does this package support Laravel’s validation system out of the box?
- No, but you can easily create a custom validation rule. Extend Laravel’s `Rule` interface and use `PhoneNumberUtil::parse()` and `isValidNumber()` to validate input. This integrates seamlessly with Laravel’s form requests and API validation.
- What Laravel versions does this package officially support?
- The package itself requires PHP 8.1+, but it works with any Laravel version supporting that PHP version. Tested compatibility extends to Laravel 9+ and 10+. Always check the package’s `composer.json` for minor version constraints.
- How can I cache parsed phone numbers to improve performance in Laravel?
- Cache the singleton `PhoneNumberUtil` instance in Laravel’s cache (e.g., Redis) or use PHP’s `static` property to avoid repeated instantiation. For bulk operations, queue jobs with cached results to reduce CPU load during parsing/formatting.
- What happens if a phone number is invalid or unparseable?
- The package throws a `NumberParseException` for unparseable input. Catch this exception in your validation logic and handle it gracefully—either reject the input, log it, or prompt the user to re-enter the number. Use `try-catch` blocks in custom validation rules.
- Can I use this package for SMS/OTP workflows in Laravel Notifications?
- Yes. Parse and validate phone numbers before sending SMS notifications via Laravel’s `Notification` system. Store normalized E164 numbers in your database to ensure consistency. Combine with packages like `vonage/client` or `twilio/sdk` for delivery.
- What’s the difference between this ‘lite’ version and the full `giggsey/libphonenumber-for-php`?
- This ‘lite’ version excludes geolocation, carrier info, and short number handling, focusing only on parsing, validation, and formatting. It’s smaller (~1MB vs ~5MB) and faster for core use cases. Use the full version only if you need carrier or location data.
- How do I handle edge cases like invalid country codes or complex numbering plans (e.g., China, India)?
- Use `PhoneNumberUtil::isValidNumberForRegion()` to validate against specific regions. For complex plans, implement fallback logic—default to E164 formatting if parsing fails. Test with known edge cases (e.g., `+86` for China) and log discrepancies for review.
- Is there a way to normalize phone numbers before storing them in a Laravel database?
- Yes. Use `PhoneNumberUtil::parse()` followed by `getNumberType()` or `formatE164()` to store consistent data. Add a Laravel model observer or accessor to normalize numbers on save/retrieve. Example: `$user->phone = $phoneUtil->formatE164($parsedNumber);`
- How often does the underlying Google libphonenumber data get updated, and what’s the rollback plan?
- Updates depend on Google’s libphonenumber releases (typically quarterly). Monitor the [changelog](https://github.com/giggsey/libphonenumber-for-php-lite/releases) and cache results locally with TTL (e.g., Redis) to mitigate breaking changes. Rollback by pinning to a specific version in `composer.json`.