From 3c05f7260c6b9021fa68ddf33aeb0e44d554ecaa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Sep 2011 22:25:54 -0500 Subject: [PATCH] refactoring string class. --- laravel/bootstrap/core.php | 5 +++ laravel/str.php | 79 ++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index 0c90cae6..c9e18571 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -62,4 +62,9 @@ function e($value) function __($key, $replacements = array(), $language = null) { return Lang::line($key, $replacements, $language); +} + +function fe($function) +{ + return function_exists($function); } \ No newline at end of file diff --git a/laravel/str.php b/laravel/str.php index 6213211b..ec10899c 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -5,78 +5,93 @@ class Str { /** * Convert a string to lowercase. * + * + * // Convert a string to lowercase + * echo Str::lower('STOP YELLING'); + * + * // Convert a UTF-8 string to lowercase + * echo Str::lower('Τάχιστη'); + * + * * @param string $value * @return string */ public static function lower($value) { - if (function_exists('mb_strtolower')) - { - return mb_strtolower($value, Config::get('application.encoding')); - } - - return strtolower($value); + return (fe('mb_strtolower')) ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value); } /** * Convert a string to uppercase. * + * + * // Convert a string to uppercase + * echo Str::upper('speak louder'); + * + * // Convert a UTF-8 string to uppercase + * echo Str::upper('Τάχιστη'); + * + * * @param string $value * @return string */ public static function upper($value) { - if (function_exists('mb_strtoupper')) - { - return mb_strtoupper($value, Config::get('application.encoding')); - } - - return strtoupper($value); + return (fe('mb_strtoupper')) ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value); } /** - * Convert a string to title case (ucwords). + * Convert a string to title case (ucwords equivalent). + * + * + * // Convert a string to title case + * echo Str::title('taylor otwell'); + * + * // Convert a UTF-8 string to title case + * echo Str::title('Τάχιστη αλώπηξ'); + * * * @param string $value * @return string */ public static function title($value) { - if (function_exists('mb_convert_case')) - { - return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')); - } - - return ucwords(strtolower($value)); + return (fe('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value)); } /** * Get the length of a string. * + * + * // Get the length of a string + * echo Str::length('taylor otwell'); + * + * // Get the length of a UTF-8 string + * echo Str::length('Τάχιστη αλώπηξ'); + * + * * @param string $value * @return int */ public static function length($value) { - if (function_exists('mb_strlen')) - { - return mb_strlen($value, Config::get('application.encoding')); - } - - return strlen($value); + return (fe('mb_strlen')) ? mb_strlen($value, Config::get('application.encoding')) : strlen($value); } /** * Convert a string to 7-bit ASCII. * + * + * // Returns "Deuxieme Article" + * echo Str::ascii('Deuxième Article'); + * + * * @param string $value * @return string */ public static function ascii($value) { - $foreign = Config::get('ascii'); - - $value = preg_replace(array_keys($foreign), array_values($foreign), $value); + $value = preg_replace(array_keys($foreign = Config::get('ascii')), array_values($foreign), $value); return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value); } @@ -84,7 +99,13 @@ public static function ascii($value) /** * Generate a random alpha or alpha-numeric string. * - * Supported types: 'alnum' and 'alpha'. + * + * // Generate a 40 character random, alpha-numeric string + * echo Str::random(40); + * + * // Generate a 16 character random, alphabetic string + * echo Str::random(16, 'alpha'); + * * * @param int $length * @param string $type