|
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 (!isset($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
|
|
|
$diff = $base_time->diff($target_time); |
|
103
|
|
|
|
|
104
|
|
|
$string = ['year' => 'y', 'month' => 'm', 'day' => 'd', 'hour' => 'h', 'minute' => 'i', 'second' => 's']; |
|
105
|
|
|
foreach ($string as $k => &$v) { |
|
106
|
|
|
if ($diff->$v) { |
|
107
|
|
|
$v = $diff->$v; |
|
108
|
|
|
} |
|
109
|
|
|
else unset($string[$k]); |
|
110
|
|
|
} |
|
111
|
|
|
$string['status'] = $base_time < $target_time ? 'later' : 'ago'; |
|
112
|
|
|
|
|
113
|
|
|
return count($string) > 1 ? $string : ['status' => 'now']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* same as mysqli::real_escape_string but does not need a db connection and allow array escape |
|
118
|
|
|
* |
|
119
|
|
|
* e.g. => tools::realEscapeString(input: $text1); |
|
120
|
|
|
* |
|
121
|
|
|
* e.g. => tools::realEscapeString([$text1,$text2,$text3]); |
|
122
|
|
|
* |
|
123
|
|
|
* @param string|string[] $input |
|
124
|
|
|
* |
|
125
|
|
|
* @return string[]|string |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function realEscapeString(string|array $input): string|array { |
|
128
|
|
|
if(is_array($input)) { |
|
|
|
|
|
|
129
|
|
|
return array_map(__METHOD__, $input); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if(!empty($input) && is_string($input)) { |
|
133
|
|
|
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $input); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $input; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* replace `search` with `replace` in `subject` but only one of it(the first result) |
|
141
|
|
|
* |
|
142
|
|
|
* e.g. => tools::strReplaceFirst('hello','bye','hello :)'); |
|
143
|
|
|
* |
|
144
|
|
|
* @param string|string[] $search |
|
145
|
|
|
* @param string|string[] $replace |
|
146
|
|
|
* @param string|string[] $subject |
|
147
|
|
|
* |
|
148
|
|
|
* @return string[]|string |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function strReplaceFirst(string|array $search, string|array $replace, string|array $subject): string|array { |
|
151
|
|
|
$pos = strpos($subject, $search); |
|
|
|
|
|
|
152
|
|
|
if ($pos !== false) { |
|
153
|
|
|
return substr_replace($subject, $replace, $pos, strlen($search)); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
return $subject; |
|
156
|
|
|
} |
|
157
|
|
|
} |