|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BPT\tools; |
|
4
|
|
|
|
|
5
|
|
|
use BPT\constants\parseMode; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
trait convert { |
|
10
|
|
|
/** |
|
11
|
|
|
* Convert byte to symbolic size like 2.98 MB |
|
12
|
|
|
* |
|
13
|
|
|
* You could set `precision` to configure decimals after number(2 for 2.98 and 3 for 2.987) |
|
14
|
|
|
* |
|
15
|
|
|
* e.g. => tools::byteFormat(123456789); |
|
16
|
|
|
* |
|
17
|
|
|
* e.g. => tools::byteFormat(byte: 123456789); |
|
18
|
|
|
* |
|
19
|
|
|
* @param int $byte size in byte |
|
20
|
|
|
* @param int $precision decimal precision |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function byteFormat (int $byte, int $precision = 2): string { |
|
25
|
|
|
$rate_counter = 0; |
|
26
|
|
|
|
|
27
|
|
|
while ($byte > 1024){ |
|
28
|
|
|
$byte /= 1024; |
|
29
|
|
|
$rate_counter++; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($rate_counter !== 0) { |
|
33
|
|
|
$byte = round($byte, $precision); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $byte . ' ' . ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'][$rate_counter]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Escape text for different parse_modes |
|
41
|
|
|
* |
|
42
|
|
|
* mode parameter can be : `MarkdownV2` , `Markdown` , `HTML` , default : `parseMode::HTML`(`HTML`) |
|
43
|
|
|
* |
|
44
|
|
|
* e.g. => tools::modeEscape('hello men! *I* Have nothing anymore'); |
|
45
|
|
|
* |
|
46
|
|
|
* e.g. => tools::modeEscape(text: 'hello men! *I* Have nothing anymore'); |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $text Your text e.g. => 'hello men! *I* Have nothing anymore' |
|
49
|
|
|
* @param string $mode Your selected mode e.g. => `parseMode::HTML` | `HTML` |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|false return false when mode is incorrect |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function modeEscape (string $text, string $mode = parseMode::HTML): string|false { |
|
54
|
|
|
return match ($mode) { |
|
55
|
|
|
parseMode::HTML => str_replace(['&', '<', '>',], ["&", "<", ">",], $text), |
|
56
|
|
|
parseMode::MARKDOWN => str_replace(['\\', '_', '*', '`', '['], ['\\\\', '\_', '\*', '\`', '\[',], $text), |
|
57
|
|
|
parseMode::MARKDOWNV2 => str_replace( |
|
58
|
|
|
['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\\'], |
|
59
|
|
|
['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!', '\\\\'], |
|
60
|
|
|
$text), |
|
61
|
|
|
default => false |
|
62
|
|
|
}; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Clear text and make it safer to use |
|
67
|
|
|
* |
|
68
|
|
|
* e.g. => tools::clearText(text: 'asdasdasdas'); |
|
69
|
|
|
* |
|
70
|
|
|
* e.g. => tools::clearText($message->text); |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $text your text to be cleaned |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function clearText(string $text): string { |
|
77
|
|
|
return htmlentities(strip_tags(htmlspecialchars(stripslashes(trim($text))))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Show time different in array format |
|
82
|
|
|
* |
|
83
|
|
|
* Its calculated different between given time and now |
|
84
|
|
|
* |
|
85
|
|
|
* e.g. => tools::time2string(datetime: 1636913656); |
|
86
|
|
|
* |
|
87
|
|
|
* e.g. => tools::time2string(time()); |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|string $target_time your chosen time for compare with base_time, could be timestamp or could be a string like `next sunday` |
|
90
|
|
|
* @param int|string|null $base_time base time, could be timestamp or could be a string like `next sunday`, set null for current time |
|
91
|
|
|
* |
|
92
|
|
|
* @return array{status: string,year: string,month: string,day: string,hour: string,minute: string,second: string} |
|
93
|
|
|
* @throws Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function timeDiff (int|string $target_time, int|string|null $base_time = null): array { |
|
96
|
|
|
if (empty($base_time)) { |
|
97
|
|
|
$base_time = '@'.time(); |
|
98
|
|
|
} |
|
99
|
|
|
$base_time = new DateTime($base_time); |
|
100
|
|
|
$target_time = new DateTime(is_numeric($target_time) ? '@' . $target_time : $target_time . ' +00:00'); |
|
101
|
|
|
|
|
102
|
|
|
$status = $base_time < $target_time ? 'later' : 'ago'; |
|
103
|
|
|
$diff = $base_time->diff($target_time); |
|
104
|
|
|
|
|
105
|
|
|
$string = ['year' => 'y', 'month' => 'm', 'day' => 'd', 'hour' => 'h', 'minute' => 'i', 'second' => 's']; |
|
106
|
|
|
foreach ($string as $k => &$v) { |
|
107
|
|
|
if ($diff->$v) { |
|
108
|
|
|
$v = $diff->$v; |
|
109
|
|
|
} |
|
110
|
|
|
else unset($string[$k]); |
|
111
|
|
|
} |
|
112
|
|
|
$string['status'] = $status; |
|
113
|
|
|
|
|
114
|
|
|
return count($string) > 1 ? $string : ['status' => 'now']; |
|
115
|
|
|
} |
|
116
|
|
|
} |