1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionException; |
8
|
|
|
|
9
|
|
|
class LaravelHelpers |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Return the alphabet in array form. |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public static function alphabet(): array |
17
|
|
|
{ |
18
|
|
|
return Cache::rememberForever('alphabet', function () { |
19
|
|
|
return range('A', 'Z'); |
20
|
|
|
}); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return the index of a letter in the alphabet. |
25
|
|
|
* |
26
|
|
|
* @param int $index |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public static function alphabetIndex(int $index): string |
30
|
|
|
{ |
31
|
|
|
return self::alphabet()[$index]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Retrieve a class's short name (without namespace). |
36
|
|
|
* |
37
|
|
|
* @param $class |
38
|
|
|
* @param bool $short Full name or short name |
39
|
|
|
* @param string|null $default |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public static function getClassName($class, $short = false, $default = null): string |
43
|
|
|
{ |
44
|
|
|
// Attempt to resolve the $class's name |
45
|
|
|
try { |
46
|
|
|
return (new ReflectionClass($class))->{$short ? 'getShortName' : 'getName'}(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Return $default |
50
|
|
|
catch (ReflectionException $e) { |
51
|
|
|
return $default; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Serialize and simple hash a value to create a unique ID. |
57
|
|
|
* |
58
|
|
|
* @param $value |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public static function serializeHash($value): int |
62
|
|
|
{ |
63
|
|
|
return crc32(serialize($value)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrieve a random float between two values with a specified number of decimals. |
68
|
|
|
* |
69
|
|
|
* @param $min |
70
|
|
|
* @param $max |
71
|
|
|
* @param int $decimals |
72
|
|
|
* @return float |
73
|
|
|
*/ |
74
|
|
|
public static function randomFloat(int $min, int $max, int $decimals = 2): float |
75
|
|
|
{ |
76
|
|
|
$decimal = str_pad('1', $decimals + 1, '0'); |
77
|
|
|
|
78
|
|
|
return rand($min, $max) + (rand(1, $decimal) / $decimal); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Determine if a string is a Binary String. |
83
|
|
|
* |
84
|
|
|
* @param string $string |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public static function isBinary(string $string): bool |
88
|
|
|
{ |
89
|
|
|
return preg_match('~[^\x20-\x7E\t\r\n]~', $string) > 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine if a string is serialized. |
94
|
|
|
* |
95
|
|
|
* https://stackoverflow.com/questions/1369936/check-to-see-if-a-string-is-serialized/4994628 |
96
|
|
|
* |
97
|
|
|
* @param $data |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public static function isSerialized($data): bool |
101
|
|
|
{ |
102
|
|
|
// if it isn't a string, it isn't serialized |
103
|
|
|
if (! is_string($data)) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
$data = trim($data); |
107
|
|
|
if ('N;' == $data) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
if (! preg_match('/^([adObis]):/', $data, $badions)) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
switch ($badions[1]) { |
114
|
|
|
case 'a': |
115
|
|
|
case 'O': |
116
|
|
|
case 's': |
117
|
|
|
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
|
case 'b': |
122
|
|
|
case 'i': |
123
|
|
|
case 'd': |
124
|
|
|
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Shorten a number by adding alphanumeric notation denoting thousands, millions, billing or trillions. |
135
|
|
|
* |
136
|
|
|
* - example: AppInfo::formatNumber(10000) -> '10k' |
137
|
|
|
* - https://stackoverflow.com/questions/4116499/php-count-round-thousand-to-a-k-style-count-like-facebook-share-twitter-bu/36365553 |
138
|
|
|
* |
139
|
|
|
* @param int $number |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public static function formatNumber(int $number): string |
143
|
|
|
{ |
144
|
|
|
if ($number > 1000) { |
145
|
|
|
$x = round($number); |
146
|
|
|
$x_number_format = number_format($x); |
147
|
|
|
$x_array = explode(',', $x_number_format); |
148
|
|
|
$x_parts = ['k', 'm', 'b', 't']; |
149
|
|
|
$x_count_parts = count($x_array) - 1; |
150
|
|
|
$x_display = $x_array[0].((int) $x_array[1][0] !== 0 ? '.'.$x_array[1][0] : ''); |
151
|
|
|
$x_display .= $x_parts[$x_count_parts - 1]; |
152
|
|
|
|
153
|
|
|
return $x_display; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $number; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|