Developed with love by KnpLabs Hire us for your project!
94

phone-number-bundle

by misd-service-development

Integrates libphonenumber into your Symfony2 application

PhoneNumberBundle

Build Status

This bundle integrates Google's libphonenumber into your Symfony2 application through the giggsey/libphonenumber-for-php port.

Authors

Installation

  1. Add the PhoneNumberBundle to your dependencies:

    // composer.json
    
    {
       // ...
       "require": {
           // ...
           "misd/phone-number-bundle": "~1.0"
       }
    }
    
  2. Use Composer to download and install the PhoneNumberBundle:

    $ php composer.phar update misd/phone-number-bundle
    
  3. Register the bundle in your application:

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

Usage

PhoneNumberUtil service

The libphonenumber\PhoneNumberUtil class is available as the libphonenumber.phone_number_util service:

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

Doctrine mapping

Requires doctrine/doctrine-bundle.

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

// app/config.yml

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

You can then use the phone_number mapping:

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

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

Formatting libphonenumber\PhoneNumber objects

Twig

The phone_number_format function takes two arguments: a libphonenumber\PhoneNumber object and an optional libphonenumber\PhoneNumberFormat constant name.

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

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

By default phone numbers are formatted in the libphonenumber\PhoneNumberFormat::INTERNATIONAL format.

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.

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

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

or:

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

By default phone numbers are formatted in the libphonenumber\PhoneNumberFormat::INTERNATIONAL format.

Serializing libphonenumber\PhoneNumber objects

Requires jms/serializer-bundle.

Instances of libphonenumber\PhoneNumber are automatically serialized in the E.164 format.

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

use JMS\Serializer\Annotation\Type;

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

Using libphonenumber\PhoneNumber objects in forms

You can use the tel form type to create phone number fields. For example:

use libphonenumber\PhoneNumberFormat;
use Symfony\Component\Form\FormBuilderInterface;

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

By default the default_region and format options are PhoneNumberUtil::UNKNOWN_REGION and PhoneNumberFormat::INTERNATIONAL respectively.

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:

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

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

You can set the default region through the defaultRegion property:

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 are mobile and fixed_line. (Note the libphonenumber cannot always distinguish between mobile and fixed-line numbers (eg in the USA), in which case it will be accepted.)

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

Translations

The bundle contains translations for the form field and validation constraints.

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'.

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

Copyright (c) 2013 University of Cambridge

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Bump year
    By thewilkybarkid, 2 years ago
  • Merge pull request #34 from waaghals/patch-1
    By thewilkybarkid, 2 years ago
  • Add dutch translations
    By , 2 years ago
  • Merge pull request #22 from fdelapena/master
    By thewilkybarkid, 2 years ago
  • Add Spanish translation (#20)
    By , 2 years ago
  • Merge pull request #31 from winzou/patch-1
    By thewilkybarkid, 2 years ago
  • Merge pull request #32 from thewilkybarkid/extension-test
    By thewilkybarkid, 2 years ago
  • Improve extension test
    By thewilkybarkid, 2 years ago
  • Add French translations
    By winzou, 2 years ago
  • Merge pull request #21 from thewilkybarkid/doctrine-exception
    By thewilkybarkid, 2 years ago
  • Add translations note
    By thewilkybarkid, 3 years ago
  • Merge pull request #26 from giovkanata/master
    By thewilkybarkid, 3 years ago
  • Create validators.it.xlf
    By , 3 years ago
  • Merge branch '1.0'
    By thewilkybarkid, 3 years ago
  • Ready for 1.0.4
    By thewilkybarkid, 3 years ago
  • Add libphonenumber-for-php v7 support
    By thewilkybarkid, 3 years ago
  • Merge pull request #23 from xabbuh/issue-20
    By thewilkybarkid, 3 years ago
  • add german translations
    By xabbuh, 3 years ago
  • Throw exceptions on failure
    By thewilkybarkid, 3 years ago
  • Update PHPUnit
    By thewilkybarkid, 3 years ago
  • Merge pull request #17 from pdawczak/translation-pl
    By thewilkybarkid, 3 years ago
  • Validation error messages translation (pl)
    By , 3 years ago
  • Merge pull request #15 from thewilkybarkid/constraint-types
    By thewilkybarkid, 3 years ago
  • Bump version
    By thewilkybarkid, 3 years ago
  • Ready for 1.0.3
    By thewilkybarkid, 3 years ago
  • Handle international numbers when using national format. Fixes #13
    By thewilkybarkid, 3 years ago
  • Validate phone number type
    By thewilkybarkid, 3 years ago
  • Adhere to the interface and throw a TransformationFailedException when not a PhoneNumber object
    By thewilkybarkid, 3 years ago
  • Add extra keyword
    By thewilkybarkid, 3 years ago
  • Tweak Doctrine doc, fixes #8
    By thewilkybarkid, 3 years ago