Download the PHP package lacus/br-utils without Composer
On this page you can find all versions of the php package lacus/br-utils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package br-utils

🚀 Full support for the new alphanumeric CNPJ format.
A PHP toolkit to handle the main operations with Brazilian-related data: CPF (Individual's Taxpayer ID) and CNPJ (Business Tax ID). It provides a top-level BrUtils wrapper around lacus/cpf-utils and lacus/cnpj-utils, exposing all bundled resources under unified namespaces.
PHP Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Unified top-level API: One
BrUtilsinstance with$cpfand$cnpjdomain accessors - ✅ Bundled domains:
lacus/cpf-utilsandlacus/cnpj-utilsinstalled together - ✅ Alphanumeric CNPJ: Full support for the new alphanumeric CNPJ format (introduced in 2026)
- ✅ Configurable defaults: Set formatter, generator, and (for CNPJ) validator options on each domain instance
- ✅ Per-call overrides: Override any component option for a single method call
- ✅ Dual API style: Top-level façade (
BrUtils), domain aggregators (CpfUtils,CnpjUtils), standalone components, and functional helpers - ✅ Shared namespaces: CPF symbols under
Lacus\BrUtils\Cpf\; CNPJ symbols underLacus\BrUtils\Cnpj\ - ✅ Typed error handling: Dedicated exception hierarchies from bundled packages (CNPJ v2
TypeError/Exceptionmodel; CPF v1InvalidArgumentExceptionfor invalid options)
Installation
This installs lacus/br-utils together with lacus/cpf-utils and lacus/cnpj-utils (which in turn pulls in the CNPJ component packages). You do not need separate composer require calls for the domain packages when using lacus/br-utils.
Import
Pick the API that fits your use case.
Top-level façade:
Domain aggregators:
CPF components (object-oriented):
CNPJ components (object-oriented):
Functional helpers:
Quick start
With BrUtils (all-in-one):
With domain aggregators:
With functional helpers:
Usage
You can work in four equivalent ways:
BrUtils— single instance with shared defaults across both CPF and CNPJ domains.- Domain aggregators —
CpfUtilsandCnpjUtilsdirectly (same classes used internally byBrUtils). - Component classes —
CpfFormatter,CnpjGenerator, and so on. - Functional helpers —
cpf_fmt(),cnpj_gen(), and related functions for one-off calls.
All approaches expose the same options and behavior within each domain. For full option tables and component-specific details, see the README of each bundled package.
BrUtils
-
__construct:new BrUtils($cpf = [], $cnpj = [])Each
$cpf/$cnpjargument may be a pre-builtCpfUtils/CnpjUtilsinstance or a configuration array spread into the corresponding utils constructor. Within that array, each resource key (formatter,generator, andvalidatorfor CNPJ) accepts either an options object or an associative array of option values.Example:
new BrUtils(cpf: ['formatter' => ['hidden' => true]], cnpj: ['validator' => ['type' => CnpjValidationType::Numeric]]). -
$cpf,$cnpj: Property-style access to the domain utils instances (CpfUtilsandCnpjUtils). getCpfUtils(),getCnpjUtils(): Return the internal domain instances for direct use.
Instance defaults and per-call overrides
Passing a CnpjFormatterOptions, CnpjGeneratorOptions, or CnpjValidatorOptions instance to the BrUtils constructor stores that object by reference — mutating it later affects subsequent calls with no per-call override.
CPF operations
CPF methods are accessed via $utils->cpf, CpfUtils, or the cpf_*() helpers. CPF uses the v1 API from lacus/cpf-utils: string-only input, positional/named formatter and generator options, and no validator settings.
Formatting (format / cpf_fmt)
| Parameter | Type | Default | Description |
|---|---|---|---|
escape |
?bool |
false |
When true, HTML-escapes the final string |
hidden |
?bool |
false |
When true, replaces the inclusive index range [hiddenStart, hiddenEnd] on the normalized 11-digit string before punctuation is applied |
hiddenKey |
?string |
'*' |
Replacement for each hidden position |
hiddenStart |
?int |
3 |
Start index 0–10 (inclusive) |
hiddenEnd |
?int |
10 |
End index 0–10 (inclusive) |
dotKey |
?string |
'.' |
Separator between digit groups |
dashKey |
?string |
'-' |
Separator before the last two digits |
onFail |
?\Closure |
see below | Closure(mixed $value, Exception $e): string — used when sanitized length ≠ 11 |
Default onFail returns the original input unchanged. Invalid length does not throw from format().
Generation (generate / cpf_gen)
| Parameter | Type | Default | Description |
|---|---|---|---|
format |
?bool |
false |
When true, returns formatted CPF (000.000.000-00); otherwise returns compact 11-digit output |
prefix |
?string |
'' |
Base seed for generation. Non-digit characters are stripped; only the first 9 digits (indexes 0–8) are used |
Validation (isValid / cpf_val)
Accepts formatted or unformatted CPF strings. Returns true or false without throwing for invalid CPF.
CNPJ operations
CNPJ methods are accessed via $utils->cnpj, CnpjUtils, or the cnpj_*() helpers. CNPJ uses the v2 API from lacus/cnpj-utils.
Formatting (format / cnpj_fmt)
Supports the same options as lacus/cnpj-fmt. Input accepts string or list<string>.
| Parameter | Type | Default | Description |
|---|---|---|---|
hidden |
?bool |
false |
When true, replaces the inclusive index range [hiddenStart, hiddenEnd] on the normalized 14-character string before punctuation is applied |
hiddenKey |
?string |
'*' |
Replacement for each hidden position (may be multi-character or empty); must not use disallowed key characters |
hiddenStart |
?int |
5 |
Start index 0–13 (inclusive) |
hiddenEnd |
?int |
13 |
End index 0–13 (inclusive); if hiddenStart > hiddenEnd, they are swapped |
dotKey |
?string |
'.' |
Separator between groups XX / XXX / XXX |
slashKey |
?string |
'/' |
Separator before the branch block |
dashKey |
?string |
'-' |
Separator before the last two characters |
escape |
?bool |
false |
When true, HTML-escapes the final string |
encode |
?bool |
false |
When true, URL-encodes the final string |
onFail |
?\Closure |
see below | Closure(mixed $value, CnpjFormatterException $e): string — used when sanitized length ≠ 14 |
Default onFail returns an empty string. Wrong input types throw CnpjFormatterInputTypeError.
Generation (generate / cnpj_gen)
Supports the same options as lacus/cnpj-gen.
| Parameter | Type | Default | Description |
|---|---|---|---|
format |
?bool |
false |
When true, returns formatted CNPJ (XX.XXX.XXX/XXXX-XX); otherwise returns compact 14-character output |
prefix |
?string |
'' |
Base seed for generation. Non-alphanumeric chars are stripped, letters are uppercased, and only first 12 chars (indexes 0–11) are used; characters at index 12+ are ignored |
type |
CnpjGenerationType\|'alphanumeric'\|'alphabetic'\|'numeric'\|null |
CnpjGenerationType::Alphanumeric |
Character family used for generated base positions |
prefix validation rules:
- base ID
00000000is rejected (when first 8 chars are present) - branch ID
0000is rejected (when chars 9–12 are present) - 12 repeated numeric digits are rejected (e.g.
111111111111)
Validation (isValid / cnpj_val)
Supports the same options as lacus/cnpj-val. Input accepts string or list<string>.
| Parameter | Type | Default | Description |
|---|---|---|---|
type |
CnpjValidationType\|'alphanumeric'\|'numeric'\|null |
CnpjValidationType::Alphanumeric |
Character set after sanitization |
caseSensitive |
?bool |
true |
When false, lowercase letters are uppercased before alphanumeric validation |
Invalid CNPJ returns false without throwing. Wrong input types throw CnpjValidatorInputTypeError.
Domain aggregators (standalone)
Use CpfUtils or CnpjUtils directly when you only need one domain:
Accessing components
Each domain aggregator exposes its internal formatter, generator, and validator:
Use getCpfUtils() / getCnpjUtils() on BrUtils, or the component getters on each domain utils instance, when you already have a configured instance and want the underlying component without creating a new one.
Mixing styles
Use BrUtils where a shared configuration helps, and standalone components or helpers elsewhere — they are the same underlying classes:
Errors & exceptions
BrUtils does not define its own exception types; it propagates errors from the bundled packages:
- CPF formatting / generation:
InvalidArgumentExceptionfor invalid option types or values (e.g. out-of-rangehiddenStart, prefix longer than 9 digits). - CNPJ formatting:
CnpjFormatterInputTypeError,CnpjFormatterOptionsTypeError,CnpjFormatterOptionsHiddenRangeInvalidException,CnpjFormatterOptionsForbiddenKeyCharacterException, and related classes. - CNPJ generation:
CnpjGeneratorOptionsTypeError,CnpjGeneratorOptionPrefixInvalidException,CnpjGeneratorOptionTypeInvalidException, and related classes. - CNPJ validation:
CnpjValidatorInputTypeError,CnpjValidatorOptionsTypeError,CnpjValidatorOptionTypeInvalidException, and related classes.
Invalid option types on CNPJ are TypeError subclasses; invalid option values are Exception subclasses. CPF and CNPJ validation failures return false. CPF formatting length failure is handled by onFail (default: return input); CNPJ formatting length failure uses onFail (default: return '').
For exhaustive exception lists and edge-case behavior, see each bundled package README.
Bundled packages
| Package | Main resources | README |
|---|---|---|
lacus/cpf-utils |
CpfUtils, CpfFormatter, CpfGenerator, CpfValidator, cpf_fmt(), cpf_gen(), cpf_val() |
docs |
lacus/cnpj-utils |
CnpjUtils, CnpjFormatter, CnpjGenerator, CnpjValidator, CnpjType, CnpjValidationType, cnpj_fmt(), cnpj_gen(), cnpj_val() |
docs |
All CPF symbols are available under Lacus\BrUtils\Cpf\; all CNPJ symbols under Lacus\BrUtils\Cnpj\. Interactive demos: CPF and CNPJ.
API
BrUtils: Top-level façade with$cpf/$cnpjproperty access andgetCpfUtils()/getCnpjUtils()CpfUtils: Domain aggregator for CPF format, generate, and validateCnpjUtils: Domain aggregator for CNPJ format, generate, and validateCpfFormatter,CpfFormatterOptions,CpfGenerator,CpfGeneratorOptions,CpfValidator: CPF component classesCnpjFormatter,CnpjFormatterOptions,CnpjGenerator,CnpjGeneratorOptions,CnpjValidator,CnpjValidatorOptions: CNPJ component classesCnpjGenerationType,CnpjValidationType: CNPJ generation and validation enumscpf_fmt(),cpf_gen(),cpf_val(): CPF functional helpers (Lacus\BrUtils\Cpf\)cnpj_fmt(),cnpj_gen(),cnpj_val(): CNPJ functional helpers (Lacus\BrUtils\Cnpj\)- Exceptions: CPF —
InvalidArgumentExceptionfor invalid options; CNPJ — fullTypeError/Exceptionhierarchies from bundled packages (see linked READMEs)
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