phillarmonic/cpf-cnpj 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phillarmonic/cpf-cnpj

最新稳定版本:1.0.0

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

  1. The library automatically removes any non-digit characters from the input, so you can pass formatted or unformatted numbers.
  2. 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
  3. The format() method returns false if 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.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-27