1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\Util; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Utils |
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 Utils |
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) return '0 ' . $unit[0]; |
73
|
|
|
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . ($unit[$i] ?? 'B'); |
74
|
|
|
} else { |
75
|
|
|
$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); |
76
|
|
|
if ($bytes == 0) return '0 ' . $unit[0]; |
77
|
|
|
return @round($bytes / pow(1000, ($i = floor(log($bytes, 1000)))), 2) . ' ' . ($unit[$i] ?? 'B'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The microtime as float |
83
|
|
|
* |
84
|
|
|
* @return float |
85
|
|
|
*/ |
86
|
|
|
public static function getMicroTime(): float |
87
|
|
|
{ |
88
|
|
|
return microtime(true); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
} |