ArrayHash.php
2 years ago
ArrayList.php
2 years ago
Arrays.php
2 years ago
Callback.php
2 years ago
DateTime.php
2 years ago
FileSystem.php
2 years ago
Floats.php
2 years ago
Helpers.php
2 years ago
Html.php
2 years ago
Image.php
2 years ago
Json.php
2 years ago
ObjectHelpers.php
2 years ago
ObjectMixin.php
2 years ago
Paginator.php
2 years ago
Random.php
2 years ago
Reflection.php
2 years ago
Strings.php
2 years ago
Type.php
2 years ago
Validators.php
2 years ago
exceptions.php
2 years ago
Random.php
39 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Nette Framework (https://nette.org) |
| 5 | * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | */ |
| 7 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Utils; |
| 9 | |
| 10 | use FapiMember\Library\Nette; |
| 11 | /** |
| 12 | * Secure random string generator. |
| 13 | */ |
| 14 | final class Random |
| 15 | { |
| 16 | use Nette\StaticClass; |
| 17 | /** |
| 18 | * Generates a random string of given length from characters specified in second argument. |
| 19 | * Supports intervals, such as `0-9` or `A-Z`. |
| 20 | */ |
| 21 | public static function generate(int $length = 10, string $charlist = '0-9a-z'): string |
| 22 | { |
| 23 | $charlist = count_chars(preg_replace_callback('#.-.#', function (array $m): string { |
| 24 | return implode('', range($m[0][0], $m[0][2])); |
| 25 | }, $charlist), 3); |
| 26 | $chLen = strlen($charlist); |
| 27 | if ($length < 1) { |
| 28 | throw new Nette\InvalidArgumentException('Length must be greater than zero.'); |
| 29 | } elseif ($chLen < 2) { |
| 30 | throw new Nette\InvalidArgumentException('Character list must contain at least two chars.'); |
| 31 | } |
| 32 | $res = ''; |
| 33 | for ($i = 0; $i < $length; $i++) { |
| 34 | $res .= $charlist[random_int(0, $chLen - 1)]; |
| 35 | } |
| 36 | return $res; |
| 37 | } |
| 38 | } |
| 39 |