lacus/cnpj-gen
Composer 安装命令:
composer require lacus/cnpj-gen
包简介
Utility to generate CNPJ (Brazilian Business Tax ID)
README 文档
README
🚀 Full support for the new alphanumeric CNPJ format.
A PHP utility to generate valid CNPJ (Brazilian Business Tax ID).
PHP Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Alphanumeric CNPJ generation: Supports base characters from
0-9andA-Zwith numeric check digits - ✅ Flexible options API: Use named arguments or a
CnpjGeneratorOptionsinstance - ✅ Configurable base prefix: Provide up to 12 base characters and generate only missing positions
- ✅ Format output toggle: Return compact (
14chars) or formatted (18chars) output - ✅ Per-call override model: Instance defaults can be overridden for one
generate()call only - ✅ Typed option validation: Dedicated
TypeError/Exceptionsubclasses for invalid option usage - ✅ Dual API style: Object-oriented (
CnpjGenerator) and functional (cnpj_gen())
Installation
# using Composer
$ composer require lacus/cnpj-gen
Import
<?php use Lacus\BrUtils\Cnpj\CnpjGenerator; use Lacus\BrUtils\Cnpj\CnpjGeneratorOptions; use Lacus\BrUtils\Cnpj\Enums\CnpjType; use function Lacus\BrUtils\Cnpj\cnpj_gen;
Quick start
<?php use Lacus\BrUtils\Cnpj\CnpjGenerator; $generator = new CnpjGenerator(); $generator->generate(); // e.g. "AB123CDE000196" $generator->generate(format: true); // e.g. "AB.123.CDE/0001-96"
Usage
The main entry points are the class CnpjGenerator, the options object CnpjGeneratorOptions, the enum CnpjType, and the helper cnpj_gen().
CnpjGenerator
-
__construct:new CnpjGenerator(?CnpjGeneratorOptions $options = null, $format = null, $prefix = null, $type = null)If
$optionsis aCnpjGeneratorOptionsinstance, that same instance is stored internally (mutations later affect futuregenerate()calls with no per-call override). Otherwise, a new options object is built from named values. -
getOptions(): Returns the internalCnpjGeneratorOptionsinstance. -
generate:generate(?CnpjGeneratorOptions $options = null, $format = null, $prefix = null, $type = null): stringPer-call options are merged over instance defaults only for that call. Returned value is:
14characters whenformat = false(default)18characters with separators whenformat = true(XX.XXX.XXX/XXXX-XX)
<?php use Lacus\BrUtils\Cnpj\CnpjGenerator; use Lacus\BrUtils\Cnpj\Enums\CnpjType; $generator = new CnpjGenerator(type: CnpjType::Numeric); $generator->generate(); // e.g. "12345678000195" $generator->generate(format: true); // e.g. "12.345.678/0001-95" $generator->generate(prefix: 'AB123CDE'); // e.g. "AB123CDE000196" $generator->generate(prefix: 'AB123CDE', format: true); // e.g. "AB.123.CDE/0001-96"
Default options on the instance; per-call overrides:
$generator = new CnpjGenerator(format: true, type: CnpjType::Numeric); $generator->generate(); // formatted numeric CNPJ $generator->generate(format: false); // this call only: unformatted $generator->generate(); // formatted again (instance defaults preserved)
CnpjGeneratorOptions
CnpjGeneratorOptions encapsulates generator configuration (format, prefix, type), supports magic property access (__get/__set), and can merge layered values through overrides.
- Constructor:
new CnpjGeneratorOptions($format = null, $prefix = null, $type = null, ?array $overrides = [])overridesaccepts a list of arrays and/or otherCnpjGeneratorOptionsinstances- merge order is left to right (last override wins)
set(...): Updates one or more option values and returns$thisgetAll(): Returns a shallow snapshot array (format,prefix,type)
Generation options
| 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 |
CnpjType|'alphanumeric'|'alphabetic'|'numeric'|null |
CnpjType::Alphanumeric |
Character family used for generated base positions (0-9, A-Z, or both) |
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)
CnpjType
Available enum cases:
CnpjType::AlphanumericCnpjType::AlphabeticCnpjType::Numeric
Helper methods:
CnpjType::values(): list<string>CnpjType::toSequenceType(): SequenceType
Functional helper
cnpj_gen() is a convenience wrapper:
- Builds a new
CnpjGeneratorwith the same constructor arguments - Calls
generate()once
$cnpj = cnpj_gen(); // e.g. "AB123CDE000196" $cnpj = cnpj_gen(format: true); // e.g. "AB.123.CDE/0001-96" $cnpj = cnpj_gen(prefix: '12345678', type: CnpjType::Numeric);
Errors & exceptions
This package uses TypeError vs Exception semantics:
- Type errors indicate wrong API/option types
- Exceptions indicate invalid option values or business-rule violations
Relevant classes:
CnpjGeneratorTypeError(abstract, extends PHPTypeError)CnpjGeneratorOptionsTypeErrorCnpjGeneratorException(abstract, extendsException)CnpjGeneratorOptionPrefixInvalidExceptionCnpjGeneratorOptionTypeInvalidException
<?php use Lacus\BrUtils\Cnpj\CnpjGenerator; use Lacus\BrUtils\Cnpj\Exceptions\CnpjGeneratorOptionPrefixInvalidException; use Lacus\BrUtils\Cnpj\Exceptions\CnpjGeneratorOptionTypeInvalidException; use Lacus\BrUtils\Cnpj\Exceptions\CnpjGeneratorOptionsTypeError; try { $generator = new CnpjGenerator(prefix: '00000000'); $generator->generate(); } catch (CnpjGeneratorOptionPrefixInvalidException $e) { echo $e->getMessage(); } try { new CnpjGenerator(type: 'invalid'); } catch (CnpjGeneratorOptionTypeInvalidException $e) { echo $e->getMessage(); } try { new CnpjGenerator(prefix: 123); } catch (CnpjGeneratorOptionsTypeError $e) { echo $e->getMessage(); }
Other available resources
CnpjGeneratorOptions::CNPJ_LENGTH(14)CnpjGeneratorOptions::CNPJ_PREFIX_MAX_LENGTH(12)
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-gen 适用场景与选型建议
lacus/cnpj-gen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 964 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「faker」 「fake」 「generate」 「cnpj」 「br」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lacus/cnpj-gen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lacus/cnpj-gen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lacus/cnpj-gen 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Mocks, stubs, and spies for PHP.
Phony for Kahlan.
Symfony bundle to manage fixtures with Alice and Faker.
Additional plugin for fakerphp/faker that allows you to generate a random animal
Mock PSR-18 HTTP client
统计信息
- 总下载量: 964
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-03