fixing bugs and refactoring.

This commit is contained in:
Taylor Otwell
2011-10-15 22:38:43 -05:00
parent b40e69c47a
commit a7e98e8e9a
7 changed files with 57 additions and 34 deletions

View File

@@ -188,9 +188,28 @@ class Str {
*/
public static function random($length, $type = 'alnum')
{
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
}
return substr(str_shuffle(str_repeat(($type == 'alnum') ? $pool.'0123456789' : $pool, 5)), 0, $length);
/**
* Get the character pool for a given type of random string.
*
* @param string $type
* @return string
*/
protected static function pool($type)
{
switch ($type)
{
case 'alpha':
return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
case 'alnum':
return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
default:
throw new \Exception("Invalid random string type [$type].");
}
}
}