2017 © Pedro Peláez
 

symfony-bundle phone-number-bundle

Integrates libphonenumber into your Symfony2 application

image

20steps/phone-number-bundle

Integrates libphonenumber into your Symfony2 application

  • Friday, April 7, 2017
  • by helmuthva
  • Repository
  • 4 Watchers
  • 0 Stars
  • 53 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 87 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

PhoneNumberBundle

Build Status Total Downloads Downloads per month Latest stable version License, (*1)

This bundle integrates Google's libphonenumber into your Symfony2/Symfony3 application through the giggsey/libphonenumber-for-php port., (*2)

Installation

  1. Use Composer to download the PhoneNumberBundle:, (*3)

    $ composer require misd/phone-number-bundle
  2. Register the bundle in your application:, (*4)

    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Misd\PhoneNumberBundle\MisdPhoneNumberBundle()
        );
    }

Usage

Services

The following services are available:, (*5)

Service ID libphonenumber version
libphonenumber\PhoneNumberUtil libphonenumber.phone_number_util
libphonenumber\geocoding\PhoneNumberOfflineGeocoder libphonenumber.phone_number_offline_geocoder >=5.8.8
libphonenumber\ShortNumberInfo libphonenumber.short_number_info >=5.8
libphonenumber\PhoneNumberToCarrierMapper libphonenumber.phone_number_to_carrier_mapper >=5.8.8
libphonenumber\PhoneNumberToTimeZonesMapper libphonenumber.phone_number_to_time_zones_mapper >=5.8.8

So to parse a string into a libphonenumber\PhoneNumber object:, (*6)

$phoneNumber = $container->get('libphonenumber.phone_number_util')->parse($string, PhoneNumberUtil::UNKNOWN_REGION);

Doctrine mapping

Requires doctrine/doctrine-bundle., (*7)

To persist libphonenumber\PhoneNumber objects, add the Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType mapping to your application's config:, (*8)

// app/config.yml

doctrine:
    dbal:
        types:
            phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType

You can then use the phone_number mapping:, (*9)

/**
 * @ORM\Column(type="phone_number")
 */
private $phoneNumber;

This creates a varchar(35) column with a Doctrine mapping comment., (*10)

Note that if you're putting the phone_number type on an already-existing schema the current values must be converted to the libphonenumber\PhoneNumberFormat::E164 format., (*11)

Formatting libphonenumber\PhoneNumber objects

Twig

The phone_number_format filter can be used to format a phone number object. A libphonenumber\PhoneNumberFormat constant can be passed as argument to specify in which format the number should be printed., (*12)

For example, to format an object called myPhoneNumber in the libphonenumber\PhoneNumberFormat::NATIONAL format:, (*13)

{{ myPhoneNumber|phone_number_format('NATIONAL') }}

By default phone numbers are formatted in the libphonenumber\PhoneNumberFormat::INTERNATIONAL format., (*14)

PHP template

The format() method in the phone_number_format helper takes two arguments: a libphonenumber\PhoneNumber object and an optional libphonenumber\PhoneNumberFormat constant name or value., (*15)

For example, to format $myPhoneNumber in the libphonenumber\PhoneNumberFormat::NATIONAL format, either use:, (*16)

<?php echo $view['phone_number_format']->format($myPhoneNumber, 'NATIONAL') ?>

or:, (*17)

<?php echo $view['phone_number_format']->format($myPhoneNumber, \libphonenumber\PhoneNumberFormat::NATIONAL) ?>

By default phone numbers are formatted in the libphonenumber\PhoneNumberFormat::INTERNATIONAL format., (*18)

Serializing libphonenumber\PhoneNumber objects

Requires jms/serializer-bundle., (*19)

Instances of libphonenumber\PhoneNumber are automatically serialized in the E.164 format., (*20)

Phone numbers can be deserialized from an international format by setting the type to libphonenumber\PhoneNumber. For example:, (*21)

use JMS\Serializer\Annotation\Type;

/**
 * @Type("libphonenumber\PhoneNumber")
 */
private $phoneNumber;

Using libphonenumber\PhoneNumber objects in forms

You can use the PhoneNumberType (tel for Symfony <= 2.7) form type to create phone number fields. There are two widgets available., (*22)

Single text field

A single text field allows the user to type in the complete phone number. When an international prefix is not entered, the number is assumed to be part of the set default_region. For example:, (*23)

use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('phone_number', PhoneNumberType::class, array('default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL));
}

By default the default_region and format options are PhoneNumberUtil::UNKNOWN_REGION and PhoneNumberFormat::INTERNATIONAL respectively., (*24)

Country choice fields

The phone number can be split into a country choice and phone number text fields. This allows the user to choose the relevant country (from a customisable list) and type in the phone number without international dialling., (*25)

use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('phone_number', PhoneNumberType::class, array('widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, 'country_choices' => array('GB', 'JE', 'FR', 'US'), 'preferred_country_choices' => array('GB', 'JE')));
}

This produces the preferred choices of 'Jersey' and 'United Kingdom', and regular choices of 'France' and 'United States'., (*26)

By default the country_choices is empty, which means all countries are included, as is preferred_country_choices., (*27)

Validating phone numbers

You can use the Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber constraint to make sure that either a libphonenumber\PhoneNumber object or a plain string is a valid phone number. For example:, (*28)

use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

/**
 * @AssertPhoneNumber
 */
private $phoneNumber;

You can set the default region through the defaultRegion property:, (*29)

use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

/**
 * @AssertPhoneNumber(defaultRegion="GB")
 */
private $phoneNumber;

By default any valid phone number will be accepted. You can restrict the type through the type property, recognised values:, (*30)

  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::ANY (default)
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::FIXED_LINE
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::MOBILE
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::PAGER
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::PERSONAL_NUMBER
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::PREMIUM_RATE
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::SHARED_COST
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::TOLL_FREE
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::UAN
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::VOIP
  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::VOICEMAIL

(Note that libphonenumber cannot always distinguish between mobile and fixed-line numbers (eg in the USA), in which case it will be accepted.), (*31)

/**
 * @AssertPhoneNumber(type="mobile")
 */
private $mobilePhoneNumber;

Translations

The bundle contains translations for the form field and validation constraints., (*32)

In cases where a language uses multiple terms for mobile phones, the generic language locale will use the term 'mobile', while country-specific locales will use the relevant term. So in English, for example, en uses 'mobile', en_US uses 'cell' and en_SG uses 'handphone'., (*33)

If your language doesn't yet have translations, feel free to open a pull request to add them in!, (*34)

The Versions