1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,124 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+declare(strict_types=1); |
|
4 |
+ |
|
5 |
+/* |
|
6 |
+ * This file is part of dacore bundle for Contao. |
|
7 |
+ * |
|
8 |
+ * (c) Benjamin Roth |
|
9 |
+ * |
|
10 |
+ * @license commercial |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+namespace vossmedien\DacoreBundle\Generator; |
|
14 |
+ |
|
15 |
+class CodeGenerator |
|
16 |
+{ |
|
17 |
+ /** |
|
18 |
+ * Seed presets |
|
19 |
+ */ |
|
20 |
+ public const SEED_NUMERIC = 1; |
|
21 |
+ public const SEED_LETTERS_LOWER = 2; |
|
22 |
+ public const SEED_LETTERS_UPPER = 4; |
|
23 |
+ public const SEED_SYMBOLS = 8; |
|
24 |
+ |
|
25 |
+ public const SEED_READABLE = 5; |
|
26 |
+ public const SEED_LETTERS_MIXED = 6; |
|
27 |
+ public const SEED_SECURE = 15; |
|
28 |
+ |
|
29 |
+ |
|
30 |
+ /** |
|
31 |
+ * Generate a code |
|
32 |
+ * |
|
33 |
+ * @param array $arrOptions |
|
34 |
+ * @return string |
|
35 |
+ */ |
|
36 |
+ public function generate($arrOptions = []) |
|
37 |
+ { |
|
38 |
+ $arrOptions = array_merge |
|
39 |
+ ( |
|
40 |
+ array |
|
41 |
+ ( |
|
42 |
+ 'length' => 12, // ToDo: Retrieve default length from global settings |
|
43 |
+ 'seed_preset' => self::SEED_SECURE, |
|
44 |
+ 'custom_seed' => null, |
|
45 |
+ 'prefix' => '', |
|
46 |
+ 'suffix' => '', |
|
47 |
+ ),$arrOptions |
|
48 |
+ ); |
|
49 |
+ |
|
50 |
+ |
|
51 |
+ if (intval($arrOptions['length']) < 5) |
|
52 |
+ { |
|
53 |
+ throw new \InvalidArgumentException('The length option has to be at least 5.'); |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ // Seed presets |
|
57 |
+ $numbers = [1,2,3,4,5,6,7,8,9]; |
|
58 |
+ $lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; |
|
59 |
+ $uppercase = ['A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z']; |
|
60 |
+ $symbols = ['°','!','§','$','%','&','/','(',')','=','?','*','+','~','#','-','_','.',':',';','>','<','|','[',']','{','}','\\']; |
|
61 |
+ |
|
62 |
+ $seed = []; |
|
63 |
+ $code = ''; |
|
64 |
+ |
|
65 |
+ // Use custom seed |
|
66 |
+ if ($arrOptions['custom_seed'] !== null) |
|
67 |
+ { |
|
68 |
+ if (is_array($arrOptions['custom_seed'])) |
|
69 |
+ { |
|
70 |
+ $seed = $arrOptions['custom_seed']; |
|
71 |
+ } elseif (is_string($arrOptions['custom_seed'])) |
|
72 |
+ { |
|
73 |
+ $seed = str_split($arrOptions['custom_seed'],1); |
|
74 |
+ } else { |
|
75 |
+ throw new \InvalidArgumentException('The custom_seed option needs to be an array or string.'); |
|
76 |
+ } |
|
77 |
+ } else |
|
78 |
+ { |
|
79 |
+ // Add numbers to seed |
|
80 |
+ if ($arrOptions['seed_preset'] & self::SEED_NUMERIC) |
|
81 |
+ { |
|
82 |
+ $seed = array_merge($seed, $numbers, $numbers); // add numbers twice to increase their occurrence in generated code |
|
83 |
+ } |
|
84 |
+ |
|
85 |
+ // Add lowercase letters to seed |
|
86 |
+ if ($arrOptions['seed_preset'] & self::SEED_LETTERS_LOWER) |
|
87 |
+ { |
|
88 |
+ $seed = array_merge($seed, $lowercase); |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ // Add uppercase letters to seed |
|
92 |
+ if ($arrOptions['seed_preset'] & self::SEED_LETTERS_UPPER) |
|
93 |
+ { |
|
94 |
+ $seed = array_merge($seed, $uppercase); |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ // Add symbols to seed |
|
98 |
+ if ($arrOptions['seed_preset'] & self::SEED_SYMBOLS) |
|
99 |
+ { |
|
100 |
+ $seed = array_merge($seed, $symbols); |
|
101 |
+ } |
|
102 |
+ } |
|
103 |
+ |
|
104 |
+ // Generate random code string |
|
105 |
+ for ($i = 0; $i < intval($arrOptions['length']); $i++) { |
|
106 |
+ $code .= substr(strval($seed[mt_rand(0, count($seed) - 1)]),0,1); |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ // Add optional prefix |
|
110 |
+ if ($arrOptions['prefix']) |
|
111 |
+ { |
|
112 |
+ $code = $arrOptions['prefix'].$code; |
|
113 |
+ } |
|
114 |
+ |
|
115 |
+ // Add optional suffix |
|
116 |
+ if ($arrOptions['suffix']) |
|
117 |
+ { |
|
118 |
+ $code = $arrOptions['suffix'].$code; |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ return $code; |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+} |