baraja-core/phone-number
Composer 安装命令:
composer require baraja-core/phone-number
包简介
Simple package for phone number manipulation.
README 文档
README
There is no easy way to validate and format phone numbers in PHP, so I wrote a simple library for this that has no dependencies, but still handles this role.
The goal is to check the format of a phone number, or convert it to a basic canonical form (which is always valid).
Installing
Simply composer:
$ composer require baraja-core/phone-number
How to use the library
The principle of this tool is based on formatting and validating phone numbers from the user, or from sources over which you have no control.
The most common use is to correct phone number formatting:
$original = '+420 777123456'; $formatted = \Baraja\PhoneNumber\PhoneNumberFormatter::fix($original); echo $original . '<br>'; echo $formatted;
The function fixes the number formatting and returns the string +420 777 123 456.
If no prefix is specified by the user, the +420 prefix is assumed. You can change the default preference with the second parameter:
// returns: +421 777 123 456 \Baraja\PhoneNumber\PhoneNumberFormatter::fix('+420 777123456', 421);
The phone code is overwritten only if the user does not enter it and it fails to be detected automatically.
Input and output formatting
The input string can look (almost) any way. The built-in algorithm can automatically remove non-valid characters (for example, some users write a note next to a phone number that will be removed automatically). So you don't have to worry about formatting the input at all, but the output will always be consistent.
The output always looks the same (it is normalized to the canonical format).
The general format is:
+420 777 123 456
| \_________/
Prefix |
National number
If you submit a non-valid input (or an input that cannot be automatically corrected), an exception will be thrown.
Error trapping
If the number cannot be safely normalized to a base form, or does not exist, throw an \InvalidArgumentException exception.
If you wish to convert the exception to a boolean, use the built-in asset validator:
\Baraja\PhoneNumber\PhoneNumberValidator::isValid('123'); // false \Baraja\PhoneNumber\PhoneNumberValidator::isValid('777123456'); // true \Baraja\PhoneNumber\PhoneNumberValidator::isValid('+420 777123456'); // true \Baraja\PhoneNumber\PhoneNumberValidator::isValid('+420 777 123 456'); // true \Baraja\PhoneNumber\PhoneNumberValidator::isValid('+420 77 712 34 56'); // true
baraja-core/phone-number 适用场景与选型建议
baraja-core/phone-number 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 104.06k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 baraja-core/phone-number 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 baraja-core/phone-number 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 104.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 5
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2021-04-21