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 MAX_DECIMALS = 6; |
14
|
|
|
public const DECIMAL_POINT = '.'; |
15
|
|
|
public const THOUSANDS_SEPARATOR = ''; |
16
|
|
|
public const DEFAULT_PRECISION = DEFAULT_PRECISION; |
17
|
|
|
|
18
|
|
|
/** @var null|string */ |
19
|
|
|
protected static $decimalPoint; |
20
|
|
|
/** @var null|string */ |
21
|
|
|
protected static $thousandsSeparator; |
22
|
|
|
/** @var null|int */ |
23
|
|
|
protected static $maxDecimals; |
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, static::refineDecimals($decimals)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param null|int $decimals |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
47 |
|
public static function refineDecimals(?int $decimals): int |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
return |
54
|
47 |
|
(int)bounds( |
55
|
47 |
|
$decimals ?? static::DEFAULT_DECIMALS, |
56
|
47 |
|
0, |
57
|
47 |
|
static::$maxDecimals ?? static::MAX_DECIMALS |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param float $value |
63
|
|
|
* @param int|null $units |
64
|
|
|
* @param int|null $decimals |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
21 |
|
public static function time(float $value, ?int $units = null, ?int $decimals = null): string |
68
|
|
|
{ |
69
|
21 |
|
if (null === $units && null === $decimals) { |
70
|
13 |
|
return format_time_auto($value); |
71
|
|
|
} |
72
|
|
|
return |
73
|
8 |
|
format_time( |
74
|
8 |
|
$value, |
75
|
8 |
|
$units, |
76
|
8 |
|
static::refineDecimals($decimals), |
77
|
8 |
|
static::$decimalPoint ?? static::DECIMAL_POINT, |
78
|
8 |
|
static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param float $fraction |
84
|
|
|
* @param null|int $decimals |
85
|
|
|
* @param null|string $prefix |
86
|
|
|
* @param string $suffix |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
28 |
|
public static function percent( |
90
|
|
|
float $fraction, |
91
|
|
|
?int $decimals = null, |
92
|
|
|
?string $prefix = null, |
93
|
|
|
string $suffix = '%' |
94
|
|
|
): string { |
95
|
|
|
return |
96
|
28 |
|
($prefix ?? '') . |
97
|
28 |
|
number_format( |
98
|
28 |
|
$fraction * 100, |
99
|
28 |
|
static::refineDecimals($decimals), |
100
|
28 |
|
static::$decimalPoint ?? static::DECIMAL_POINT, |
101
|
28 |
|
static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR |
102
|
|
|
) . |
103
|
28 |
|
$suffix; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $decimalPoint |
108
|
|
|
*/ |
109
|
14 |
|
public static function setDecimalPoint(string $decimalPoint): void |
110
|
|
|
{ |
111
|
14 |
|
self::$decimalPoint = $decimalPoint; |
112
|
14 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $thousandsSeparator |
116
|
|
|
*/ |
117
|
14 |
|
public static function setThousandsSeparator(string $thousandsSeparator): void |
118
|
|
|
{ |
119
|
14 |
|
self::$thousandsSeparator = $thousandsSeparator; |
120
|
14 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int $maxDecimals |
124
|
|
|
*/ |
125
|
7 |
|
public static function setMaxDecimals(int $maxDecimals): void |
126
|
|
|
{ |
127
|
7 |
|
self::$maxDecimals = abs($maxDecimals); |
128
|
7 |
|
} |
129
|
|
|
|
130
|
21 |
|
public static function resetDecimalPoint(): void |
131
|
|
|
{ |
132
|
21 |
|
self::$decimalPoint = null; |
133
|
21 |
|
} |
134
|
|
|
|
135
|
21 |
|
public static function resetThousandsSeparator(): void |
136
|
|
|
{ |
137
|
21 |
|
self::$thousandsSeparator = null; |
138
|
21 |
|
} |
139
|
|
|
|
140
|
7 |
|
public static function resetMaxDecimals(): void |
141
|
|
|
{ |
142
|
7 |
|
self::$maxDecimals = null; |
143
|
7 |
|
} |
144
|
|
|
} |
145
|
|
|
|