phillarmonic/cpf-cnpj
Composer 安装命令:
composer require phillarmonic/cpf-cnpj
包简介
A PHP library for validating and formatting CPF and CNPJ numbers.
关键字:
README 文档
README
A PHP library for validating and formatting CPF and CNPJ numbers from Brazil.
Installation
You can install this library using Composer. Run the following command in your project directory:
composer require phillarmonic/cpf-cnpj
Usage
Validating CPF Numbers
To validate a CPF number:
use Phillarmonic\CpfCnpj\CPF; $cpf = new CPF('123.456.789-09'); if ($cpf->isValid()) { echo "The CPF is valid."; } else { echo "The CPF is not valid."; }
Formatting CPF Numbers
To format a valid CPF number:
use Phillarmonic\CpfCnpj\CPF; $cpf = new CPF('12345678909'); $formattedCpf = $cpf->format(); if ($formattedCpf !== false) { echo "Formatted CPF: " . $formattedCpf; // Outputs: 123.456.789-09 } else { echo "Invalid CPF, unable to format."; }
Validating CNPJ Numbers
To validate a CNPJ number:
use Phillarmonic\CpfCnpj\CNPJ; $cnpj = new CNPJ('12.345.678/0001-95'); if ($cnpj->isValid()) { echo "The CNPJ is valid."; } else { echo "The CNPJ is not valid."; }
Formatting CNPJ Numbers
To format a valid CNPJ number:
use Phillarmonic\CpfCnpj\CNPJ; $cnpj = new CNPJ('12345678000195'); $formattedCnpj = $cnpj->format(); if ($formattedCnpj !== false) { echo "Formatted CNPJ: " . $formattedCnpj; // Outputs: 12.345.678/0001-95 } else { echo "Invalid CNPJ, unable to format."; }
Important Notes
- The library automatically removes any non-digit characters from the input, so you can pass formatted or unformatted numbers.
- The
isValid()method checks for:- Correct length (11 digits for CPF, 14 digits for CNPJ)
- Not in the blacklist of known invalid numbers
- Validity of check digits
- The
format()method returnsfalseif the number is invalid.
Error Handling
The library doesn't throw exceptions. Instead, it returns false or fails silently in case of invalid input. Always check the return value of isValid() before using the number, and check if format() returns false before using the formatted result.
Contributing
If you'd like to contribute to this project, please submit a pull request on the GitHub repository.
License
This library is open-sourced software licensed under the MIT license.
phillarmonic/cpf-cnpj 适用场景与选型建议
phillarmonic/cpf-cnpj 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「formatting」 「cpf」 「cnpj」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phillarmonic/cpf-cnpj 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phillarmonic/cpf-cnpj 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phillarmonic/cpf-cnpj 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that can render international postal addresses in different formats.
Validador de cpf com suporte para laravel 5.*
A data transforming/formatting package for php.
PHP extension with Brazilian states and formats for CPF, CNPJ and ZIP
Com esse pacote você poderá consultar, gratuitamente, CPFs diretamente no site da receita.
Run all code formatting tools with one command: pretty
统计信息
- 总下载量: 17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-27