Commit c50246c6 authored by Taylor Otwell's avatar Taylor Otwell

Refactoring Str class.

parent 607b23b7
......@@ -70,28 +70,8 @@ class Str {
{
$value = '';
// -----------------------------------------------------
// Get the proper character pool for the type.
// -----------------------------------------------------
switch ($type)
{
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
default:
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
$pool_length = strlen($pool = static::pool($type)) - 1;
// -----------------------------------------------------
// Get the pool length and split the pool into an array.
// -----------------------------------------------------
$pool_length = strlen($pool) - 1;
$pool = str_split($pool, 1);
// -----------------------------------------------------
// Build the random string to the specified length.
// -----------------------------------------------------
for ($i = 0; $i < $length; $i++)
{
$value .= $pool[mt_rand(0, $pool_length)];
......@@ -100,4 +80,26 @@ class Str {
return $value;
}
/**
* Get a chracter pool.
*
* @param string $type
* @return string
*/
private static function pool($type = 'alnum')
{
if ($type == 'alnum')
{
return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
elseif ($type == 'alpha')
{
return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
else
{
throw new \Exception("Unrecognized random pool [$type].");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment