1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit; |
6
|
|
|
|
7
|
|
|
use function AlecRabbit\Helpers\bounds; |
8
|
|
|
use function AlecRabbit\Helpers\is_negative; |
9
|
|
|
use const AlecRabbit\Helpers\Constants\BRACKETS_ANGLE; |
10
|
|
|
use const AlecRabbit\Helpers\Constants\BRACKETS_CURLY; |
11
|
|
|
use const AlecRabbit\Helpers\Constants\BRACKETS_PARENTHESES; |
12
|
|
|
use const AlecRabbit\Helpers\Constants\BRACKETS_SQUARE; |
13
|
|
|
use const AlecRabbit\Helpers\Constants\BRACKETS_SUPPORTED; |
14
|
|
|
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION; |
15
|
|
|
use const AlecRabbit\Helpers\Constants\UNIT_HOURS; |
16
|
|
|
use const AlecRabbit\Helpers\Constants\UNIT_MILLISECONDS; |
17
|
|
|
use const AlecRabbit\Helpers\Constants\UNIT_MINUTES; |
18
|
|
|
use const AlecRabbit\Helpers\Constants\UNIT_SECONDS; |
19
|
|
|
use const AlecRabbit\Helpers\Constants\UNITS; |
20
|
|
|
use const AlecRabbit\Helpers\Strings\Constants\BYTES_UNITS; |
21
|
|
|
use const AlecRabbit\Helpers\Strings\Constants\TIME_COEFFICIENTS; |
22
|
|
|
use const AlecRabbit\Helpers\Strings\Constants\TIME_UNITS; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $text |
26
|
|
|
* @param string|null $tag |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
function tag(string $text, string $tag = null): string |
30
|
|
|
{ |
31
|
|
|
return |
32
|
1 |
|
$tag ? "<{$tag}>$text</{$tag}>" : $text; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $text |
37
|
|
|
* @param int $brackets |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
function brackets(string $text, int $brackets = BRACKETS_SQUARE): string |
41
|
|
|
{ |
42
|
3 |
|
if (!\in_array($brackets, BRACKETS_SUPPORTED, true)) { |
43
|
1 |
|
throw new \InvalidArgumentException( |
44
|
1 |
|
'Parameter 2 should be BRACKETS_SQUARE | BRACKETS_CURLY | BRACKETS_PARENTHESES | BRACKETS_ANGLE' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
switch ($brackets) { |
48
|
2 |
|
case BRACKETS_CURLY: |
49
|
1 |
|
$text = "{{$text}}"; |
50
|
1 |
|
break; |
51
|
2 |
|
case BRACKETS_SQUARE: |
52
|
2 |
|
$text = "[{$text}]"; |
53
|
2 |
|
break; |
54
|
1 |
|
case BRACKETS_PARENTHESES: |
55
|
1 |
|
$text = "({$text})"; |
56
|
1 |
|
break; |
57
|
1 |
|
case BRACKETS_ANGLE: |
58
|
1 |
|
$text = "⟨{$text}⟩"; |
59
|
1 |
|
break; |
60
|
|
|
} |
61
|
2 |
|
return $text; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $text |
66
|
|
|
* @param null|string $open |
67
|
|
|
* @param null|string $close |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
function str_decorate(string $text, ?string $open = null, ?string $close = null): string |
71
|
|
|
{ |
72
|
1 |
|
if (null !== $open && null === $close) { |
73
|
1 |
|
$close = $open; |
74
|
|
|
} |
75
|
1 |
|
return "{$open}{$text}{$close}"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
function format_bytes(int $bytes, ?string $unit = null, int $decimals = 2): string |
80
|
|
|
{ |
81
|
37 |
|
$negative = is_negative($bytes); |
82
|
|
|
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
83
|
37 |
|
$bytes = \abs($bytes); |
84
|
37 |
|
$value = 0; |
85
|
37 |
|
$unit = \strtoupper($unit ?? ''); |
86
|
37 |
|
if ($bytes > 0) { |
87
|
|
|
// Generate automatic prefix by bytes |
88
|
|
|
// If wrong prefix given |
89
|
36 |
|
if (!\array_key_exists($unit, BYTES_UNITS)) { |
90
|
12 |
|
$pow = (int)\floor(\log($bytes) / \log(1024)); |
91
|
12 |
|
$unit = (string)\array_search($pow, BYTES_UNITS, true); |
92
|
|
|
} |
93
|
|
|
// Calculate byte value by prefix |
94
|
36 |
|
$value = ($bytes / 1024 ** \floor(BYTES_UNITS[$unit])); |
95
|
|
|
} else { |
96
|
1 |
|
$unit = 'B'; |
97
|
|
|
} |
98
|
37 |
|
if ($unit === 'B') { |
99
|
6 |
|
$decimals = 0; |
100
|
|
|
} |
101
|
37 |
|
$decimals = (int)bounds($decimals, 0, 24); |
102
|
|
|
|
103
|
|
|
// Format output |
104
|
|
|
return |
105
|
37 |
|
sprintf('%s%.' . $decimals . 'f' . $unit, $negative ? '-' : '', $value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
function format_time(?float $value, ?int $units = null, int $precision = DEFAULT_PRECISION): string |
109
|
|
|
{ |
110
|
16 |
|
$units = $units ?? UNIT_MILLISECONDS; |
111
|
16 |
|
$precision = (int)bounds($precision, 0, 6); |
112
|
16 |
|
$value = $value ?? 0.0; |
113
|
|
|
return |
114
|
16 |
|
sprintf( |
115
|
16 |
|
'%s%s', |
116
|
16 |
|
round($value * TIME_COEFFICIENTS[$units], $precision), |
117
|
16 |
|
TIME_UNITS[$units] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function format_time_auto(float $value): string |
122
|
|
|
{ |
123
|
20 |
|
if ($value > 10000) { |
124
|
2 |
|
return format_time($value, UNIT_HOURS, 3); |
125
|
|
|
} |
126
|
18 |
|
if ($value > 1000) { |
127
|
1 |
|
return format_time($value, UNIT_MINUTES, 2); |
128
|
|
|
} |
129
|
17 |
|
if ($value > 100) { |
130
|
1 |
|
return format_time($value, UNIT_SECONDS, 0); |
131
|
|
|
} |
132
|
|
|
$pow = |
133
|
16 |
|
(int)bounds( |
134
|
16 |
|
\abs( |
135
|
16 |
|
\floor( |
136
|
16 |
|
\log($value) / \log(1000) |
137
|
|
|
) |
138
|
|
|
), |
139
|
16 |
|
0, |
140
|
16 |
|
3 |
141
|
|
|
); |
142
|
16 |
|
return \round($value * 1000 ** $pow, 1) . TIME_UNITS[UNITS[$pow]]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
function format_time_ns(int $value): string |
146
|
|
|
{ |
147
|
9 |
|
return format_time_auto($value / 1000000000); |
148
|
|
|
} |
149
|
|
|
|