helpers.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. use Carbon\Carbon;
  3. use Carbon\CarbonInterval;
  4. const MiB = 1048576;
  5. const GiB = 1073741824;
  6. const TiB = 1099511627776;
  7. const Minute = 60;
  8. const Hour = 3600;
  9. const Day = 86400;
  10. const Mbps = 125000;
  11. // base64加密(处理URL)
  12. if (! function_exists('base64url_encode')) {
  13. function base64url_encode(string $data): string
  14. {
  15. return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
  16. }
  17. }
  18. // base64解密(处理URL)
  19. if (! function_exists('base64url_decode')) {
  20. function base64url_decode(string $data): false|string
  21. {
  22. return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
  23. }
  24. }
  25. // 根据流量值自动转换单位输出
  26. if (! function_exists('formatBytes')) {
  27. function formatBytes(int $bytes, ?string $base = null, int $precision = 2): string
  28. {
  29. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  30. $bytes = max($bytes, 0);
  31. $power = floor(($bytes ? log($bytes) : 0) / log(1024));
  32. $power = min($power, count($units) - 1);
  33. $bytes /= 1024 ** $power;
  34. if ($base) {
  35. $power += max(array_search($base, $units), 0);
  36. }
  37. return round($bytes, $precision).' '.$units[$power];
  38. }
  39. }
  40. // 秒转时间
  41. if (! function_exists('formatTime')) {
  42. function formatTime(int $seconds): string
  43. {
  44. $interval = CarbonInterval::seconds($seconds);
  45. return $interval->cascade()->forHumans();
  46. }
  47. }
  48. // 获取系统设置
  49. if (! function_exists('sysConfig')) {
  50. function sysConfig(?string $key = null, ?string $default = null): array|string|int|null
  51. {
  52. return $key ? config("settings.$key", $default) : config('settings');
  53. }
  54. }
  55. // Array values and indexes clean
  56. if (! function_exists('array_clean')) {
  57. function array_clean(array &$array): array
  58. {
  59. foreach ($array as $key => &$value) {
  60. if (is_array($value)) {
  61. $value = array_clean($value);
  62. }
  63. if (empty($value)) {
  64. unset($array[$key]);
  65. }
  66. }
  67. return $array;
  68. }
  69. }
  70. // string url safe sanitize
  71. if (! function_exists('string_urlsafe')) {
  72. function string_urlsafe(string $string, bool $force_lowercase = true, bool $anal = false): string
  73. {
  74. $clean = preg_replace('/[~`!@#$%^&*()_=+\[\]{}\\|;:"\'<>,.?\/]/', '_', strip_tags($string));
  75. $clean = preg_replace('/\s+/', '-', $clean);
  76. $clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
  77. if ($force_lowercase) {
  78. $clean = function_exists('mb_strtolower') ? mb_strtolower($clean, 'UTF-8') : strtolower($clean);
  79. }
  80. return $clean;
  81. }
  82. }
  83. if (! function_exists('localized_date')) {
  84. function localized_date($date): string
  85. {
  86. $locale = app()->getLocale();
  87. $carbon = Carbon::parse($date);
  88. $format = config("common.language.$locale.3") ?? 'Y-m-d';
  89. return $carbon->format($format);
  90. }
  91. }