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
|
|
|
* Determine if the Application is running in a 'production' environment. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public static function isProductionEnvironment(): bool |
61
|
|
|
{ |
62
|
|
|
return env('APP_ENV') == 'production'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Determine if the Application is running in a 'development' environment. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public static function isDevelopmentEnvironment(): bool |
71
|
|
|
{ |
72
|
|
|
return env('APP_ENV') == 'development'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Serialize and simple hash a value to create a unique ID. |
77
|
|
|
* |
78
|
|
|
* @param $value |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public static function serializeHash($value): int |
82
|
|
|
{ |
83
|
|
|
return crc32(serialize($value)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve a random float between two values with a specified number of decimals. |
88
|
|
|
* |
89
|
|
|
* @param $min |
90
|
|
|
* @param $max |
91
|
|
|
* @param int $decimals |
92
|
|
|
* @return float |
93
|
|
|
*/ |
94
|
|
|
public static function randomFloat(int $min, int $max, int $decimals = 2): float |
95
|
|
|
{ |
96
|
|
|
$decimal = str_pad('1', $decimals + 1, '0'); |
97
|
|
|
|
98
|
|
|
return rand($min, $max) + (rand(1, $decimal) / $decimal); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if a string is a Binary String |
103
|
|
|
* |
104
|
|
|
* @param string $string |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public static function isBinary(string $string): bool |
108
|
|
|
{ |
109
|
|
|
return preg_match('~[^\x20-\x7E\t\r\n]~', $string) > 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Determine if a string is serialized. |
114
|
|
|
* |
115
|
|
|
* https://stackoverflow.com/questions/1369936/check-to-see-if-a-string-is-serialized/4994628 |
116
|
|
|
* |
117
|
|
|
* @param $data |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public static function isSerialized($data): bool |
121
|
|
|
{ |
122
|
|
|
// if it isn't a string, it isn't serialized |
123
|
|
|
if (! is_string($data)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
$data = trim($data); |
127
|
|
|
if ('N;' == $data) { |
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
if (! preg_match('/^([adObis]):/', $data, $badions)) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
switch ($badions[1]) { |
134
|
|
|
case 'a': |
135
|
|
|
case 'O': |
136
|
|
|
case 's': |
137
|
|
|
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
break; |
141
|
|
|
case 'b': |
142
|
|
|
case 'i': |
143
|
|
|
case 'd': |
144
|
|
|
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) { |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|