FormatHelper.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Module\Cleanup\Helpers;
  3. /**
  4. * 格式化帮助类
  5. *
  6. * 提供各种数据格式化功能
  7. */
  8. class FormatHelper
  9. {
  10. /**
  11. * 格式化字节大小
  12. *
  13. * @param int $bytes 字节数
  14. * @param int $precision 精度
  15. * @return string 格式化后的大小
  16. */
  17. public static function formatBytes(int $bytes, int $precision = 2): string
  18. {
  19. if ($bytes == 0) {
  20. return '0 B';
  21. }
  22. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  23. $base = log($bytes, 1024);
  24. $index = floor($base);
  25. if ($index >= count($units)) {
  26. $index = count($units) - 1;
  27. }
  28. $size = round(pow(1024, $base - $index), $precision);
  29. return $size . ' ' . $units[$index];
  30. }
  31. /**
  32. * 格式化执行时间
  33. *
  34. * @param float $seconds 秒数
  35. * @return string 格式化后的时间
  36. */
  37. public static function formatExecutionTime(float $seconds): string
  38. {
  39. if ($seconds < 1) {
  40. return round($seconds * 1000) . ' ms';
  41. } elseif ($seconds < 60) {
  42. return round($seconds, 2) . ' 秒';
  43. } elseif ($seconds < 3600) {
  44. $minutes = floor($seconds / 60);
  45. $remainingSeconds = $seconds % 60;
  46. return $minutes . ' 分 ' . round($remainingSeconds) . ' 秒';
  47. } else {
  48. $hours = floor($seconds / 3600);
  49. $minutes = floor(($seconds % 3600) / 60);
  50. return $hours . ' 小时 ' . $minutes . ' 分';
  51. }
  52. }
  53. /**
  54. * 格式化数字
  55. *
  56. * @param int|float $number 数字
  57. * @return string 格式化后的数字
  58. */
  59. public static function formatNumber($number): string
  60. {
  61. if ($number >= 1000000000) {
  62. return round($number / 1000000000, 1) . 'B';
  63. } elseif ($number >= 1000000) {
  64. return round($number / 1000000, 1) . 'M';
  65. } elseif ($number >= 1000) {
  66. return round($number / 1000, 1) . 'K';
  67. } else {
  68. return number_format($number);
  69. }
  70. }
  71. /**
  72. * 格式化百分比
  73. *
  74. * @param float $value 值
  75. * @param float $total 总数
  76. * @param int $precision 精度
  77. * @return string 格式化后的百分比
  78. */
  79. public static function formatPercentage(float $value, float $total, int $precision = 2): string
  80. {
  81. if ($total == 0) {
  82. return '0%';
  83. }
  84. $percentage = ($value / $total) * 100;
  85. return round($percentage, $precision) . '%';
  86. }
  87. /**
  88. * 格式化进度条HTML
  89. *
  90. * @param float $progress 进度值 (0-100)
  91. * @param string $color 颜色类型
  92. * @return string HTML字符串
  93. */
  94. public static function formatProgressBar(float $progress, string $color = 'primary'): string
  95. {
  96. $progress = max(0, min(100, $progress));
  97. return "<div class='progress' style='height: 20px;'>
  98. <div class='progress-bar bg-{$color}' style='width: {$progress}%'>{$progress}%</div>
  99. </div>";
  100. }
  101. /**
  102. * 根据状态获取标签颜色
  103. *
  104. * @param int $status 状态值
  105. * @param array $colorMap 颜色映射
  106. * @return string 颜色类名
  107. */
  108. public static function getStatusColor(int $status, array $colorMap = []): string
  109. {
  110. return $colorMap[$status] ?? 'secondary';
  111. }
  112. /**
  113. * 格式化状态标签
  114. *
  115. * @param int $status 状态值
  116. * @param array $statusMap 状态映射
  117. * @param array $colorMap 颜色映射
  118. * @return string HTML字符串
  119. */
  120. public static function formatStatusLabel(int $status, array $statusMap, array $colorMap = []): string
  121. {
  122. $text = $statusMap[$status] ?? '未知';
  123. $color = self::getStatusColor($status, $colorMap);
  124. return "<span class='badge badge-{$color}'>{$text}</span>";
  125. }
  126. /**
  127. * 格式化时间差
  128. *
  129. * @param string|\DateTime $startTime 开始时间
  130. * @param string|\DateTime|null $endTime 结束时间,null表示当前时间
  131. * @return string 格式化后的时间差
  132. */
  133. public static function formatTimeDiff($startTime, $endTime = null): string
  134. {
  135. if (is_string($startTime)) {
  136. $startTime = new \DateTime($startTime);
  137. }
  138. if ($endTime === null) {
  139. $endTime = new \DateTime();
  140. } elseif (is_string($endTime)) {
  141. $endTime = new \DateTime($endTime);
  142. }
  143. $diff = $endTime->diff($startTime);
  144. if ($diff->days > 0) {
  145. return $diff->days . ' 天';
  146. } elseif ($diff->h > 0) {
  147. return $diff->h . ' 小时 ' . $diff->i . ' 分钟';
  148. } elseif ($diff->i > 0) {
  149. return $diff->i . ' 分钟 ' . $diff->s . ' 秒';
  150. } else {
  151. return $diff->s . ' 秒';
  152. }
  153. }
  154. /**
  155. * 格式化文件路径显示
  156. *
  157. * @param string $path 文件路径
  158. * @param int $maxLength 最大显示长度
  159. * @return string 格式化后的路径
  160. */
  161. public static function formatPath(string $path, int $maxLength = 50): string
  162. {
  163. if (strlen($path) <= $maxLength) {
  164. return $path;
  165. }
  166. $start = substr($path, 0, 20);
  167. $end = substr($path, -($maxLength - 23));
  168. return $start . '...' . $end;
  169. }
  170. }