mpetrini/phone-number-bundle

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

安装次数: 3,005

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 143

类型:symfony-bundle

v2.0.1 2020-11-03 12:26 UTC

This package is auto-updated.

Last update: 2024-09-16 17:40:21 UTC


README

Build Status Total Downloads Downloads per month Latest stable version License

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

安装

  1. 使用 Composer 下载 PhoneNumberBundle
        $ composer require misd/phone-number-bundle
  1. 在您的应用程序中注册扩展
        // 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 格式。

模板

Twig

phone_number_format

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

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

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

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

phone_number_of_type

可以使用 phone_number_of_type 测试来检查电话号码是否与类型匹配:必须传递 libphonenumber\PhoneNumberType 常量名称来指定数字必须匹配的类型。

例如,要检查名为 myPhoneNumber 的对象是否是 libphonenumber\PhoneNumberType::MOBILE 类型

    {% if myPhoneNumber is phone_number_of_type('MOBILE') }} %} ... {% endif %}

PHP 模板

format()

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

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

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

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

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

isType()

phone_number_helper 中的 isType() 方法接受两个参数:一个 libphonenumber\PhoneNumber 对象和一个可选的 libphonenumber\PhoneNumberType 常量名称或值。

例如,要检查 $myPhoneNumber 是否是 libphonenumber\PhoneNumberType::MOBILE 类型

    <?php if $view['phone_number_helper']->isType($myPhoneNumber, 'MOBILE'): ?>
    ...
    <?php endif; ?>

    <?php if $view['phone_number_helper']->isType($myPhoneNumber, \libphonenumber\PhoneNumberType::MOBILE): ?>
    ...
    <?php endif; ?>

序列化 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 为 phone_number)表单类型来创建电话号码字段。有两种小部件可供使用。

单个文本字段

单个文本字段允许用户输入完整的电话号码。如果没有输入国际前缀,则假定该数字是 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 也是如此。可以通过指定 country_placeholder 选项在列表上方创建一个占位符选项。

验证电话号码

您可以使用 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”。

如果您的语言还没有翻译,请随时提交一个拉取请求来添加它们!