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

94 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace DtApp\ThinkLibrary\helper;
use Exception;
/**
* 随机管理类
* @mixin Randoms
* @package DtApp\ThinkLibrary\helper
*/
class Randoms
{
/**
* 生成随机
* @param int $length 长度
* @param int $type 类型1 纯数字2 纯小写字母3 纯大写字母4 数字和小写字母5 数字和大写字母6 大小写字母7 数字和大小写字母
* @return string
* @throws Exception
*/
public function generate(int $length = 6, int $type = 1): string
{
// 取字符集数组
$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++) {
$str .= $charset[random_int(0, count($charset) - 1)];
// 验证规则
if ($type === 4 && strlen($str) >= 2) {
if (!preg_match('/\d+/', $str) || !preg_match('/[a-z]+/', $str)) {
$str = substr($str, 0, -1);
--$i;
}
}
if ($type === 5 && strlen($str) >= 2) {
if (!preg_match('/\d+/', $str) || !preg_match('/[A-Z]+/', $str)) {
$str = substr($str, 0, -1);
--$i;
}
}
if ($type === 6 && strlen($str) >= 2) {
if (!preg_match('/[a-z]+/', $str) || !preg_match('/[A-Z]+/', $str)) {
$str = substr($str, 0, -1);
--$i;
}
}
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);
$i -= 2;
}
}
}
return $str;
}
}