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