1 | <?php |
||||
2 | /* |
||||
3 | * This file is part of the "andrey-helldar/support" project. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Andrey Helldar <[email protected]> |
||||
9 | * |
||||
10 | * @copyright 2021 Andrey Helldar |
||||
11 | * |
||||
12 | * @license MIT |
||||
13 | * |
||||
14 | * @see https://github.com/andrey-helldar/support |
||||
15 | */ |
||||
16 | |||||
17 | namespace Helldar\Support\Helpers; |
||||
18 | |||||
19 | class Digit |
||||
20 | { |
||||
21 | /** |
||||
22 | * Calculating the factorial of a number. |
||||
23 | * |
||||
24 | * @param int $n |
||||
25 | * |
||||
26 | * @return int |
||||
27 | */ |
||||
28 | 2 | public function factorial(int $n = 0): int |
|||
29 | { |
||||
30 | 2 | if ($n === 0) { |
|||
31 | 2 | return 1; |
|||
32 | } |
||||
33 | |||||
34 | 2 | return $n * $this->factorial($n - 1); |
|||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * Converts a number into a short version. |
||||
39 | * eg: 1000 >> 1K. |
||||
40 | * |
||||
41 | * @param float $number |
||||
42 | * @param int $precision |
||||
43 | * |
||||
44 | * @return string |
||||
45 | */ |
||||
46 | 2 | public function toShort(float $number, int $precision = 1): string |
|||
47 | { |
||||
48 | 2 | $length = strlen((string) ((int) $number)); |
|||
49 | 2 | $length = ceil($length / 3) * 3 + 1; |
|||
50 | |||||
51 | 2 | $suffix = $this->suffix($length); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
52 | 2 | $value = $this->rounded($number, $length, $precision); |
|||
0 ignored issues
–
show
$length of type double is incompatible with the type integer expected by parameter $length of Helldar\Support\Helpers\Digit::rounded() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
53 | |||||
54 | 2 | return $value . $suffix; |
|||
55 | } |
||||
56 | |||||
57 | /** |
||||
58 | * Create short unique identifier from number. |
||||
59 | * Actually using in short URL. |
||||
60 | * |
||||
61 | * @param int $number |
||||
62 | * @param string $chars |
||||
63 | * |
||||
64 | * @return string |
||||
65 | */ |
||||
66 | 2 | public function shortKey(int $number, string $chars = 'abcdefghijklmnopqrstuvwxyz'): string |
|||
67 | { |
||||
68 | 2 | $length = strlen($chars); |
|||
69 | 2 | $mod = $number % $length; |
|||
70 | |||||
71 | 2 | if ($number - $mod === 0) { |
|||
72 | 2 | return substr($chars, $number, 1); |
|||
73 | } |
||||
74 | |||||
75 | 2 | $result = ''; |
|||
76 | |||||
77 | 2 | while ($mod > 0 || $number > 0) { |
|||
78 | 2 | $result = substr($chars, $mod, 1) . $result; |
|||
79 | 2 | $number = ($number - $mod) / $length; |
|||
80 | 2 | $mod = $number % $length; |
|||
81 | } |
||||
82 | |||||
83 | 2 | return $result; |
|||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * Format a number with grouped with divider. |
||||
88 | * |
||||
89 | * @param float $number |
||||
90 | * @param int $length |
||||
91 | * @param int $precision |
||||
92 | * |
||||
93 | * @return float |
||||
94 | */ |
||||
95 | 4 | public function rounded(float $number, int $length = 4, int $precision = 1): float |
|||
96 | { |
||||
97 | 4 | $divided = (float) bcpow(10, $length - 4, 2); |
|||
98 | |||||
99 | 4 | return round($number / $divided, $precision); |
|||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * Converts a numeric value to a string. |
||||
104 | * |
||||
105 | * @param float|int $value |
||||
106 | * |
||||
107 | * @return string |
||||
108 | */ |
||||
109 | 2 | public function convertToString(float $value): string |
|||
110 | { |
||||
111 | 2 | return $value; |
|||
112 | } |
||||
113 | |||||
114 | 2 | protected function suffix(int $length = 0): string |
|||
115 | { |
||||
116 | $available = [ |
||||
117 | 2 | 4 => '', |
|||
118 | 7 => 'K', |
||||
119 | 10 => 'M', |
||||
120 | 13 => 'B', |
||||
121 | 16 => 'T+', |
||||
122 | ]; |
||||
123 | |||||
124 | 2 | return $available[$length] ?? end($available); |
|||
125 | } |
||||
126 | } |
||||
127 |