1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Accessories; |
6
|
|
|
|
7
|
|
|
use function AlecRabbit\format_bytes; |
8
|
|
|
use function AlecRabbit\format_time; |
9
|
|
|
use function AlecRabbit\format_time_auto; |
10
|
|
|
use function AlecRabbit\Helpers\bounds; |
11
|
|
|
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION; |
12
|
|
|
|
13
|
|
|
class Pretty |
14
|
|
|
{ |
15
|
|
|
public const DEFAULT_DECIMALS = 2; |
16
|
|
|
public const MAX_DECIMALS = 6; |
17
|
|
|
public const DECIMAL_POINT = '.'; |
18
|
|
|
public const THOUSANDS_SEPARATOR = ''; |
19
|
|
|
public const DEFAULT_PRECISION = DEFAULT_PRECISION; |
20
|
|
|
|
21
|
|
|
/** @var null|string */ |
22
|
|
|
protected static $decimalPoint; |
23
|
|
|
/** @var null|string */ |
24
|
|
|
protected static $thousandsSeparator; |
25
|
|
|
/** @var null|int */ |
26
|
|
|
protected static $maxDecimals; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Static class. Private Constructor. |
30
|
|
|
*/ |
31
|
|
|
// @codeCoverageIgnoreStart |
32
|
|
|
protected function __construct() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
// @codeCoverageIgnoreEnd |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $number |
39
|
|
|
* @param null|string $unit |
40
|
|
|
* @param int|null $decimals |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
16 |
|
public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string |
44
|
|
|
{ |
45
|
|
|
return |
46
|
16 |
|
format_bytes($number, $unit, static::refineDecimals($decimals)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param null|int $decimals |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
75 |
|
public static function refineDecimals(?int $decimals): int |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
return |
57
|
75 |
|
(int)bounds( |
58
|
75 |
|
$decimals ?? static::DEFAULT_DECIMALS, |
59
|
75 |
|
0, |
60
|
75 |
|
static::$maxDecimals ?? static::MAX_DECIMALS |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param float $seconds |
66
|
|
|
* @param null|int $units |
67
|
|
|
* @param null|int $decimals |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
21 |
|
public static function seconds(float $seconds, ?int $units = null, ?int $decimals = null): string |
71
|
|
|
{ |
72
|
|
|
return |
73
|
21 |
|
static::time($seconds, $units, $decimals); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param float $seconds |
78
|
|
|
* @param int|null $units |
79
|
|
|
* @param int|null $decimals |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
102 |
|
public static function time(float $seconds, ?int $units = null, ?int $decimals = null): string |
83
|
|
|
{ |
84
|
102 |
|
if (null === $units && null === $decimals) { |
85
|
71 |
|
return format_time_auto($seconds); |
86
|
|
|
} |
87
|
|
|
return |
88
|
31 |
|
format_time( |
89
|
31 |
|
$seconds, |
90
|
31 |
|
$units, |
91
|
31 |
|
static::refineDecimals($decimals), |
92
|
31 |
|
static::$decimalPoint ?? static::DECIMAL_POINT, |
93
|
31 |
|
static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param float $milliseconds |
99
|
|
|
* @param null|int $units |
100
|
|
|
* @param null|int $decimals |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
6 |
|
public static function milliseconds(float $milliseconds, ?int $units = null, ?int $decimals = null): string |
104
|
|
|
{ |
105
|
|
|
return |
106
|
6 |
|
static::time($milliseconds / 1000, $units, $decimals); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param float $microseconds |
111
|
|
|
* @param null|int $units |
112
|
|
|
* @param null|int $decimals |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
20 |
|
public static function microseconds(float $microseconds, ?int $units = null, ?int $decimals = null): string |
116
|
|
|
{ |
117
|
|
|
return |
118
|
20 |
|
static::useconds($microseconds, $units, $decimals); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param float $useconds |
123
|
|
|
* @param null|int $units |
124
|
|
|
* @param null|int $decimals |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
40 |
|
public static function useconds(float $useconds, ?int $units = null, ?int $decimals = null): string |
128
|
|
|
{ |
129
|
|
|
return |
130
|
40 |
|
static::time($useconds / 1000000, $units, $decimals); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param float $nanoseconds |
135
|
|
|
* @param null|int $units |
136
|
|
|
* @param null|int $decimals |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
14 |
|
public static function nanoseconds(float $nanoseconds, ?int $units = null, ?int $decimals = null): string |
140
|
|
|
{ |
141
|
|
|
return |
142
|
14 |
|
static::time($nanoseconds / 1000000000, $units, $decimals); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param float $fraction |
147
|
|
|
* @param null|int $decimals |
148
|
|
|
* @param null|string $prefix |
149
|
|
|
* @param string $suffix |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
28 |
|
public static function percent( |
153
|
|
|
float $fraction, |
154
|
|
|
?int $decimals = null, |
155
|
|
|
?string $prefix = null, |
156
|
|
|
string $suffix = '%' |
157
|
|
|
): string { |
158
|
|
|
return |
159
|
28 |
|
($prefix ?? '') . |
160
|
28 |
|
number_format( |
161
|
28 |
|
$fraction * 100, |
162
|
28 |
|
static::refineDecimals($decimals), |
163
|
28 |
|
static::$decimalPoint ?? static::DECIMAL_POINT, |
164
|
28 |
|
static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR |
165
|
|
|
) . |
166
|
28 |
|
$suffix; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $decimalPoint |
171
|
|
|
*/ |
172
|
14 |
|
public static function setDecimalPoint(string $decimalPoint): void |
173
|
|
|
{ |
174
|
14 |
|
self::$decimalPoint = $decimalPoint; |
175
|
14 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $thousandsSeparator |
179
|
|
|
*/ |
180
|
14 |
|
public static function setThousandsSeparator(string $thousandsSeparator): void |
181
|
|
|
{ |
182
|
14 |
|
self::$thousandsSeparator = $thousandsSeparator; |
183
|
14 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param int $maxDecimals |
187
|
|
|
*/ |
188
|
7 |
|
public static function setMaxDecimals(int $maxDecimals): void |
189
|
|
|
{ |
190
|
7 |
|
self::$maxDecimals = abs($maxDecimals); |
191
|
7 |
|
} |
192
|
|
|
|
193
|
21 |
|
public static function resetDecimalPoint(): void |
194
|
|
|
{ |
195
|
21 |
|
self::$decimalPoint = null; |
196
|
21 |
|
} |
197
|
|
|
|
198
|
21 |
|
public static function resetThousandsSeparator(): void |
199
|
|
|
{ |
200
|
21 |
|
self::$thousandsSeparator = null; |
201
|
21 |
|
} |
202
|
|
|
|
203
|
7 |
|
public static function resetMaxDecimals(): void |
204
|
|
|
{ |
205
|
7 |
|
self::$maxDecimals = null; |
206
|
7 |
|
} |
207
|
|
|
} |
208
|
|
|
|