Giter Club home page Giter Club logo

phone-number-bundle's Introduction

PhoneNumberBundle

Build Status

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

Authors

Installation

  1. Add 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 in the libphonenumber.phone_number_util service:

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

Doctrine mapping

Requires doctrine/doctrine-bundle.

To use the Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType mapping, add it 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(20) 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.

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

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

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

Serializing libphonenumber\PhoneNumber objects

Requires jms/serializer-bundle.

Instance 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 ZZ 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;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.