Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Random | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
char | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
str | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
alpha | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
num | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
alphaNum | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * @license MIT |
5 | * @author hazuki3417<hazuki3417@gmail.com> |
6 | * @copyright 2021 hazuki3417 all rights reserved. |
7 | */ |
8 | |
9 | namespace Selen\Str\Generator; |
10 | |
11 | /** |
12 | * ランダムな文字列の生成を提供するクラスです |
13 | */ |
14 | class Random |
15 | { |
16 | /** |
17 | * プールからランダムな1文字を生成します |
18 | * |
19 | * @param string $pool プールする文字列を指定します |
20 | * |
21 | * @return string 生成した文字列を返します |
22 | */ |
23 | public static function char(string $pool): string |
24 | { |
25 | $index = mt_rand(0, strlen($pool) - 1); |
26 | return \substr($pool, $index, 1); |
27 | } |
28 | |
29 | /** |
30 | * プールからランダムな文字列を生成します |
31 | * |
32 | * @param string $pool プールする文字列を指定します |
33 | * @param int $length 文字列長を指定します |
34 | * |
35 | * @return string 生成した文字列を返します |
36 | */ |
37 | public static function str(string $pool, int $length): string |
38 | { |
39 | $str = ''; |
40 | |
41 | for ($i = 0; $i < $length; ++$i) { |
42 | $str .= self::char($pool); |
43 | } |
44 | return $str; |
45 | } |
46 | |
47 | /** |
48 | * ランダムなアルファベット文字列を生成します |
49 | * |
50 | * @param int $length 文字列長を指定します |
51 | * |
52 | * @return string 生成した文字列を返します |
53 | */ |
54 | public static function alpha(int $length): string |
55 | { |
56 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
57 | return self::str($pool, $length); |
58 | } |
59 | |
60 | /** |
61 | * ランダムな数字文字列を生成します |
62 | * |
63 | * @param int $length 文字列長を指定します |
64 | * |
65 | * @return string 生成した文字列を返します |
66 | */ |
67 | public static function num(int $length): string |
68 | { |
69 | $pool = '0123456789'; |
70 | return self::str($pool, $length); |
71 | } |
72 | |
73 | /** |
74 | * ランダムなアルファベット・数字文字列を生成します |
75 | * |
76 | * @param int $length 文字列長を指定します |
77 | * |
78 | * @return string 生成した文字列を返します |
79 | */ |
80 | public static function alphaNum(int $length): string |
81 | { |
82 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
83 | return self::str($pool, $length); |
84 | } |
85 | } |