Satheez /
php-helper-functions
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Helper class that provides useful php functions. |
||||
| 4 | * |
||||
| 5 | * @author Vettivel Satheez <[email protected]> |
||||
| 6 | * |
||||
| 7 | * @link https://github.com/satheez |
||||
| 8 | * |
||||
| 9 | * @license MIT |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | namespace Sa\Helper; |
||||
| 13 | |||||
| 14 | class Generate |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * Generate random int. |
||||
| 18 | * |
||||
| 19 | * @param int $minValue |
||||
| 20 | * @param int $maxValue |
||||
| 21 | * |
||||
| 22 | * @return int |
||||
| 23 | */ |
||||
| 24 | public static function randomInt(int $minValue = 0, int $maxValue = 1000): int |
||||
| 25 | { |
||||
| 26 | return rand($minValue, $maxValue); |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Generate random float. |
||||
| 31 | * |
||||
| 32 | * @param float $minValue |
||||
| 33 | * @param float $maxValue |
||||
| 34 | * @param int $decimalPlace |
||||
| 35 | * |
||||
| 36 | * @return float |
||||
| 37 | */ |
||||
| 38 | public static function randomFloat(float $minValue = 0.0, float $maxValue = 1000.0, int $decimalPlace = 1): float |
||||
| 39 | { |
||||
| 40 | |||||
| 41 | // Decimal place can be from 1 to 5 |
||||
| 42 | $decimalPlace = ($decimalPlace > 0 && $decimalPlace <= 5) ? $decimalPlace : 1; |
||||
| 43 | |||||
| 44 | $divider = (int) pow(10, $decimalPlace); |
||||
| 45 | |||||
| 46 | return rand(($minValue * $divider), ($maxValue * $divider)) / $divider; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
$maxValue * $divider of type double is incompatible with the type integer expected by parameter $max of rand().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 47 | } |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * Generate random string. |
||||
| 51 | * |
||||
| 52 | * @param int $length |
||||
| 53 | * @param bool $includeNumbers |
||||
| 54 | * @param bool $includeSymbols |
||||
| 55 | * |
||||
| 56 | * @return string |
||||
| 57 | */ |
||||
| 58 | public static function randomString(int $length = 10, bool $includeNumbers = true, bool $includeSymbols = false): string |
||||
| 59 | { |
||||
| 60 | $range = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
| 61 | |||||
| 62 | // Include numbers |
||||
| 63 | if ($includeNumbers) { |
||||
| 64 | $range .= '0123456789'; |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | // Include symbols |
||||
| 68 | if ($includeSymbols) { |
||||
| 69 | $range .= '_!@#$^~'; |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | $finalString = ''; |
||||
| 73 | |||||
| 74 | $srtLen = strlen($range); |
||||
| 75 | |||||
| 76 | for ($i = 0; $i < $length; $i++) { |
||||
| 77 | $randomIndex = rand(0, $srtLen - 1); |
||||
| 78 | |||||
| 79 | $finalString .= $range[$randomIndex]; |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | return $finalString; |
||||
| 83 | } |
||||
| 84 | } |
||||
| 85 |