Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| StringUtils | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| generateString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\RnG |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\RnG; |
| 16 | |
| 17 | /** |
| 18 | * String generator. |
| 19 | * |
| 20 | * @package phpOMS\Utils\RnG |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class StringUtils |
| 26 | { |
| 27 | /** |
| 28 | * Get a random string. |
| 29 | * |
| 30 | * @param int $min Min. length |
| 31 | * @param int $max Max. length |
| 32 | * @param string $charset Allowed characters |
| 33 | * |
| 34 | * @return string |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public static function generateString(int $min = 10, int $max = 10, |
| 39 | string $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 40 | ) : string |
| 41 | { |
| 42 | $length = \mt_rand($min, $max); |
| 43 | $charactersLength = \strlen($charset); |
| 44 | $randomString = ''; |
| 45 | |
| 46 | for ($i = 0; $i < $length; ++$i) { |
| 47 | $randomString .= $charset[\mt_rand(0, $charactersLength - 1)]; |
| 48 | } |
| 49 | |
| 50 | return $randomString; |
| 51 | } |
| 52 | } |