|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
final class Digit |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Calculating the factorial of a number. |
|
9
|
|
|
* |
|
10
|
|
|
* @param int $n |
|
11
|
|
|
* |
|
12
|
|
|
* @return int |
|
13
|
|
|
*/ |
|
14
|
2 |
|
public function factorial(int $n = 0): int |
|
15
|
|
|
{ |
|
16
|
2 |
|
if ($n === 0) { |
|
17
|
2 |
|
return 1; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
2 |
|
return $n * $this->factorial($n - 1); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Converts a number into a short version. |
|
25
|
|
|
* eg: 1000 >> 1K. |
|
26
|
|
|
* |
|
27
|
|
|
* @param float $number |
|
28
|
|
|
* @param int $precision |
|
29
|
|
|
* |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function toShort(float $number, int $precision = 1): string |
|
33
|
|
|
{ |
|
34
|
2 |
|
$length = strlen((string) ((int) $number)); |
|
35
|
2 |
|
$length = ceil($length / 3) * 3 + 1; |
|
36
|
|
|
|
|
37
|
2 |
|
$suffix = $this->suffix($length); |
|
|
|
|
|
|
38
|
2 |
|
$value = $this->rounded($number, $length, $precision); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
2 |
|
return $value . $suffix; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create short unique identifier from number. |
|
45
|
|
|
* Actually using in short URL. |
|
46
|
|
|
* |
|
47
|
|
|
* @param int $number |
|
48
|
|
|
* @param string $chars |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function shortKey(int $number, string $chars = 'abcdefghijklmnopqrstuvwxyz'): string |
|
53
|
|
|
{ |
|
54
|
2 |
|
$length = strlen($chars); |
|
55
|
2 |
|
$mod = $number % $length; |
|
56
|
|
|
|
|
57
|
2 |
|
if ($number - $mod === 0) { |
|
58
|
2 |
|
return substr($chars, $number, 1); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
$result = ''; |
|
62
|
|
|
|
|
63
|
2 |
|
while ($mod > 0 || $number > 0) { |
|
64
|
2 |
|
$result = substr($chars, $mod, 1) . $result; |
|
65
|
2 |
|
$number = ($number - $mod) / $length; |
|
66
|
2 |
|
$mod = $number % $length; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Format a number with grouped with divider. |
|
74
|
|
|
* |
|
75
|
|
|
* @param float $number |
|
76
|
|
|
* @param int $length |
|
77
|
|
|
* @param int $precision |
|
78
|
|
|
* |
|
79
|
|
|
* @return float |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function rounded(float $number, int $length = 4, int $precision = 1): float |
|
82
|
|
|
{ |
|
83
|
4 |
|
$divided = (float) bcpow(10, $length - 4, 2); |
|
84
|
|
|
|
|
85
|
4 |
|
return round($number / $divided, $precision); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Converts a numeric value to a string. |
|
90
|
|
|
* |
|
91
|
|
|
* @param float|int $value |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function convertToString(float $value): string |
|
96
|
|
|
{ |
|
97
|
2 |
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
protected function suffix(int $length = 0): string |
|
101
|
|
|
{ |
|
102
|
|
|
$available = [ |
|
103
|
2 |
|
4 => '', |
|
104
|
|
|
7 => 'K', |
|
105
|
|
|
10 => 'M', |
|
106
|
|
|
13 => 'B', |
|
107
|
|
|
16 => 'T+', |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
2 |
|
return $available[$length] ?? end($available); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|