1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit; |
6
|
|
|
|
7
|
|
|
use function AlecRabbit\Helpers\bounds; |
8
|
|
|
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION; |
9
|
|
|
|
10
|
|
|
class Pretty |
11
|
|
|
{ |
12
|
|
|
public const DEFAULT_DECIMALS = 2; |
13
|
|
|
public const PERCENT_MAX_DECIMALS = 4; |
14
|
|
|
public const DEC_POINT = '.'; |
15
|
|
|
public const THOUSANDS_SEPARATOR = ','; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Static class. Private Constructor. |
19
|
|
|
*/ |
20
|
|
|
// @codeCoverageIgnoreStart |
21
|
|
|
private function __construct() |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
// @codeCoverageIgnoreEnd |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $number |
28
|
|
|
* @param null|string $unit |
29
|
|
|
* @param int|null $decimals |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
10 |
|
public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string |
33
|
|
|
{ |
34
|
|
|
return |
35
|
10 |
|
format_bytes($number, $unit, $decimals ?? static::DEFAULT_DECIMALS); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param float $value |
40
|
|
|
* @param int|null $units |
41
|
|
|
* @param int|null $precision |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
20 |
|
public static function time(float $value, ?int $units = null, ?int $precision = null): string |
45
|
|
|
{ |
46
|
20 |
|
if (null === $units && null === $precision) { |
47
|
13 |
|
return format_time_auto($value); |
48
|
|
|
} |
49
|
|
|
return |
50
|
7 |
|
format_time($value, $units, $precision ?? DEFAULT_PRECISION); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param float $relative |
55
|
|
|
* @param null|int $decimals |
56
|
|
|
* @param null|string $prefix |
57
|
|
|
* @param null|string $suffix |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
3 |
|
public static function percent( |
61
|
|
|
float $relative, |
62
|
|
|
?int $decimals = null, |
63
|
|
|
?string $prefix = null, |
64
|
|
|
string $suffix = '%' |
65
|
|
|
): string { |
66
|
3 |
|
$prefix = $prefix ?? ''; |
67
|
3 |
|
$suffix = $suffix ?? ''; |
68
|
|
|
$decimals = |
69
|
3 |
|
(int)bounds($decimals ?? static::DEFAULT_DECIMALS, 0, static::PERCENT_MAX_DECIMALS); |
70
|
|
|
return |
71
|
|
|
$prefix . |
72
|
3 |
|
number_format( |
73
|
3 |
|
$relative * 100, $decimals, |
74
|
3 |
|
static::DEC_POINT, |
75
|
3 |
|
static::THOUSANDS_SEPARATOR |
76
|
|
|
) . |
77
|
3 |
|
$suffix; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|