From c50246c694a90fea441a9cda02087cb8d91fb649 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 07:14:57 -0700 Subject: [PATCH] Refactoring Str class. --- system/str.php | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/system/str.php b/system/str.php index ec446c34..7e779b81 100644 --- a/system/str.php +++ b/system/str.php @@ -70,28 +70,8 @@ public static function random($length = 16, $type = 'alnum') { $value = ''; - // ----------------------------------------------------- - // Get the proper character pool for the type. - // ----------------------------------------------------- - switch ($type) - { - case 'alpha': - $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - break; + $pool_length = strlen($pool = static::pool($type)) - 1; - default: - $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - } - - // ----------------------------------------------------- - // 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 @@ public static function random($length = 16, $type = 'alnum') 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