Download the PHP package lacus/cnpj-dv without Composer
On this page you can find all versions of the php package lacus/cnpj-dv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lacus/cnpj-dv
More information about lacus/cnpj-dv
Files in lacus/cnpj-dv
Package cnpj-dv
Short Description Utility to calculate check digits on CNPJ (Brazilian Business Tax ID)
License MIT
Homepage https://cnpj-utils.vercel.app/
Informations about the package cnpj-dv

π 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
Quick Start
With alphanumeric CNPJ (new format):
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)
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