can/supercan

CaoCan Dedicated package includes common functions

Maintainers

Package info

github.com/CaoXiaoCan/superCan

pkg:composer/can/supercan

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2025-07-24 07:58 UTC

This package is auto-updated.

Last update: 2026-05-18 08:21:01 UTC


README

一个包含常用字符串处理、敏感词过滤、限流策略、高精度数学计算及业务常用工具的 PHP 综合包,支持在 ThinkPHP/Laravel 等框架中快速使用。

✨ 功能模块

  • 字符串工具 (StringHelper):随机串、截取、掩码、脱敏等。
  • 高精度计算 (PreciseMath):基于 BCMath 的链式/静态数学运算。
  • 数组工具 (ArrayHelper):树形转换、分组、索引重排等。
  • 业务校验 (ValidateHelper):身份证、信用代码、手机号校验。
  • 时间工具 (TimeHelper):人性化时间、常用时间段获取。
  • 敏感词过滤 (Sensitive):基于 DFA 思想的高性能过滤。
  • 限流组件 (RateLimiter):基于 Redis 的接口限流。
  • CURL 封装 (Curl):简单优雅的 HTTP 请求封装。
  • 大文件处理 (FileHelper):生成器低内存逐行读取、分块读取、极速行数统计。
  • 加密解密 (EncryptHelper):AES 对称加密、安全的密码单向 Hash。
  • 接口安全 (ApiSecurityHelper):类似微信的接口参数签名、报文 AES 加解密。

📦 安装方式

composer require can/supercan

🚀 使用手册

1. 高精度数学计算 (PreciseMath)

use can\superCan\PreciseMath;

// 静态调用
$sum = PreciseMath::add('1.1', '2.2'); // '3.30'

// 链式调用
$result = PreciseMath::calc('10.00')
    ->add('5.5')
    ->mul('2')
    ->getResult(); // '31.00'

2. 数组助手 (ArrayHelper)

use can\superCan\arrayhelper\ArrayHelper;

// 扁平数组转树形 (常用于菜单、分类)
$tree = ArrayHelper::toTree($list, 'id', 'pid');

// 树形转扁平列表
$flatList = ArrayHelper::treeToList($tree, 'children');

// 按字段分组
$groups = ArrayHelper::groupBy($list, 'type');

// 提取多维数组特定字段为键名
$indexed = ArrayHelper::arrayIndex($list, 'id');

// 二维数组多字段排序
$sorted = ArrayHelper::sortBy($list, 'score', SORT_DESC);

// 提取/排除指定的键
$only = ArrayHelper::only($user, ['id', 'name']);
$except = ArrayHelper::except($user, ['password']);

// 多维数组平铺为一维
$flat = ArrayHelper::flatten([[1, 2], [3, [4, 5]]]);

// 点语法获取深层数组值
$name = ArrayHelper::get($data, 'user.profile.name', '默认值');

// 判断是否为关联数组
$isAssoc = ArrayHelper::isAssoc(['name' => 'caocan']);

// 设置多维数组的值 (点语法)
ArrayHelper::set($data, 'a.b.c', 'value');

// 判断多维数组键是否存在 (点语法)
$hasKey = ArrayHelper::has($data, 'a.b.c');

// 获取符合条件的第一个/最后一个元素
$first = ArrayHelper::first($list, function($v) { return $v > 10; });
$last = ArrayHelper::last($list, function($v) { return $v < 100; });

// 提取多维数组中某个字段符合条件的所有项
$activeUsers = ArrayHelper::where($users, 'status', 1);

// 获取多维数组的某一列 (支持点语法)
$names = ArrayHelper::pluck($users, 'user.name', 'user.id');

3. 业务校验 (ValidateHelper)

use can\superCan\validatehelper\ValidateHelper;

ValidateHelper::isIdCard('身份证号'); // bool
ValidateHelper::isCreditCode('营业执照代码'); // bool
ValidateHelper::isMobile('13800138000'); // bool

4. 字符串助手 (StringHelper)

use can\superCan\stringhelper\StringHelper;

StringHelper::random(16, 2); // 生成16位随机字母数字
StringHelper::maskPhone('13812345678'); // '138****5678'
StringHelper::camelToSnake('UserOrder'); // 'user_order'

5. 时间助手 (TimeHelper)

use can\superCan\timehelper\TimeHelper;

TimeHelper::humanDate(time() - 3600); // '1小时前'
[$start, $end] = TimeHelper::today(); // 今日开始和结束的时间戳

6. 敏感词过滤 (Sensitive)

use can\superCan\sensitive\Sensitive;

$sensitive = new Sensitive();
$clean = $sensitive->load(['关键词1', '关键词2'])
                  ->text('内容...')
                  ->filter();

7. Redis 限流器 (RateLimiter)

use can\superCan\rateLimiter\RateLimiter;

$limiter = new RateLimiter(['host' => '127.0.0.1'], 5, 60);
if (!$limiter->allow('user_1')) {
    die('请求太频繁');
}

8. CURL 请求 (Curl)

use can\superCan\curl\Curl;

$curl = new Curl();
$res = $curl->get('https://api.example.com/data', ['id' => 1]);
if ($res['success']) {
    print_r($res['data']);
}

9. 大文件处理 (FileHelper)

use can\superCan\filehelper\FileHelper;

// 极速统计大文件行数
$lines = FileHelper::countLines('/path/to/large.log');

// 生成器逐行读取 (极低内存消耗)
foreach (FileHelper::readLines('/path/to/large.log') as $line) {
    // 处理每一行
}

// 分块读取并执行回调
FileHelper::chunkRead('/path/to/large.log', function($chunk) {
    // 处理大块数据
});

10. 加密解密 (EncryptHelper)

use can\superCan\encrypthelper\EncryptHelper;

$key = 'my-secret-key-1234567890123456';

// AES 对称加解密
$encrypted = EncryptHelper::aesEncrypt('隐私数据', $key);
$decrypted = EncryptHelper::aesDecrypt($encrypted, $key);

// 密码安全 Hash
$hash = EncryptHelper::hashPassword('123456');
$isValid = EncryptHelper::verifyPassword('123456', $hash);

11. 接口安全加密 (ApiSecurityHelper)

use can\superCan\apisecurity\ApiSecurityHelper;

$apiKey = 'my-secret-key';
$params = ['appid' => 'wx123', 'body' => '测试商品', 'empty' => ''];

// 生成类似微信支付的字典序 MD5 签名 (自动剔除空值和 sign 字段)
$sign = ApiSecurityHelper::generateSign($params, $apiKey);

// 接收端验证签名
$receivedParams = $params;
$receivedParams['sign'] = $sign;
$isValid = ApiSecurityHelper::verifySign($receivedParams, $apiKey);

// 接口报文 AES 加解密
$aesKey = md5('aes-salt');
$encryptedMsg = ApiSecurityHelper::encryptMsg('{"user": 1}', $aesKey);
$decryptedMsg = ApiSecurityHelper::decryptMsg($encryptedMsg, $aesKey);

📄 开源协议

MIT License