composer require misd/phone-number-bundle
config/bundles.php:
return [
// ...
Misd\PhoneNumberBundle\MisdPhoneNumberBundle::class => ['all' => true],
];
use Misd\PhoneNumberBundle\PhoneNumber\PhoneNumberUtil;
public function validatePhone(Request $request)
{
$phoneUtil = new PhoneNumberUtil();
$phoneNumber = $phoneUtil->parse('+14155552671', 'US');
if ($phoneUtil->isValidNumber($phoneNumber)) {
$formatted = $phoneUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::E164);
return new JsonResponse(['valid' => true, 'formatted' => $formatted]);
}
return new JsonResponse(['valid' => false], 400);
}
phone_number.util service (check config/services.yaml).PhoneNumberUtil for parsing, validation, and formatting.Form Validation:
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\ValidPhoneNumber;
$builder->add('phone', TextType::class, [
'constraints' => [
new ValidPhoneNumber(['countryCode' => 'US']),
],
]);
Phone Number Parsing in Services:
public function __construct(private PhoneNumberUtil $phoneUtil) {}
public function processPhone(string $rawPhone): string
{
$phone = $this->phoneUtil->parse($rawPhone);
return $this->phoneUtil->format($phone, \libphonenumber\PhoneNumberFormat::NATIONAL);
}
Internationalization:
$phoneUtil = new PhoneNumberUtil();
$phone = $phoneUtil->parse('02079460000', 'GB'); // UK number
$geocode = $phoneUtil->getRegionCodeForNumber($phone); // Returns 'GB'
Doctrine Entity Integration (if extended):
use Misd\PhoneNumberBundle\Doctrine\Types\PhoneNumberType;
/**
* @ORM\Column(type="phone_number")
*/
private $phone;
Misd\PhoneNumberBundle\Form\Type\PhoneNumberType for form fields.PhoneNumberFormat::E164) for consistency.PhoneNumberUtil for unit tests:
$this->createMock(PhoneNumberUtil::class);
Country Code Ambiguity:
+1 vs. +44) may resolve to unexpected regions. Use setDefaultRegion() if needed:
$phoneUtil->setDefaultRegion('US');
Performance:
Deprecated Methods:
libphonenumber methods may change between versions. Check the giggsey/libphonenumber-for-php docs for updates.Doctrine Limitations:
// Example custom type (if not provided)
class PhoneNumberType extends Type {
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->getNationalNumber();
}
}
Invalid Numbers:
isValidNumber() to check validity. For edge cases, log the raw input and parsed output:
try {
$phone = $phoneUtil->parse($rawInput);
} catch (\libphonenumber\NumberParseException $e) {
// Handle exception (e.g., log $rawInput)
}
Region Mismatches:
getRegionCodeForNumber() returns unexpected results, verify the input format or use getNumberType() to check for mobile/landline.Custom Validation:
Extend the ValidPhoneNumber constraint or create a custom validator:
class CustomPhoneValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
$phoneUtil = new PhoneNumberUtil();
$phone = $phoneUtil->parse($value);
if (!$phoneUtil->isValidNumber($phone)) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
Phone Number Storage: Store parsed numbers in a normalized format (e.g., E164) in the database to avoid reprocessing:
$phoneUtil->format($phone, \libphonenumber\PhoneNumberFormat::E164);
Localization:
Override the default region for parsing by setting it in the PhoneNumberUtil instance:
$phoneUtil->setDefaultRegion('DE'); // Default to Germany
Testing:
Use the PhoneNumberUtil constructor to inject a custom PhoneNumberMetadata for testing:
$metadata = new PhoneNumberMetadata();
$phoneUtil = new PhoneNumberUtil($metadata);
How can I help you explore Laravel packages today?