lacus/cnpj-dv
Composer 安装命令:
composer require lacus/cnpj-dv
包简介
Utility to calculate check digits on CNPJ (Brazilian Business Tax ID)
关键字:
README 文档
README
🚀 Full support for the new alphanumeric CNPJ format.
A PHP utility to calculate check digits on CNPJ (Brazilian Business Tax ID).
PHP Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Alphanumeric CNPJ: Full support for the new alphanumeric CNPJ format (introduced in 2026)
- ✅ Flexible input: Accepts
stringorarrayof strings - ✅ Format agnostic: Strips non-alphanumeric characters from string input and uppercases letters
- ✅ Auto-expansion: Multi-character strings in arrays are joined and parsed like a single string
- ✅ Input validation: Rejects ineligible CNPJs (all-zero base ID
00000000, all-zero branch0000, or 12 numeric-only repeated digits) - ✅ Lazy evaluation: Check digits are calculated only when accessed (via properties)
- ✅ Caching: Calculated values are cached for subsequent access
- ✅ Property-style API:
first,second,both,cnpj(via magic__get) - ✅ Minimal dependencies: Only
lacus/utils - ✅ Error handling: Specific types for type, length, and invalid CNPJ scenarios (
TypeErrorvsExceptionsemantics)
Installation
# using Composer
$ composer require lacus/cnpj-dv
Quick Start
<?php use Lacus\BrUtils\Cnpj\CnpjCheckDigits; $checkDigits = new CnpjCheckDigits('914157320007'); $checkDigits->first; // '9' $checkDigits->second; // '3' $checkDigits->both; // '93' $checkDigits->cnpj; // '91415732000793'
With alphanumeric CNPJ (new format):
<?php use Lacus\BrUtils\Cnpj\CnpjCheckDigits; $checkDigits = new CnpjCheckDigits('MGKGMJ9X0001'); $checkDigits->first; // '6' $checkDigits->second; // '8' $checkDigits->both; // '68' $checkDigits->cnpj; // 'MGKGMJ9X000168'
Usage
The main resource of this package is the class CnpjCheckDigits. Through an instance, you access CNPJ check-digit information:
__construct:new CnpjCheckDigits(string|array $cnpjInput)— 12–14 alphanumeric characters after sanitization (formatting stripped from strings; letters uppercased). Only the first 12 characters are used as the base; if you pass 13 or 14 characters (e.g. a full CNPJ including prior check digits), characters 13–14 are ignored and the digits are recalculated.first: First check digit (13th character of the full CNPJ). Lazy, cached.second: Second check digit (14th character of the full CNPJ). Lazy, cached.both: Both check digits concatenated as a string.cnpj: The complete CNPJ as a string of 14 characters (12 base characters + 2 check digits).
Input formats
The CnpjCheckDigits class accepts multiple input formats:
String input: raw digits and/or letters, or formatted CNPJ (e.g. 91.415.732/0007-93, MG.KGM.J9X/0001-68). Non-alphanumeric characters are removed; lowercase letters are uppercased.
Array of strings: each element must be a string; values are concatenated and then parsed like a single string (e.g. ['9','1','4',…], ['9141','5732','0007'], ['MG','KGM','J9X','0001']). Non-string elements are not allowed.
Errors & exceptions handling
This package uses TypeError vs Exception semantics: type errors indicate incorrect API use (e.g. wrong type); exceptions indicate invalid or ineligible data (e.g. invalid length or business rules). You can catch specific classes or use the abstract bases.
- CnpjCheckDigitsTypeError (abstract) — base for type errors; extends PHP’s
TypeError - CnpjCheckDigitsInputTypeError — input is not
stringorarrayof strings (or array contains a non-string element) - CnpjCheckDigitsException (abstract) — base for data/flow exceptions; extends
Exception - CnpjCheckDigitsInputLengthException — sanitized length is not 12–14
- CnpjCheckDigitsInputInvalidException — base ID
00000000, branch ID0000, or 12 identical numeric digits (repeated-digit pattern)
<?php use Lacus\BrUtils\Cnpj\CnpjCheckDigits; use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsException; use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputInvalidException; use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputLengthException; use Lacus\BrUtils\Cnpj\Exceptions\CnpjCheckDigitsInputTypeError; // Input type (e.g. integer not allowed) try { new CnpjCheckDigits(12345678000100); } catch (CnpjCheckDigitsInputTypeError $e) { echo $e->getMessage(); } // Length (must be 12–14 alphanumeric characters after sanitization) try { new CnpjCheckDigits('12345678901'); } catch (CnpjCheckDigitsInputLengthException $e) { echo $e->getMessage(); } // Invalid (e.g. all-zero base or branch, or repeated numeric digits) try { new CnpjCheckDigits('000000000001'); } catch (CnpjCheckDigitsInputInvalidException $e) { echo $e->getMessage(); } // Any data exception from the package try { // risky code } catch (CnpjCheckDigitsException $e) { // handle }
Other available resources
CNPJ_MIN_LENGTH:12— class constantCnpjCheckDigits::CNPJ_MIN_LENGTH, and globalLacus\BrUtils\Cnpj\CNPJ_MIN_LENGTHwhen the autoloadedcnpj-dv.phpfile is loaded.CNPJ_MAX_LENGTH:14— class constantCnpjCheckDigits::CNPJ_MAX_LENGTH, and globalLacus\BrUtils\Cnpj\CNPJ_MAX_LENGTHwhencnpj-dv.phpis loaded.
Calculation algorithm
The package computes check digits with the official Brazilian modulo-11 rules extended to alphanumeric characters:
- Character value: each character contributes
ord(character) − 48(so0–9stay 0–9; letters use their ASCII offset from0). - Weights: from right to left, multiply by weights that cycle 2, 3, 4, 5, 6, 7, 8, 9, then repeat from 2.
- First check digit (13th position): apply steps 1–2 to the first 12 base characters; let
r = sum % 11. The digit is0ifr < 2, otherwise11 − r. - Second check digit (14th position): apply steps 1–2 to the first 12 characters plus the first check digit; same formula for
r.
Contribution & Support
We welcome contributions! Please see our Contributing Guidelines for details. If you find this project helpful, please consider:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
License
This project is licensed under the MIT License — see the LICENSE file for details.
Changelog
See CHANGELOG for a list of changes and version history.
Made with ❤️ by Lacus Solutions
lacus/cnpj-dv 适用场景与选型建议
lacus/cnpj-dv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 776 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「check」 「verify」 「verification」 「cnpj」 「br」 「pt-BR」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lacus/cnpj-dv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lacus/cnpj-dv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lacus/cnpj-dv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a Facade to help validate and verify email addresses
A Symfony bundle for chameleon-system/sanitycheck
This Extension integrates a credit check and more made by the LEONEX Risk Management Platform into your order process. Extensive configuration and evaluation options is provided in the Platform
Laravel Package for easier user email verification.
SoftWax Health Check Bundle for Symfony framework
Check for holidays - localeaware
统计信息
- 总下载量: 776
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-24