1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stonec0der\ShortenNums; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
* ShortenNums Class |
8
|
|
|
* |
9
|
|
|
* Created 14/01/2020 |
10
|
|
|
* Author: Cedric Megnie N. (Stonec0der). |
11
|
|
|
* Email: [email protected] |
12
|
|
|
* |
13
|
|
|
* This Class function is to shorten some number and return shorter form |
14
|
|
|
* It convert 1000 to 1k, 10000 to 10K, 1050 to 1k and 10500 to 10.5k |
15
|
|
|
* You can shorten from 1000 up to 1000000000000 => 1B (1 billion). |
16
|
|
|
* |
17
|
|
|
* Licence: MIT |
18
|
|
|
*/ |
19
|
|
|
class ShortenNums |
20
|
|
|
{ |
21
|
|
|
//private static $thousand_format; |
22
|
|
|
//private static $million_format; |
23
|
|
|
//private static $billion_format; |
24
|
|
|
//private static $trillion_format; |
25
|
|
|
// TODO: Add method to use $value / 999 for 999-999999 to return 0.9K instead of rounded it to 1K |
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var array Max supported numbers. |
29
|
|
|
*/ |
30
|
|
|
private static $tresholds = [ |
31
|
|
|
'K' => '999999', |
32
|
|
|
'M' => '999999999', |
33
|
|
|
'B' => '999999999999', |
34
|
|
|
'T' => '999999999999000', |
35
|
|
|
]; |
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $formats = [ |
41
|
|
|
'K' => 1000, |
42
|
|
|
'M' => 1000000, |
43
|
|
|
'B' => 1000000000, |
44
|
|
|
'T' => 1000000000000, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Convert long number to readable Numbers 1000 => 1K |
49
|
|
|
* |
50
|
|
|
* @param string $value This should be pass as a string for now. |
51
|
|
|
* @param int $precision Number of number after decimal point. |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public static function formatNumber($value, $precision): string |
55
|
|
|
{ |
56
|
|
|
// $treshold2 = 999; // If you want 0.9k for 999 |
57
|
|
|
$suffix = ''; |
58
|
|
|
$clean_number = ''; |
59
|
|
|
// Check if value is a valid integer and does not start with 0, and force user to pass value as string |
60
|
|
|
self::validateNumber($value); |
61
|
|
|
foreach(self::$tresholds as $suffix => $treshold) { |
62
|
|
|
if ($value < $treshold) { |
63
|
|
|
//if (isset(self::$thousand_format)) |
64
|
|
|
// $clean_number = $value / self::$thousand_format; |
65
|
|
|
$clean_number = $value / self::$formats[$suffix]; |
66
|
|
|
// Round and format number |
67
|
|
|
//die(var_dump($clean_number, $formats[$suffix])); |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
$formated_number = number_format($clean_number,$precision); |
72
|
|
|
|
73
|
|
|
return (preg_match('/\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Convert 1000 => 1K, 10000 => 10K & 1000000 => 100K |
78
|
|
|
* |
79
|
|
|
* @param string $value |
80
|
|
|
* @param int $precision Number of number after decimal point. |
81
|
|
|
* @return string A string number formated as 1K-1.5K |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public static function formatThousand($value, $precision): string |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
//$format = 1000; // If you dont wnat 0.9k but straight 1k for 999 |
86
|
|
|
// $treshold2 = 999; // If you want 0.9k for 999 |
87
|
|
|
$range = [999, 999999]; |
88
|
|
|
$suffix = 'K'; |
89
|
|
|
// Check if value is a valid integer and does not start with 0, and force user to pass value as string |
90
|
|
|
self::validateNumber($value); |
91
|
|
|
self::validateRange($value, $range); |
92
|
|
|
|
93
|
|
|
//if (isset(self::$thousand_format)) |
94
|
|
|
// $clean_number = $value / self::$thousand_format; |
95
|
|
|
$clean_number = $value / self::$format[$suffix]; |
96
|
|
|
// Round and format number |
97
|
|
|
$formated_number = number_format($clean_number,$precision); |
98
|
|
|
|
99
|
|
|
return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Convert 1,000,000 place to 1M |
104
|
|
|
* @param string $value |
105
|
|
|
* @param int $precision Number of number after decimal point. |
106
|
|
|
* @return string A string number formated as 1M-1.5M |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public static function formatMillion($value, $precision): string |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$range = [999999, 999999999]; |
111
|
|
|
$suffix = 'M'; |
112
|
|
|
// Check if value is a valid integer and does not start with 0, and force user to pass value as string |
113
|
|
|
self::validateNumber($value); |
114
|
|
|
self::validateRange($value, $range); |
115
|
|
|
|
116
|
|
|
//if (isset(self::$million_format)) |
117
|
|
|
// $clean_number = $value / self::$million_format; |
118
|
|
|
$clean_number = $value / self::$formats[$suffix]; |
119
|
|
|
// Round and format number |
120
|
|
|
$formated_number = number_format($clean_number,$precision); |
121
|
|
|
|
122
|
|
|
return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert 1000,000,000 place to 1B |
127
|
|
|
* @param string $value |
128
|
|
|
* @param int $precision Number of number after decimal point. |
129
|
|
|
* @return string A string number formated as 1B-1.5B |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public static function formatBillion($value, $precision): string |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$range = [999999999, 999999999999]; |
134
|
|
|
$suffix = 'B'; |
135
|
|
|
// Check if value is a valid integer and does not start with 0, and force user to pass value as string |
136
|
|
|
self::validateNumber($value); |
137
|
|
|
self::validateRange($value, $range); |
138
|
|
|
|
139
|
|
|
//if (isset(self::$billion_format)) |
140
|
|
|
// $clean_number = $value / self::$billion_format; |
141
|
|
|
$clean_number = $value / self::$formats[$suffix]; |
142
|
|
|
// Round and format number |
143
|
|
|
$formated_number = number_format($clean_number,$precision); |
144
|
|
|
|
145
|
|
|
return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Convert 1,000,000,000,000 place to 1T |
150
|
|
|
* @param string $value |
151
|
|
|
* @param int $precision Number of number after decimal point. |
152
|
|
|
* @return string A string number formated as 1B-1.5B |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public static function formatTrillion($value, $precision): string |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$range = [999999999999, 999999999999000]; |
157
|
|
|
$suffix = 'T'; |
158
|
|
|
// Check if value is a valid integer and does not start with 0, and force user to pass value as string |
159
|
|
|
self::validateNumber($value); |
160
|
|
|
self::validateRange($value, $range); |
161
|
|
|
|
162
|
|
|
//if (isset(self::$trillion_format)) |
163
|
|
|
// $clean_number = $value / self::$trillion_format; |
164
|
|
|
$clean_number = $value / self::$formats[$suffix]; |
165
|
|
|
// Round and format number |
166
|
|
|
$formated_number = number_format($clean_number,$precision); |
167
|
|
|
|
168
|
|
|
return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* The Current value is not yet suppoerted, typically it greater than the supported values |
174
|
|
|
* @param string $value the invalid number |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
// private static function notSupported($value): string |
178
|
|
|
// { |
179
|
|
|
// return 'Sorry ' . $value . ' is not Supported!'; |
180
|
|
|
// } |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Validate number |
184
|
|
|
* @param string $value |
185
|
|
|
*/ |
186
|
|
|
private static function validateNumber($value) |
187
|
|
|
{ |
188
|
|
|
// Tis temporary enforce that the pass value should be pass as string of an integer instead non quoted integer example: '1000' is valid, 1000 is not. |
189
|
|
|
if (!is_string($value)) |
190
|
|
|
throw new \Exception("TypeError: $value should be pass as quoted string to avoid unespected result.", 1); |
191
|
|
|
// Accept only integers and string with valid integers. |
192
|
|
|
if (!is_int((int)$value)) |
193
|
|
|
throw new \Exception("TypeError: $value is not a valid integer", 1); |
194
|
|
|
// Should not start with 0. Also check the number length |
195
|
|
|
if (!preg_match('/((?!(0))^[\d]+)$/i', (string)$value)) |
196
|
|
|
throw new \Exception("Error: $value is not valid, value should not start with 0", 1); |
197
|
|
|
// Invalid number TODO: Allow negative numbers |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private static function validateRange($value, $range) |
201
|
|
|
{ |
202
|
|
|
if ((int)$value < $range[0] || (int)$value > $range[1]) |
203
|
|
|
throw new \Exception("Error: The provided value \"$value\" is not in the range of \"$range[0]-$range[1]\"."); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.