Beispiel #1
0
 public static function generate($options = array())
 {
     $options = self::_normalizeOptions($options);
     extract(array_merge(self::$defaultOptions, self::$_options, $options));
     self::$_options = array();
     $unique = false;
     $first_alaphabet = false;
     for ($i = 0; $i < strlen($type); $i++) {
         switch ($type[$i]) {
             case 'u':
                 $unique = true;
                 break;
             case 'f':
                 $first_alaphabet = true;
         }
     }
     $result = '';
     $result_length = 0;
     do {
         $c = self::generateCharacter($possible, $options);
         if ($first_alaphabet && $result_length == 0 && preg_match('/[a-z]/i', $possible)) {
             if (!preg_match('/[a-z]/', strtolower($c))) {
                 continue;
             }
         }
         if ($unique && strstr($result, $c)) {
             if ($result_length === strlen($possible)) {
                 trigger_error('Possible characters length is less than max length');
             }
             continue;
         }
         $result .= $c;
         $result_length++;
         if ($random_length) {
             $minLength = isset($random_length['min']) ? $random_length['min'] : 1;
             if ($minLength <= $result_length) {
                 $rand = $generator(1, $length);
                 if ($result_length < $rand) {
                     return $result;
                 }
             }
         }
     } while ($result_length < $length);
     return $result;
 }