You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
think-library/src/helper/Randoms.php

97 lines
3.6 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | gitlab 仓库地址 https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
4 years ago
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
4 years ago
declare (strict_types=1);
4 years ago
4 years ago
namespace DtApp\ThinkLibrary\helper;
4 years ago
4 years ago
use Exception;
4 years ago
/**
* 随机管理类
4 years ago
* @mixin Randoms
4 years ago
* @package DtApp\ThinkLibrary\helper
4 years ago
*/
4 years ago
class Randoms
4 years ago
{
/**
* 生成随机
* @param int $length 长度
* @param int $type 类型1 纯数字2 纯小写字母3 纯大写字母4 数字和小写字母5 数字和大写字母6 大小写字母7 数字和大小写字母
4 years ago
* @return string
4 years ago
* @throws Exception
4 years ago
*/
public function generate(int $length = 6, int $type = 1)
{
// 取字符集数组
$number = range(0, 9);
$lowerLetter = range('a', 'z');
$upperLetter = range('A', 'Z');
// 根据type合并字符集
if ($type == 1) {
$charset = $number;
} elseif ($type == 2) {
$charset = $lowerLetter;
} elseif ($type == 3) {
$charset = $upperLetter;
} elseif ($type == 4) {
$charset = array_merge($number, $lowerLetter);
} elseif ($type == 5) {
$charset = array_merge($number, $upperLetter);
} elseif ($type == 6) {
$charset = array_merge($lowerLetter, $upperLetter);
} elseif ($type == 7) {
$charset = array_merge($number, $lowerLetter, $upperLetter);
} else {
$charset = $number;
}
$str = '';
// 生成字符串
for ($i = 0; $i < $length; $i++) {
4 years ago
$str .= $charset[random_int(0, count($charset) - 1)];
4 years ago
// 验证规则
if ($type == 4 && strlen($str) >= 2) {
if (!preg_match('/\d+/', $str) || !preg_match('/[a-z]+/', $str)) {
$str = substr($str, 0, -1);
4 years ago
--$i;
4 years ago
}
}
if ($type == 5 && strlen($str) >= 2) {
if (!preg_match('/\d+/', $str) || !preg_match('/[A-Z]+/', $str)) {
$str = substr($str, 0, -1);
4 years ago
--$i;
4 years ago
}
}
if ($type == 6 && strlen($str) >= 2) {
if (!preg_match('/[a-z]+/', $str) || !preg_match('/[A-Z]+/', $str)) {
$str = substr($str, 0, -1);
4 years ago
--$i;
4 years ago
}
}
if ($type == 7 && strlen($str) >= 3) {
if (!preg_match('/\d+/', $str) || !preg_match('/[a-z]+/', $str) || !preg_match('/[A-Z]+/', $str)) {
$str = substr($str, 0, -2);
4 years ago
$i -= 2;
4 years ago
}
}
}
return $str;
}
}