| Total Complexity | 10 |
| Total Lines | 62 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | class Generator |
||
| 6 | { |
||
| 7 | const PASS_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'; |
||
| 8 | const PASS_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 9 | const PASS_DIGITS = '0123456789'; |
||
| 10 | const PASS_SYMBOLS = '!@#$%^&*()_-=+;:.?'; |
||
| 11 | |||
| 12 | private $sets = []; |
||
| 13 | |||
| 14 | public function generate($length = 20) |
||
| 15 | { |
||
| 16 | $all = ''; |
||
| 17 | $password = ''; |
||
| 18 | foreach ($this->sets as $set) { |
||
| 19 | $password .= $set[$this->tweak(str_split($set))]; |
||
| 20 | $all .= $set; |
||
| 21 | } |
||
| 22 | $all = str_split($all); |
||
| 23 | for ($i = 0; $i < $length - count($this->sets); $i++) { |
||
| 24 | $password .= $all[$this->tweak($all)]; |
||
| 25 | } |
||
| 26 | |||
| 27 | return str_shuffle($password); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function tweak($array) |
||
| 31 | { |
||
| 32 | if (function_exists('random_int')) { |
||
| 33 | return random_int(0, count($array) - 1); |
||
| 34 | } elseif (function_exists('mt_rand')) { |
||
| 35 | return mt_rand(0, count($array) - 1); |
||
| 36 | } else { |
||
| 37 | return array_rand($array); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | public function useLower() |
||
| 42 | { |
||
| 43 | $this->sets['lower'] = self::PASS_LOWERCASE; |
||
| 44 | |||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function useUpper() |
||
| 49 | { |
||
| 50 | $this->sets['upper'] = self::PASS_UPPERCASE; |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function useDigits() |
||
| 60 | } |
||
| 61 | |||
| 62 | public function useSymbols() |
||
| 67 | } |
||
| 68 | } |
||
| 69 |