lacus/cpf-dv
Composer 安装命令:
composer require lacus/cpf-dv
包简介
Utility to calculate check digits on CPF (Brazilian Individual's Taxpayer ID)
关键字:
README 文档
README
A PHP utility to calculate check digits on CPF (Brazilian Individual's Taxpayer ID).
PHP Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Flexible input: Accepts
stringorarrayof strings - ✅ Format agnostic: Automatically strips non-numeric characters from string input
- ✅ Auto-expansion: Multi-character strings in arrays are expanded to individual 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,cpf(via magic__get) - ✅ Minimal dependencies: Only
lacus/utils - ✅ Error handling: Specific types for type, length, and invalid CPF scenarios (
TypeErrorvsExceptionsemantics)
Installation
# using Composer
$ composer require lacus/cpf-dv
Quick Start
<?php use Lacus\BrUtils\Cpf\CpfCheckDigits; $checkDigits = new CpfCheckDigits('054496519'); $checkDigits->first; // '1' $checkDigits->second; // '0' $checkDigits->both; // '10' $checkDigits->cpf; // '05449651910'
Usage
The main resource of this package is the class CpfCheckDigits. Through an instance, you access CPF check-digit information:
__construct:new CpfCheckDigits(string|array $cpfInput)— 9–11 digits (formatting stripped from strings).first: First check digit (10th digit of the CPF). Lazy, cached.second: Second check digit (11th digit of the CPF). Lazy, cached.both: Both check digits concatenated as a string.cpf: The complete CPF as a string of 11 digits (9 base digits + 2 check digits).
Input formats
The CpfCheckDigits class accepts multiple input formats:
String input: plain digits or formatted CPF (e.g. 054.496.519-10). Non-numeric characters are automatically stripped. Use 9 digits (base only) or 11 digits (only the first 9 are used).
Array of strings: single-character strings or multi-character strings (expanded to individual digits), e.g. ['0','5','4','4','9','6','5','1','9'], ['054496519'], ['054','496','519'].
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 CPF). You can catch specific classes or use the abstract bases.
- CpfCheckDigitsTypeError (abstract) — base for type errors; extends PHP’s
TypeError - CpfCheckDigitsInputTypeError — input is not
stringorstring[] - CpfCheckDigitsException (abstract) — base for data/flow exceptions; extends
Exception - CpfCheckDigitsInputLengthException — sanitized length is not 9–11
- CpfCheckDigitsInputInvalidException — input ineligible (e.g. repeated digits like
111111111)
<?php use Lacus\BrUtils\Cpf\CpfCheckDigits; use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsException; use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputInvalidException; use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputLengthException; use Lacus\BrUtils\Cpf\Exceptions\CpfCheckDigitsInputTypeError; // Input type (e.g. integer not allowed) try { new CpfCheckDigits(12345678901); } catch (CpfCheckDigitsInputTypeError $e) { echo $e->getMessage(); } // Length (must be 9–11 digits after sanitization) try { new CpfCheckDigits('12345678'); } catch (CpfCheckDigitsInputLengthException $e) { echo $e->getMessage(); } // Invalid (e.g. repeated digits) try { new CpfCheckDigits(['999', '999', '999']); } catch (CpfCheckDigitsInputInvalidException $e) { echo $e->getMessage(); } // Any data exception from the package try { // risky code } catch (CpfCheckDigitsException $e) { // handle }
Other available resources
CPF_MIN_LENGTH:9— class constantCpfCheckDigits::CPF_MIN_LENGTH, and globalLacus\BrUtils\Cpf\CPF_MIN_LENGTHwhen the autoloadedcpf-dv.phpfile is loaded.CPF_MAX_LENGTH:11— class constantCpfCheckDigits::CPF_MAX_LENGTH, and globalLacus\BrUtils\Cpf\CPF_MAX_LENGTHwhencpf-dv.phpis loaded.
Calculation algorithm
The package calculates CPF check digits using the official Brazilian algorithm:
- First check digit (10th position): digits 1–9 of the CPF base; weights 10, 9, 8, 7, 6, 5, 4, 3, 2 (from left to right);
remainder = 11 - (sum(digit × weight) % 11); result is0if remainder > 9, otherwiseremainder. - Second check digit (11th position): digits 1–9 + first check digit; weights 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 (from left to right); same formula.
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/cpf-dv 适用场景与选型建议
lacus/cpf-dv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「check」 「verify」 「verification」 「cpf」 「br」 「pt-BR」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lacus/cpf-dv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lacus/cpf-dv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lacus/cpf-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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-21