163 lines
4.3 KiB
PHP
163 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
*/
|
|
|
|
namespace App\Utils;
|
|
|
|
use function PHPUnit\Framework\matches;
|
|
|
|
class Str
|
|
{
|
|
/**
|
|
* 获取UUID
|
|
* @param string $prefix
|
|
* @return string
|
|
*/
|
|
static function uuid(string $prefix = ''): string
|
|
{
|
|
$chars = md5(uniqid(mt_rand(), true));
|
|
$uuid = substr($chars, 0, 8) . '-';
|
|
$uuid .= substr($chars, 8, 4) . '-';
|
|
$uuid .= substr($chars, 12, 4) . '-';
|
|
$uuid .= substr($chars, 16, 4) . '-';
|
|
$uuid .= substr($chars, 20, 12);
|
|
return $prefix . $uuid;
|
|
}
|
|
|
|
/**
|
|
* 获取随机字符串
|
|
* @param int $num
|
|
* @param string $prefix
|
|
* @return string
|
|
*/
|
|
static function randStr(int $num, string $prefix = ""): string
|
|
{
|
|
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
|
$_str = "";
|
|
while ($num > 0) {
|
|
$_str .= substr($str, rand(0, strlen($str) - 1), 1);
|
|
$num--;
|
|
}
|
|
return $prefix . $_str;
|
|
}
|
|
|
|
static function snakeToCamel($str, $capitalized = false)
|
|
{
|
|
$result = str_replace('_', '', ucwords($str, '_'));
|
|
if (!$capitalized) {
|
|
$result = lcfirst($result);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function typeMysqlToPhp($Type): string
|
|
{
|
|
switch ($Type) {
|
|
case str_contains($Type, "int"):
|
|
case str_contains($Type, "smallint"):
|
|
case str_contains($Type, "tinyint"):
|
|
case str_contains($Type, "mediumint"):
|
|
case str_contains($Type, "bigint"):
|
|
case str_contains($Type, "bool"):
|
|
case str_contains($Type, "boolean"):
|
|
return "int";
|
|
case str_contains($Type, "float"):
|
|
case str_contains($Type, "double"):
|
|
return "float";
|
|
case str_contains($Type, "text"):
|
|
case str_contains($Type, "char"):
|
|
case str_contains($Type, "varchar"):
|
|
case str_contains($Type, "blob"):
|
|
case str_contains($Type, "decimal"):
|
|
case str_contains($Type, "numeric"):
|
|
case str_contains($Type, "date"):
|
|
case str_contains($Type, "datetime"):
|
|
case str_contains($Type, "timestamp"):
|
|
default:
|
|
return "string";
|
|
}
|
|
}
|
|
|
|
public static function isInsert($field, $extra): int
|
|
{
|
|
if ($extra == "auto_increment") {
|
|
return 0;
|
|
}
|
|
return !in_array($field, ["deleted_at", "update_time"]) ? 1 : 0;
|
|
}
|
|
|
|
public static function isEdit($field): int
|
|
{
|
|
return !in_array($field, ["deleted_at", "create_time"]) ? 1 : 0;
|
|
}
|
|
|
|
public static function isList($field, $extra): int
|
|
{
|
|
if ($extra == "auto_increment") {
|
|
return 1;
|
|
}
|
|
return !in_array($field, ["deleted_at"]) ? 1 : 0;
|
|
}
|
|
|
|
public static function isQuery($field, $extra): int
|
|
{
|
|
if ($extra == "auto_increment") {
|
|
return 0;
|
|
}
|
|
return !in_array($field, ["deleted_at", "update_time", "create_time"]) ? 1 : 0;
|
|
}
|
|
|
|
public static function htmlType($field): string
|
|
{
|
|
switch ($field) {
|
|
case "status":
|
|
return "radio";
|
|
case str_contains($field, "_time"):
|
|
return "datetime";
|
|
case str_contains($field, "_date"):
|
|
return "date";
|
|
default:
|
|
return "text";
|
|
}
|
|
}
|
|
|
|
public static function fileType($ext): string
|
|
{
|
|
switch (strtolower($ext)) {
|
|
case 'bmp':
|
|
case 'jpg':
|
|
case 'jpeg':
|
|
case 'png':
|
|
case 'gif':
|
|
return "image";
|
|
case 'avi':
|
|
case 'mp4':
|
|
case 'flv':
|
|
return "video";
|
|
case 'doc':
|
|
case 'docx':
|
|
return "wold";
|
|
case 'xls':
|
|
case 'xlsx':
|
|
return "excel";
|
|
case 'ppt':
|
|
case 'pptx':
|
|
return "ppt";
|
|
case 'mp3':
|
|
case 'wav':
|
|
return "audio";
|
|
default:
|
|
return "other";
|
|
}
|
|
}
|
|
|
|
static function splitKey(string $key): array
|
|
{
|
|
$parts = explode('.', $key);
|
|
$first = array_shift($parts);
|
|
$rest = implode('.', $parts);
|
|
return [$first, $rest];
|
|
}
|
|
|
|
} |