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