componenta/phone
Composer 安装命令:
composer require componenta/phone
包简介
Phone number value object backed by libphonenumber
README 文档
README
Phone number value object backed by giggsey/libphonenumber-for-php.
Use it when phone numbers should be validated, normalized to E.164, formatted for UI, and serialized as a stable string.
Installation
composer require componenta/phone
Related Packages
This package is standalone but relies on giggsey/libphonenumber-for-php.
| Package | Why it may be used nearby |
|---|---|
componenta/validation |
Validates raw phone input before creating Phone. |
componenta/auth |
Can use phone numbers for OTP or passwordless flows. |
componenta/cqrs |
Registration/profile commands can type phone fields with this value object. |
Usage
use Componenta\Stdlib\Phone; $phone = Phone::create('916 123-45-67', region: 'RU'); (string) $phone; // "+79161234567" $phone->toInternational(); $phone->toNational(); $phone->toRfc3966(); $phone->countryCode(); // 7 $phone->detectedRegion(); // "RU"
Constructors
Phone::create($phone, $region = 'RU')parses and validates raw inputPhone::fromE164($e164)creates from a pre-normalized E.164 string and still validates itPhone::tryCreate($phone, $region = 'RU')returnsnullinstead of throwing
The region is only a parsing hint for local formats. Numbers starting with + are parsed region-agnostically. The stored value is always E.164.
Validation And Errors
Invalid input throws InvalidArgumentException. Static isValid() can be used for boolean checks when no value object is needed.
Serialization
__toString() and jsonSerialize() return the E.164 value. masked() preserves the country code and the last four digits for logs and UI.
Performance
The parsed PhoneNumber instance is created lazily and cached on the value object after the first formatting/classification call.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-14