<?php
declare (strict_types=1);
namespace DtApp\ThinkLibrary\extend;
class CodeExtend
{
public static function random(int $size = 10, int $type = 1, string $prefix = ''): string
{
$numbs = '0123456789';
$chars = 'abcdefghijklmnopqrstuvwxyz';
if ($type === 1) {
$chars = $numbs;
}
if ($type === 3) {
$chars = "{$numbs}{$chars}";
}
$code = $prefix . $chars[random_int(1, strlen($chars) - 1)];
while (strlen($code) < $size) {
$code .= $chars[random_int(0, strlen($chars) - 1)];
}
return $code;
}
public static function uniqidDate(int $size = 16, string $prefix = ''): string
{
if ($size < 14) {
$size = 14;
}
$code = $prefix . date('Ymd') . (date('H') + date('i')) . date('s');
while (strlen($code) < $size) {
$code .= random_int(0, 9);
}
return $code;
}
public static function uniqidNumber(int $size = 12, string $prefix = ''): string
{
$time = time() . '';
if ($size < 10) {
$size = 10;
}
$code = $prefix . ((int)$time[0] + (int)$time[1]) . substr($time, 2) . random_int(0, 9);
while (strlen($code) < $size) {
$code .= random_int(0, 9);
}
return $code;
}
}