PluginProbe ʕ •ᴥ•ʔ
FAPI Member / trunk
FAPI Member vtrunk
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Utils / Random.php
fapi-member / src / Utils Last commit date
AlertProvider.php 1 year ago ApiClient.php 1 day ago DateTimeHelper.php 1 year ago DateTimes.php 2 years ago DateTimesImmutable.php 2 years ago Debugger.php 1 year ago DisplayHelper.php 1 year ago EmailHelper.php 1 year ago PostTypeHelper.php 2 years ago Random.php 4 years ago SecurityValidator.php 2 years ago ShortcodeSubstitutor.php 2 months ago TemplateProvider.php 2 years ago functions.php 3 years ago
Random.php
57 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
8 namespace FapiMember\Utils;
9
10 use InvalidArgumentException;
11
12 /**
13 * Secure random string generator.
14 */
15 final class Random {
16
17
18 /**
19 * Generates a random string of given length from characters specified in second argument.
20 * Supports intervals, such as `0-9` or `A-Z`.
21 *
22 * @param int $length
23 * @param string $charlist
24 * @return string
25 */
26 public static function generate( $length = 10, $charlist = '0-9a-z' ) {
27 $charlist = count_chars(
28 preg_replace_callback(
29 '#.-.#',
30 static function ( array $m ) {
31 return implode( '', range( $m[0][0], $m[0][2] ) );
32 },
33 $charlist
34 ),
35 3
36 );
37
38 $chLen = strlen( $charlist );
39
40 if ( $length < 1 ) {
41 throw new InvalidArgumentException( 'Length must be greater than zero.' );
42 }
43
44 if ( $chLen < 2 ) {
45 throw new InvalidArgumentException( 'Character list must contain at least two chars.' );
46 }
47
48 $res = '';
49 for ( $i = 0; $i < $length; $i++ ) {
50 $res .= $charlist[ random_int( 0, $chLen - 1 ) ];
51 }
52
53 return $res;
54 }
55
56 }
57