christianjombo/laravel-phone

此软件包为Laravel和Lumen添加了基于Google的libphonenumber API的电话号码验证功能。

dev-master 2019-11-03 10:06 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:55 UTC


README

此软件包基于giggsey的PHP端口(https://github.com/giggsey/libphonenumber-for-php)和Google的libphonenumber API(https://github.com/googlei18n/libphonenumber),为Laravel和Lumen添加了电话号码功能。

目录

安装

运行以下命令以安装最新版本的软件包

composer require christianjombo/laravel-phone

Laravel

如果您不使用自动发现,请打开您的应用配置并在$providers数组中添加服务提供者

'providers' => [
   ...
   ChristianJombo\LaravelPhone\PhoneServiceProvider::class,
],

在您的语言目录中,为每种语言添加一个额外的语言行以供验证器使用

'phone' => 'The :attribute field contains an invalid number.',

Lumen

bootstrap/app.php中,注册服务提供者

$app->register(ChristianJombo\LaravelPhone\PhoneServiceProvider::class);

验证

要验证电话号码,请在验证规则数组中使用phone关键字或使用Phone规则类以可表达的方式定义规则。电话验证器可以以三种方式操作。

  • 您可以直接指定作为验证器参数的ISO 3166-1 alpha-2兼容国家代码,例如:

    'phonefield'       => 'phone:US,BE',
    // 'phonefield'    => Rule::phone()->country(['US', 'BE'])

    验证器将检查号码是否在提供的国家中至少有效一个,因此您可以随意添加任意多的国家代码。

  • 您提供一个专门的国家输入字段(以ISO 3166-1兼容国家代码为键),以便允许最终用户自行提供国家。请确保国家字段的名称与电话字段相似,但后面加上_country以进行自动发现,或者将您自定义的国家字段名称作为参数传递给验证器

    'phonefield'            => 'phone',
    // 'phonefield'         => Rule::phone()
    'phonefield_country'    => 'required_with:phonefield',
    'phonefield'            => 'phone:custom_country_field',
    // 'phonefield'         => Rule::phone()->countryField('custom_country_field')
    'custom_country_field'  => 'required_with:phonefield',
  • 您指示验证器使用AUTO关键字(可选地附带任何后备国家)来检测号码属于哪个国家

    'phonefield'       => 'phone:AUTO,US',
    // 'phonefield'    => Rule::phone()->detect()->country('US')

    验证器将尝试从号码本身中提取国家,然后检查该国家的号码是否有效。如果无法猜测国家,如果提供了后备国家,则使用后备国家进行验证。请注意,当电话号码以国际格式(以+符号开头,例如+32 ...)输入时,国家猜测才会工作。前导双零将不会被正确解析,因为这并不是一个已建立的规则。

要指定对号码类型的约束,只需将允许的类型追加到参数的末尾,例如:

'phonefield'       => 'phone:US,BE,mobile',
// 'phonefield'    => Rule::phone()->country(['US', 'BE'])->type('mobile')
// 'phonefield'    => Rule::phone()->country('US', 'BE')->mobile()

最常见类型是mobilefixed_line,但您可以自由使用在此处定义的任何类型https://github.com/giggsey/libphonenumber-for-php/blob/master/src/PhoneNumberType.php

您还可以通过使用LENIENT参数来启用更宽松的验证(例如,不带区号的固定电话)。请注意,此功能与国家自动检测和号码类型验证结合使用时可能表现不佳,因此请自行承担风险。

'phonefield'       => 'phone:LENIENT,US',
// 'phonefield'    => Rule::phone()->lenient()->country('US')

实用PhoneNumber类

可以将电话号码包装在ChristianJombo\LaravelPhone\PhoneNumber类中,以通过有用的实用方法增强它。可以直接在视图中引用这些对象或将它们保存到数据库中,因为它们将优雅地降级到E164格式。

use ChristianJombo\LaravelPhone\PhoneNumber;

(string) PhoneNumber::make('+3212/34.56.78');              // +3212345678
(string) PhoneNumber::make('012 34 56 78', 'BE');          // +3212345678
(string) PhoneNumber::make('012345678')->ofCountry('BE');  // +3212345678

格式化

PhoneNumber可以以多种方式格式化

PhoneNumber::make('012 34 56 78', 'BE')->format($format);       // See libphonenumber\PhoneNumberFormat
PhoneNumber::make('012 34 56 78', 'BE')->formatE164();          // +3212345678
PhoneNumber::make('012 34 56 78', 'BE')->formatInternational(); // +32 12 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatRFC3966();       // +32-12-34-56-78
PhoneNumber::make('012/34.56.78', 'BE')->formatNational();      // 012 34 56 78

// Formats so the number can be called straight from the provided country.
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('BE'); // 012 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('NL'); // 00 32 12 34 56 78
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('US'); // 011 32 12 34 56 78

// Formats so the number can be clicked on and called straight from the provided country using a cellphone.
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('BE'); // 012345678
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('NL'); // +3212345678
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('US'); // +3212345678

号码信息

获取有关电话号码的一些信息

PhoneNumber::make('012 34 56 78', 'BE')->getType();              // 'fixed_line'
PhoneNumber::make('012 34 56 78', 'BE')->isOfType('fixed_line'); // true
PhoneNumber::make('012 34 56 78', 'BE')->getCountry();           // 'BE'
PhoneNumber::make('012 34 56 78', 'BE')->isOfCountry('BE');      // true
PhoneNumber::make('+32 12 34 56 78')->isOfCountry('BE');         // true

辅助函数

该包公开了phone()辅助函数,该函数返回ChristianJombo\LaravelPhone\PhoneNumber实例或提供$format时返回格式化后的字符串

phone($number, $country = [], $format = null)