PluginProbe
Age Gate / 3.7.3
Age Gate v3.7.3
3.7.3 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 113 releases
age-gate / vendor / nette / utils / src / Utils / Random.php

Random.php in Age Gate 3.7.3, at vendor/nette/utils/src/Utils/Random.php

47 lines 1.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
8 declare(strict_types=1);
9
10 namespace Nette\Utils;
11
12 use Nette;
13
14
15 /**
16 * Secure random string generator.
17 */
18 final class Random
19 {
20 use Nette\StaticClass;
21
22 /**
23 * Generates a random string of given length from characters specified in second argument.
24 * Supports intervals, such as `0-9` or `A-Z`.
25 */
26 public static function generate(int $length = 10, string $charlist = '0-9a-z'): string
27 {
28 $charlist = count_chars(preg_replace_callback('#.-.#', function (array $m): string {
29 return implode('', range($m[0][0], $m[0][2]));
30 }, $charlist), 3);
31 $chLen = strlen($charlist);
32
33 if ($length < 1) {
34 throw new Nette\InvalidArgumentException('Length must be greater than zero.');
35 } elseif ($chLen < 2) {
36 throw new Nette\InvalidArgumentException('Character list must contain at least two chars.');
37 }
38
39 $res = '';
40 for ($i = 0; $i < $length; $i++) {
41 $res .= $charlist[random_int(0, $chLen - 1)];
42 }
43
44 return $res;
45 }
46 }
47