1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Toolkit class |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/shahradelahi/easy-http |
9
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
10
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
11
|
|
|
*/ |
12
|
|
|
class Toolkit |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Builds url from array of parameters |
17
|
|
|
* |
18
|
|
|
* @param string $base The base url |
19
|
|
|
* @param array $body Each segment of the url |
20
|
|
|
* @param array $params The query string parameters |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public static function buildUrl(string $base, array $body, array $params): string |
24
|
|
|
{ |
25
|
|
|
$url = $base; |
26
|
|
|
$url .= '/' . implode('/', $body); |
27
|
|
|
if (count($params) > 0) { |
28
|
|
|
$url .= '?' . http_build_query($params); |
29
|
|
|
} |
30
|
|
|
return $url; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generates a random string |
35
|
|
|
* |
36
|
|
|
* @param int $length The length of the string |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function randomString(int $length = 10): string |
40
|
|
|
{ |
41
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
42
|
|
|
$charactersLength = strlen($characters); |
43
|
|
|
$randomString = ''; |
44
|
|
|
for ($i = 0; $i < $length; $i++) { |
45
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
46
|
|
|
} |
47
|
|
|
return $randomString; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Make Json string pretty |
52
|
|
|
* |
53
|
|
|
* @param string $json The json string |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public static function prettyJson(string $json): string |
57
|
|
|
{ |
58
|
|
|
return json_encode(json_decode($json), JSON_PRETTY_PRINT); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convert bytes to human-readable format |
63
|
|
|
* |
64
|
|
|
* @param int $bytes The bytes |
65
|
|
|
* @param bool $binaryPrefix Whether to use binary prefixes |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public static function bytesToHuman(int $bytes, bool $binaryPrefix = true): string |
69
|
|
|
{ |
70
|
|
|
if ($binaryPrefix) { |
71
|
|
|
$unit = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); |
72
|
|
|
if ($bytes == 0) { |
73
|
|
|
return '0 ' . $unit[0]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . ($unit[$i] ?? 'B'); |
77
|
|
|
} else { |
78
|
|
|
$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); |
79
|
|
|
if ($bytes == 0) { |
80
|
|
|
return '0 ' . $unit[0]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return @round($bytes / pow(1000, ($i = floor(log($bytes, 1000)))), 2) . ' ' . ($unit[$i] ?? 'B'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Validate insensitive case string |
89
|
|
|
* |
90
|
|
|
* @param string $string The string |
91
|
|
|
* @param string $value The second value to compare with |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public static function insensitiveString(string $string, string $value): bool |
95
|
|
|
{ |
96
|
|
|
return (bool)preg_match_all('/' . $value . '/i', $string); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Millisecond sleep |
101
|
|
|
* |
102
|
|
|
* @param int $milliseconds The milliseconds |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public static function sleep(int $milliseconds): void |
106
|
|
|
{ |
107
|
|
|
usleep($milliseconds * 1000); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get current time in milliseconds |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public static function time(): int |
116
|
|
|
{ |
117
|
|
|
return (int)(microtime(true) * 1000); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Helper to convert a binary to a string of '0' and '1'. |
122
|
|
|
* |
123
|
|
|
* @param string $string |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public static function sprintB(string $string): string |
127
|
|
|
{ |
128
|
|
|
$return = ''; |
129
|
|
|
$strLen = strlen($string); |
130
|
|
|
for ($i = 0; $i < $strLen; $i++) { |
131
|
|
|
$return .= sprintf('%08b', ord($string[$i])); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |