- How do I integrate this package with Laravel’s validation system for phone numbers?
- Create a custom validation rule extending Laravel’s `Rule` interface. Use `PhoneNumberUtil::getInstance()` to parse and validate numbers, then return `true` or `false` based on `isValidNumber()`. Example: `return $phoneUtil->isValidNumber($parsedNumber)`. This works seamlessly with Laravel’s `FormRequest` or manual validation arrays.
- What Laravel versions support this package, and why does it require PHP 8.1+?
- This package requires PHP 8.1+, so it’s compatible with Laravel 9+ (LTS) or later. PHP 8.1+ ensures modern type safety and performance optimizations, while Laravel 9+ aligns with its dependency requirements. If you’re on an older Laravel version, consider upgrading or using the full `libphonenumber-for-php` package.
- Can I use this for storing phone numbers in a database? How should I format them?
- Yes, store phone numbers in **E.164 format** (e.g., `+14155552671`) for consistency and deduplication. Use `PhoneNumberUtil::getInstance()->format($number, PhoneNumberFormat::E164)` before saving. This avoids regional formatting issues and simplifies queries. For display, reformat dynamically using `PhoneNumberFormat::NATIONAL` or `INTERNATIONAL`.
- How do I handle invalid phone numbers in Laravel forms? Should I reject them entirely?
- Decide based on your use case: **Strict mode** (reject invalid numbers) uses `isValidNumber()` and returns `false` for malformed inputs. **Lenient mode** parses numbers with `parse()` and falls back to defaults (e.g., `+XX` prefix) if validation fails. Log edge cases (e.g., toll-free vs. mobile) using `getNumberType()` for debugging.
- What’s the performance impact of parsing thousands of phone numbers in Laravel?
- The package is optimized for PHP and avoids external dependencies, making it lightweight (~1MB memory for metadata). For high-volume apps (e.g., 10K+ requests/sec), cache the `PhoneNumberUtil` instance as a Laravel singleton to avoid reinitialization. Benchmark with tools like Blackfire to validate latency under load.
- How do I format phone numbers dynamically based on the user’s locale in API responses?
- Use Laravel’s `ApiResource` transformers to format numbers on-the-fly. Pass the user’s locale (e.g., `en_US`, `fr_FR`) to `PhoneNumberUtil::format($number, PhoneNumberFormat::NATIONAL)` or `INTERNATIONAL`. Example: `return $this->formatPhone($user->phone_number, request()->locale)`. This ensures consistency with regional expectations.
- What should I do if a phone number format isn’t supported (e.g., a rare country code)?
- Handle unsupported regions gracefully: **Strict mode** throws exceptions (catch `NumberParseException`). **Lenient mode** returns raw input with a warning or defaults to `+XX` format. Test edge cases using Google’s [online demo](https://libphonenumber.com/) and log discrepancies for manual review. Avoid hardcoding fallbacks for unsupported regions.
- Can I use this package for GDPR compliance or phone number auditing in Laravel?
- Yes, log validation events using Laravel’s `Log` facade or Eloquent observers. Track parsed numbers, validation results, and timestamps in a `phone_number_logs` table. For compliance, ensure your storage adheres to GDPR principles (e.g., anonymize logs post-retention). Combine with Laravel’s `HasEvents` trait to trigger audits during model updates.
- What’s the difference between this ‘lite’ version and the full `libphonenumber-for-php` package?
- This **lite** version includes only core functionality: parsing, validation, formatting, and E.164 normalization. The full package adds geolocation, carrier info, and short number support. Choose the lite version for Laravel apps needing lightweight phone handling (e.g., user profiles) and the full package for advanced features like carrier lookup.
- How do I test phone number validation logic in Laravel’s unit tests?
- Mock `PhoneNumberUtil` using Laravel’s `Mockery` or PHPUnit’s `createMock()`. Test edge cases like invalid formats (`123`), valid but unusual numbers (`+44 20 7946 0958`), and unsupported regions. Example: `assertFalse($phoneUtil->isValidNumber($invalidNumber))`. Compare results against Google’s validation rules to ensure accuracy.