-
-
Notifications
You must be signed in to change notification settings - Fork 613
/
Utils.php
195 lines (181 loc) · 5.72 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace App;
use App\Enums\ConfigKey;
use App\Models\Config;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class Utils
{
public static function e(\Throwable $e, $message = '', $level = 'error')
{
Log::{$level}($message, [
'file' => $e->getFile(),
'line' => $e->getLine(),
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
/**
* 获取头像地址
*
* @param $email
* @param int $s
* @param string $d
* @param string $r
* @return string
*/
public static function getAvatar($email, int $s = 96, string $d = 'mp', string $r = 'g'): string
{
$url = 'https://cravatar.cn/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";
return $url;
}
/**
* 获取系统配置,获取全部配置时将返回
*
* @param string $name
* @param mixed|null $default
*
* @return mixed
*/
public static function config(string $name = '', mixed $default = null): mixed
{
/** @var Collection $configs */
$configs = Cache::rememberForever('configs', function () {
return Config::query()->pluck('value', 'name')->transform(function ($value, $key) {
switch ($key) {
case ConfigKey::IsAllowGuestUpload:
case ConfigKey::IsEnableGallery:
case ConfigKey::IsEnableApi:
case ConfigKey::IsEnableRegistration:
case ConfigKey::IsUserNeedVerify:
$value = (bool) $value;
break;
case ConfigKey::Mail:
case ConfigKey::Group:
$value = collect(json_decode($value, true));
break;
case ConfigKey::UserInitialCapacity:
$value = sprintf('%.2f', $value);
break;
default:
}
return $value;
});
});
return '' === $name ? $configs : $configs->get($name, $default);
}
/**
* 生成连续日期.
* @param string $start 开始日期
* @param string $end 结束日期
* @param string $unit day=日,month=月,year=年
* @return array
*/
public static function makeDateRange(string $start, string $end, string $unit = 'day'): array
{
$array = [];
$format = ['day' => 'Y-m-d', 'month' => 'Y-m', 'year' => 'Y'][$unit] ?? 'Y-m-d';
Carbon::create($start)->range($end, 1, $unit)->forEach(function (Carbon $item) use (&$array, $format) {
$array[] = $item->format($format);
});
return $array;
}
/**
* 转换字段单位
*
* @param int|float $size 字节b
* @return string
*/
public static function formatSize(int|float $size): string
{
if ($size <= 0) {
return "0.00 Bytes";
}
$unit = ['', 'K', 'M', 'G', 'T', 'P'];
$base = 1024;
$i = floor(log($size, $base));
$n = count($unit);
if ($i >= $n) {
$i = $n - 1;
}
return sprintf("%.2f", $size / pow($base, $i)).' '.$unit[$i].'B';
}
/**
* 格式化数字
*
* @param int|string $n 数字
* @param int $precision 精度
* @return int|string
*/
public static function shortenNumber(int|string $n, int $precision = 1): int|string
{
if ($n < 1e+3) {
return number_format($n);
} else if ($n < 1e+6) {
return number_format($n / 1e+3, $precision) . 'k';
} else if ($n < 1e+9) {
return number_format($n / 1e+6, $precision) . 'm';
} else if ($n < 1e+12) {
return number_format($n / 1e+9, $precision) . 'b';
}
return $n;
}
/**
* 递归过滤数组元素
*
* @param array $array
* @param callable|null $callback
* @param int $mode
* @return array
*/
public static function filter(array $array, callable $callback = null, int $mode = 0): array
{
foreach ($array as &$value) {
if (is_array($value)) {
$value = self::filter($value, $callback, $mode);
}
}
return array_filter($array, $callback, $mode);
}
/**
* 格式化配置,设置默认配置以及将字符串数字转换为数字
*
* @param array $defaults 默认配置
* @param array $configs 新配置
* @return array
*/
public static function parseConfigs(array $defaults, array $configs): array
{
array_walk_recursive($configs, function (&$item) {
if (ctype_digit($item)) {
$item += 0;
}
if (is_null($item)) {
unset($item);
}
});
return self::array_merge_recursive_distinct($defaults, $configs);
}
/**
* @param array<int|string, mixed> $array1
* @param array<int|string, mixed> $array2
*
* @return array<int|string, mixed>
*/
private static function array_merge_recursive_distinct(array $array1, array &$array2): array
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key]) && ! array_is_list($value)) {
$merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
}