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 DECIMAL_POINT = '.'; |
15
|
|
|
public const THOUSANDS_SEPARATOR = ''; |
16
|
|
|
public const DEFAULT_PRECISION = DEFAULT_PRECISION; |
17
|
|
|
|
18
|
|
|
/** @var null|string */ |
19
|
|
|
private static $decimalPoint; |
20
|
|
|
/** @var null|string */ |
21
|
|
|
private static $thousandsSeparator; |
22
|
|
|
/** @var null|int */ |
23
|
|
|
private static $percentMaxDecimals; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Static class. Private Constructor. |
27
|
|
|
*/ |
28
|
|
|
// @codeCoverageIgnoreStart |
29
|
|
|
private function __construct() |
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
// @codeCoverageIgnoreEnd |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param int $number |
36
|
|
|
* @param null|string $unit |
37
|
|
|
* @param int|null $decimals |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
11 |
|
public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string |
41
|
|
|
{ |
42
|
|
|
return |
43
|
11 |
|
format_bytes($number, $unit, $decimals ?? static::DEFAULT_DECIMALS); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param float $value |
48
|
|
|
* @param int|null $units |
49
|
|
|
* @param int|null $precision |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
20 |
|
public static function time(float $value, ?int $units = null, ?int $precision = null): string |
53
|
|
|
{ |
54
|
20 |
|
if (null === $units && null === $precision) { |
55
|
13 |
|
return format_time_auto($value); |
56
|
|
|
} |
57
|
|
|
return |
58
|
7 |
|
format_time($value, $units, $precision ?? static::DEFAULT_PRECISION); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param float $fraction |
63
|
|
|
* @param null|int $decimals |
64
|
|
|
* @param null|string $prefix |
65
|
|
|
* @param string $suffix |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
28 |
|
public static function percent( |
69
|
|
|
float $fraction, |
70
|
|
|
?int $decimals = null, |
71
|
|
|
?string $prefix = null, |
72
|
|
|
string $suffix = '%' |
73
|
|
|
): string { |
74
|
|
|
$decimals = |
75
|
28 |
|
(int)bounds( |
76
|
28 |
|
$decimals ?? static::DEFAULT_DECIMALS, |
77
|
28 |
|
0, |
78
|
28 |
|
static::$percentMaxDecimals ?? static::PERCENT_MAX_DECIMALS |
|
|
|
|
79
|
|
|
); |
80
|
|
|
return |
81
|
28 |
|
($prefix ?? '') . |
82
|
28 |
|
number_format( |
83
|
28 |
|
$fraction * 100, |
84
|
28 |
|
$decimals, |
85
|
28 |
|
static::$decimalPoint ?? static::DECIMAL_POINT, |
|
|
|
|
86
|
28 |
|
static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR |
|
|
|
|
87
|
|
|
) . |
88
|
28 |
|
$suffix; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $decimalPoint |
93
|
|
|
*/ |
94
|
14 |
|
public static function setDecimalPoint(string $decimalPoint): void |
95
|
|
|
{ |
96
|
14 |
|
self::$decimalPoint = $decimalPoint; |
97
|
14 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $thousandsSeparator |
101
|
|
|
*/ |
102
|
14 |
|
public static function setThousandsSeparator(string $thousandsSeparator): void |
103
|
|
|
{ |
104
|
14 |
|
self::$thousandsSeparator = $thousandsSeparator; |
105
|
14 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $percentMaxDecimals |
109
|
|
|
*/ |
110
|
7 |
|
public static function setPercentMaxDecimals(int $percentMaxDecimals): void |
111
|
|
|
{ |
112
|
7 |
|
self::$percentMaxDecimals = abs($percentMaxDecimals); |
113
|
7 |
|
} |
114
|
|
|
|
115
|
21 |
|
public static function resetDecimalPoint(): void |
116
|
|
|
{ |
117
|
21 |
|
self::$decimalPoint = null; |
118
|
21 |
|
} |
119
|
|
|
|
120
|
21 |
|
public static function resetThousandsSeparator(): void |
121
|
|
|
{ |
122
|
21 |
|
self::$thousandsSeparator = null; |
123
|
21 |
|
} |
124
|
|
|
|
125
|
7 |
|
public static function resetPercentMaxDecimals(): void |
126
|
|
|
{ |
127
|
7 |
|
self::$percentMaxDecimals = null; |
128
|
7 |
|
} |
129
|
|
|
} |
130
|
|
|
|