定制 carono/yii2-phone-helper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

carono/yii2-phone-helper

最新稳定版本:1.0.1

Composer 安装命令:

composer require carono/yii2-phone-helper

包简介

A helper for clearing a phone number and forming a string by mask

README 文档

README

License Yii2

A Yii2 extension that provides helper functions for phone number processing, including normalization, validation, and formatting.

Features

  • Phone number normalization (removing all non-digit characters)
  • Phone number validation against country codes
  • Formatting phone numbers according to masks
  • Built-in validator for Yii2 models
  • Comprehensive country code database included

Installation

The preferred way to install this extension is through composer.

Either run

composer require carono/yii2-phone-helper

or add

"carono/yii2-phone-helper": "*"

to the require section of your composer.json file.

Usage

Basic Phone Number Processing

use carono\yii2\helpers\PhoneHelper;

// Clear phone number from non-digit characters
$cleanNumber = PhoneHelper::clear('+7 (123) 456-78-90'); // returns '71234567890'

// Normalize phone number (returns null if number doesn't match any known country code)
$normalized = PhoneHelper::normalNumber('+7 (123) 456-78-90'); // returns '71234567890'

// Format phone number according to its country code mask
$formatted = PhoneHelper::asString('71234567890'); // returns '+7 (123) 456-78-90' for Russian numbers

Using in Yii2 Models

use carono\yii2\helpers\PhoneFilter;

public function rules()
{
    return [
        [['phone'], PhoneFilter::class],
        // or with custom validation
        [['phone'], 'filter', 'filter' => [PhoneHelper::class, 'normalNumber']],
    ];
}

Getting Country Code Information

$codeInfo = PhoneHelper::getCode('71234567890');
/*
Returns array like:
{
	"mask": "+7(###)###-##-##",
	"cc": "RU",
	"name_en": "Russia",
	"desc_en": "",
	"name_ru": "Россия",
	"desc_ru": ""
}
*/

Methods Reference

PhoneHelper class

  • clear(string $number): string - Removes all non-digit characters from phone number
  • normalNumber(string $number): ?string - Returns normalized number if valid, null otherwise
  • asString(string $number, ?string $mask = null): string - Formats number according to mask
  • getCode(string $number): ?array - Returns country code information for the number

PhoneFilter class

A Yii2 validator filter that automatically normalizes phone numbers in model attributes.

License

This extension is released under the MIT License. See the bundled LICENSE file for details.

Support

If you have any questions or issues, please create an issue on GitHub or contact the author at info@carono.ru.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-31