承接 gmarsano/chilean-phone-tool 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gmarsano/chilean-phone-tool

Composer 安装命令:

composer require gmarsano/chilean-phone-tool

包简介

validate and format chilean phone numbers.

README 文档

README

Introduction

A PHP composer package to work with Chilean 🇨🇱 phone numbers.

Features

  • Parsing
  • Get prefix
  • Validation
  • Formatting
  • Factory

Contents

Install

composer require gmarsano/chilean-phone-tool

Requirements

  • PHP >= 7.3

Usage

use Gmarsano\ChileanPhoneTool\Phone;

$phone = Phone::parse("+56 9 87-654-321");
$phone->validate();   // true
$phone->number();     // "987654321"
$phone->fullNumber(); // "56987654321"
$phone->prefix()      // "9"
$phone->format();     // "+56 9 87-654-321"

Set a phone number.

parse

Usually you may want to take a phone in any format, check if it's valid and bring it to the desired format. Use parse to set the number, if that the case.

If you parse a valid number you can get the number and prefix just calling these:

Phone::parse("+56 9 87-654-321")->number();
// => "987654321"

Phone::parse("+56-32-7-654-321")->prefix();
// => "32"

Clean an invalid input (use quiet to prevent validation exceptions):

Phone::parse("2 (09) 987654321")->quiet()->get();
// => "209987654321"

// yes work with integer or float, too.
Phone::parse(1.23)->quiet()->number();
// => "123"

setPhone

Sometimes you just want to know if an input value is a valid phone number without clean or make any changes on it before validation. It's time to use setPhone:

Phone::setPhone("56987654321")->quiet()->validate();
// => true

Phone::setPhone("987654321")->quiet()->validate();
// => true

Phone::setPhone("+56 9 87-654-321")->quiet()->validate();
// => false

Validation

validate

Use validate to check if it is a valid number. An invalid number will throw an exception with a message giving information about the reasons.

Phone::parse("+56-32-7-654-321")->validate();
// => true

Phone::parse("+56 1 87-654-321")->validate();
// Exception with message 'Invalid prefix.'

The features of this tool make sense on a valid phone. This is why by default it works with exceptions. But you can use quiet to avoid this behavior, but be careful, if you try to get or format an invalid number you will get the original digits or value depending on the case.

errors and messages

They allow to obtain an array with validation messages if quiet has been used. They may look like aliases, but there is a little difference:

  • with errors you get a list of the validation messages with error codes as keys.
  • with messages you only get the messages.
$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone->errors()
/*
=> [
     5 => "Invalid prefix.",
   ]
*/

$phone->messages()
/*
=> [
     "Invalid prefix.",
   ]
*/
Error messages
code message
2 Empty digits count.
3 Invalid phone number format.
4 Invalid country code.
5 Invalid prefix.
6 Invalid phone number.

Ignore prefix validation

To avoid prefix validation use ignorePrefix:

$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone = Phone::parse("+56 1 87-654-321");
$phone->ignorePrefix()->validate();
// => true

$phone->get()
// => "187654321"

$phone->prefix()
// => "18"

fix and luckyFix

Tries to set a valid phone from original input.

$phone = Phone::setPhone("+56 032 7-654-321");
$phone->quiet()->validate();
// => false

$phone->fix()->validate();
// => true

$phone->get();
// => "327654321"

Use getOld to get original value.

$phone->getOld();
// => "+56 032 7-654-321"

Sometimes the inputs may lack the Santiago prefix (2). Use luckFix to fix and try to guess if the missing prefix can be fixed.

$phone = Phone::parse("+56-37-654-321");
$phone->quiet()->validate();
// => false

$phone->luckyFix()->validate();
// => true

$phone->format();
// => "+56 2 37-654-321"

$phone->errors();
/*
=> [
     3 => "Invalid phone number format.",
   ]
*/

As you can see, after fix, validation can be true. If you need to check if original was modified because it was invalid, then you can count errors.

Formatting

format

Use format on a valid phone to get the value in standard numbering format.

$phone = Phone::setPhone("987654321");
$phone->quiet()->validate();
// => true

$phone->format();
// => "+56 9 87-654-321"

Choosing format

Give format method the desired format as an argument.

format example
FormatterInterface::STANDARD_FORMAT +56 9 87-654-321, +56 75 7-654-321
FormatterInterface::PREFIX_FORMAT (9) 87-654-321, (75) 7-654-321
FormatterInterface::DIGITS_FORMAT 56987654321, 56757654321
FormatterInterface::NUMBER_DIGITS_FORMAT 987654321, 757654321
use Gmarsano\ChileanPhoneTool\Contracts\FormatterInterface;

Phone::setPhone("987654321")->quiet()
  ->format(FormatterInterface::PREFIX_FORMAT);
// => "(9) 87-654-321"

Factory

You can generate a valid phone number using Phone Tool:

Phone::factory()->make()->first();
// => "451036552"

Make multiple numbers giving a count as argument to make method.

Phone::factory()->make(5)->all();
/*
=> [
     "523677441",
     "243584227",
     "712584943",
     "671108073",
     "633943870",
   ]
*/

You can use the unique, cellPhone, landLine or prefix modifiers to make unique numbers, cell phone, landline (red fija), or set a valid prefix manually.

Phone::factory()->cellPhone()->make()->first();
// => "973986533"

Phone::factory()->unique()->landLine()->make(3)->all();
/*
=> [
     "649800571",
     "572435282",
     "805066069",
   ]
*/

Phone::factory()->unique()->prefix(2)->make(3)->all();
/*
=> [
     "249404634",
     "250034633",
     "243960969",
   ]
*/

Use format (check choosing format) modifier to give desired format to numbers.

Phone::factory()->make()->format()->first();
// => "+56 68 4-987-466"

Phone::factory()->unique()->make(3)
  ->format(FormatterInterface::PREFIX_FORMAT)
  ->all();
/*
=> [
     "(9) 76-488-717",
     "(45) 1-806-739",
     "(42) 1-444-163",
   ]
*/

// try this if you quickly need digits with country code
Phone::factory()->make(1, true)->first();
// => "56555182350"

Aliases

methods aliases
get() number
get(true) fullNumber
getPrefix prefix
validate isValid

Inspired by

Freshworks Chilean Bundle

License

MIT License
Copyright (c) 2021 gmarsano

gmarsano/chilean-phone-tool 适用场景与选型建议

gmarsano/chilean-phone-tool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「formatter」 「phone」 「chile」 「chilean」 「telefono」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gmarsano/chilean-phone-tool 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gmarsano/chilean-phone-tool 我们能提供哪些服务?
定制开发 / 二次开发

基于 gmarsano/chilean-phone-tool 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-13