定制 lacus/cnpj-gen 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

lacus/cnpj-gen

Composer 安装命令:

composer require lacus/cnpj-gen

包简介

Utility to generate CNPJ (Brazilian Business Tax ID)

README 文档

README

cnpj-gen for PHP

Packagist Version Packagist Downloads PHP Version Test Status Last Update Date Project License

🚀 Full support for the new alphanumeric CNPJ format.

🌎 Acessar documentação em português

A PHP utility to generate valid CNPJ (Brazilian Business Tax ID).

PHP Support

PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
Passing ✔ Passing ✔ Passing ✔ Passing ✔

Features

  • Alphanumeric CNPJ generation: Supports base characters from 0-9 and A-Z with numeric check digits
  • Flexible options API: Use named arguments or a CnpjGeneratorOptions instance
  • Configurable base prefix: Provide up to 12 base characters and generate only missing positions
  • Format output toggle: Return compact (14 chars) or formatted (18 chars) output
  • Per-call override model: Instance defaults can be overridden for one generate() call only
  • Typed option validation: Dedicated TypeError/Exception subclasses 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 $options is a CnpjGeneratorOptions instance, that same instance is stored internally (mutations later affect future generate() calls with no per-call override). Otherwise, a new options object is built from named values.

  • getOptions(): Returns the internal CnpjGeneratorOptions instance.

  • generate: generate(?CnpjGeneratorOptions $options = null, $format = null, $prefix = null, $type = null): string

    Per-call options are merged over instance defaults only for that call. Returned value is:

    • 14 characters when format = false (default)
    • 18 characters with separators when format = 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 = [])
    • overrides accepts a list of arrays and/or other CnpjGeneratorOptions instances
    • merge order is left to right (last override wins)
  • set(...): Updates one or more option values and returns $this
  • getAll(): 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 00000000 is rejected (when first 8 chars are present)
  • branch ID 0000 is rejected (when chars 9-12 are present)
  • 12 repeated numeric digits are rejected (e.g. 111111111111)

CnpjType

Available enum cases:

  • CnpjType::Alphanumeric
  • CnpjType::Alphabetic
  • CnpjType::Numeric

Helper methods:

  • CnpjType::values(): list<string>
  • CnpjType::toSequenceType(): SequenceType

Functional helper

cnpj_gen() is a convenience wrapper:

  • Builds a new CnpjGenerator with 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 PHP TypeError)
  • CnpjGeneratorOptionsTypeError
  • CnpjGeneratorException (abstract, extends Exception)
  • CnpjGeneratorOptionPrefixInvalidException
  • CnpjGeneratorOptionTypeInvalidException
<?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:

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 lacus/cnpj-gen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-03