20steps/phone-number-bundle

将libphonenumber集成到您的Symfony2应用程序中

安装: 67

依赖项: 0

建议者: 0

安全: 0

星星: 0

关注者: 5

分支: 144

类型:symfony-bundle

v1.2.0 2016-12-17 17:15 UTC

README

Build Status Total Downloads Downloads per month Latest stable version License

此包通过 giggsey/libphonenumber-for-php 端口将 Google 的 libphonenumber 集成到您的 Symfony2/Symfony3 应用程序中。

安装

  1. 使用 Composer 下载 PhoneNumberBundle

    $ composer require misd/phone-number-bundle
    
  2. 在您的应用程序中注册该包

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

用法

服务

以下服务可用

要将字符串解析为 libphonenumber\PhoneNumber 对象

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

Doctrine 映射

需要 doctrine/doctrine-bundle

为了持久化 libphonenumber\PhoneNumber 对象,将 Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType 映射添加到应用程序的配置中

// app/config.yml

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

然后您可以使用 phone_number 映射

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

这创建了一个具有 Doctrine 映射注释的 varchar(35) 列。

请注意,如果您将 phone_number 类型放在已存在的模式上,当前值必须转换为 libphonenumber\PhoneNumberFormat::E164 格式。

格式化 libphonenumber\PhoneNumber 对象

Twig

可以使用 phone_number_format 过滤器格式化电话号码对象。可以通过传递一个 libphonenumber\PhoneNumberFormat 常量作为参数来指定打印数字的格式。

例如,要将名为 myPhoneNumber 的对象格式化为 libphonenumber\PhoneNumberFormat::NATIONAL 格式

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

默认情况下,电话号码以 libphonenumber\PhoneNumberFormat::INTERNATIONAL 格式格式化。

PHP 模板

phone_number_format 助手中的 format() 方法接受两个参数:一个 libphonenumber\PhoneNumber 对象和一个可选的 libphonenumber\PhoneNumberFormat 常量名称或值。

例如,要将 $myPhoneNumber 格式化为 libphonenumber\PhoneNumberFormat::NATIONAL 格式,可以使用以下方法

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

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

默认情况下,电话号码以 libphonenumber\PhoneNumberFormat::INTERNATIONAL 格式格式化。

序列化 libphonenumber\PhoneNumber 对象

需要 jms/serializer-bundle

libphonenumber\PhoneNumber 实例自动以 E.164 格式序列化。

可以通过将类型设置为 libphonenumber\PhoneNumber 从国际格式反序列化电话号码。例如

use JMS\Serializer\Annotation\Type;

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

在表单中使用 libphonenumber\PhoneNumber 对象

您可以使用 PhoneNumberType (Symfony <= 2.7 的 tel)表单类型创建电话号码字段。有两种小部件可用。

单个文本字段

单个文本字段允许用户输入完整的电话号码。如果没有输入国际前缀,则假定号码是该组 default_region 的一部分。例如

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));
}

默认情况下,default_regionformat 选项分别为 PhoneNumberUtil::UNKNOWN_REGIONPhoneNumberFormat::INTERNATIONAL

国家选择字段

电话号码可以被拆分为国家选择和电话号码文本字段。这允许用户从可自定义的列表中选择相关国家,并输入电话号码而无需国际拨号。

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')));
}

这生成了首选的“泽西”和“联合王国”,以及常规的“法国”和“美国”选择。

默认情况下,country_choices 为空,这意味着包括所有国家,同样 preferred_country_choices 也是空的。

验证电话号码

您可以使用 Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber 约束来确保一个 libphonenumber\PhoneNumber 对象或一个纯文本字符串是一个有效的电话号码。例如

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

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

您可以通过 defaultRegion 属性来设置默认区域。

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

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

默认情况下,任何有效的电话号码都将被接受。您可以通过 type 属性来限制类型,支持的值有

  • Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::ANY(默认)
  • 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

(请注意,libphonenumber 并不能总是区分移动和固定电话号码(例如在美国),在这种情况下将被接受。)

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

翻译

该组件包含表单字段和验证约束的翻译。

在某种语言使用多个术语来表示手机的情况下,通用语言区域将使用“mobile”这个术语,而特定国家区域将使用相应的术语。例如,在英语中,en 使用“mobile”,en_US 使用“cell”,而 en_SG 使用“handphone”。

如果您的语言还没有翻译,欢迎您提交一个pull request来添加它们!