Installation
composer require alpixel/phoneformatterbundle
Register the Bundle
Add to config/bundles.php (Symfony 3.3+):
return [
// ...
Alpixel\Bundle\PhoneFormatterBundle\AlpixelPhoneFormatterBundle::class => ['all' => true],
];
(For Symfony 2.x, update AppKernel.php as shown in the README.)
First Use Case Format a raw phone number in a controller:
use Alpixel\Bundle\PhoneFormatterBundle\Helper\PhoneNumberFormat;
$formatter = $this->get('alpixel.helper.phone_formatter');
$formatted = $formatter->format('+1234567890', PhoneNumberFormat::NATIONAL, 'US');
// Output: "(123) 456-7890"
Controller/Service Integration Inject the helper via dependency injection:
use Alpixel\Bundle\PhoneFormatterBundle\Helper\PhoneFormatterInterface;
class UserController extends Controller
{
public function __construct(PhoneFormatterInterface $formatter) {
$this->formatter = $formatter;
}
public function showProfile() {
$phone = $this->formatter->format($rawPhone, PhoneNumberFormat::E164, 'FR');
// ...
}
}
Twig Templating Format directly in views:
{{ user.phone|phone_format('NATIONAL', 'GB') }}
(Supports all formats: INTERNATIONAL, NATIONAL, E164, AUTO.)
Form Validation Clean and validate input before storage:
$cleaned = $formatter->format($request->get('phone'), PhoneNumberFormat::E164, 'DE');
if (!$cleaned) {
throw new \InvalidArgumentException('Invalid phone number');
}
Dynamic Locale Handling
Use AUTO format with the user’s locale:
$locale = $this->getUser()->getLocale();
$formatted = $formatter->format($rawPhone, PhoneNumberFormat::AUTO, $locale);
E164 for consistency.E164 format, display dynamically in the UI.Deprecated Symfony 2.x Support
Locale-Specific Formatting
US, GB) work perfectly, while others (e.g., JP, IN) may produce unexpected results.libphonenumber-for-php's test data.Input Sanitization
null, int) may cause errors.if (!is_string($rawPhone)) {
throw new \InvalidArgumentException('Phone number must be a string');
}
Twig Extension Scope
phone_format.custom_phone_format).PhoneNumberFormat::E164 to debug the parsed number before formatting:
$e164 = $formatter->format($rawPhone, PhoneNumberFormat::E164, 'FR');
libphonenumber-for-php may throw exceptions for invalid numbers. Catch them:
try {
$formatted = $formatter->format($rawPhone, PhoneNumberFormat::NATIONAL, 'IT');
} catch (\Exception $e) {
// Handle invalid number (e.g., log or return default)
}
Custom Formats Extend the bundle by creating a decorator service:
# config/services.yaml
services:
app.phone_formatter.decorator:
decorates: alpixel.helper.phone_formatter
arguments: ['@app.phone_formatter.decorator.inner']
Then override the format() method in your decorator.
Add New Country Codes
The bundle relies on libphonenumber-for-php. Extend its metadata if needed:
use libphonenumber\PhoneNumberUtil;
$util = PhoneNumberUtil::getInstance();
$util->setRegionCodeForNumber($phoneNumber, 'XX'); // Custom region
Performance
services:
alpixel.helper.phone_formatter:
public: false
tags: ['container.cacheable']
How can I help you explore Laravel packages today?