lacus/cpf-val 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lacus/cpf-val

Composer 安装命令:

composer require lacus/cpf-val

包简介

Utility function to validate CPF (Brazilian personal ID)

README 文档

README

cpf-val for PHP

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

Utility function/class to validate CPF (Brazilian personal ID).

PHP Support

PHP 8.1 PHP 8.2 PHP 8.3 PHP 8.4
Passing ✔ Passing ✔ Passing ✔ Passing ✔

Installation

# using Composer
$ composer require lacus/cpf-val

Import

<?php
// Using class-based resource
use Lacus\CpfVal\CpfValidator;

// Or using function-based one
use function Lacus\CpfVal\cpf_val;

Usage

Object-Oriented Usage

$validator = new CpfValidator();
$cpf = '11144477735';

echo $validator->isValid($cpf) ? 'Valid' : 'Invalid';  // returns 'Valid'

$cpf = '111.444.777-35';
echo $validator->isValid($cpf) ? 'Valid' : 'Invalid';  // returns 'Valid'

$cpf = '11144477736';
echo $validator->isValid($cpf) ? 'Valid' : 'Invalid';  // returns 'Invalid'

Imperative programming

The helper function cpf_val() is just a functional abstraction. Internally it creates an instance of CpfValidator and calls the isValid() method right away.

$cpf = '11144477735';

echo cpf_val($cpf) ? 'Valid' : 'Invalid';      // returns 'Valid'

echo cpf_val('111.444.777-35') ? 'Valid' : 'Invalid';  // returns 'Valid'

echo cpf_val('11144477736') ? 'Valid' : 'Invalid';     // returns 'Invalid'

Validation Examples

// Valid CPF numbers
cpf_val('11144477735')      // returns true
cpf_val('111.444.777-35')   // returns true
cpf_val('12345678909')      // returns true

// Invalid CPF numbers
cpf_val('11144477736')      // returns false
cpf_val('12345678901')      // returns false
cpf_val('00000000000')      // returns false
cpf_val('11111111111')      // returns false
cpf_val('123')              // returns false (too short)
cpf_val('')                 // returns false (empty)

Features

  • Format Agnostic: Accepts CPF with or without formatting (dots, dashes)
  • Strict Validation: Validates both check digits according to Brazilian CPF algorithm
  • Type Safety: Built with PHP 8.1+ strict types
  • Lightweight: Minimal dependencies, only requires lacus/cpf-gen for check digit calculation
  • Dual API: Both object-oriented and functional programming styles supported

API Reference

CpfValidator Class

isValid(string $cpfString): bool

Validates a CPF string and returns true if valid, false otherwise.

Parameters:

  • $cpfString (string): The CPF to validate (with or without formatting)

Returns:

  • bool: true if the CPF is valid, false otherwise

cpf_val() Function

cpf_val(string $cpfString): bool

Functional wrapper around CpfValidator::isValid().

Parameters:

  • $cpfString (string): The CPF to validate (with or without formatting)

Returns:

  • bool: true if the CPF is valid, false otherwise

Validation Algorithm

The package validates CPF using the official Brazilian algorithm:

  1. Length Check: Ensures the CPF has exactly 11 digits
  2. First Check Digit: Calculates and validates the 10th digit
  3. Second Check Digit: Calculates and validates the 11th digit
  4. Format Tolerance: Automatically strips non-numeric characters before validation

Error Handling

The validator is designed to be forgiving with input format but strict with validation:

  • Invalid formats (too short, too long) return false
  • Invalid check digits return false
  • Empty strings return false
  • Non-numeric strings (after stripping formatting) return false

Dependencies

  • PHP: >= 8.1
  • lacus/cpf-gen: ^1.0 (for check digit calculation)

Contribution & Support

We welcome contributions! Please see our Contributing Guidelines for details. But 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/cpf-val 适用场景与选型建议

lacus/cpf-val 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 609 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「validate」 「valid」 「cpf」 「br」 「validador」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lacus/cpf-val 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lacus/cpf-val 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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