Skip to content

Commit

Permalink
Inflector second revision
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydspaniard committed May 19, 2013
1 parent a1af54a commit edd7ec4
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions yii/helpers/base/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,15 @@ public static function singularize($word)
* Converts an underscored or CamelCase word into a English
* sentence.
*
* The titleize function converts text like "WelcomePage",
* "welcome_page" or "welcome page" to this "Welcome
* Page".
* If second parameter is set to 'first' it will only
* capitalize the first character of the title.
*
* @param string $word Word to format as tile
* @param string $uppercase If set to 'first' it will only uppercase the
* first character. Otherwise it will uppercase all
* the words in the title.
* @return string Text formatted as title
* @param string $words
* @param bool $ucAll whether to set all words to uppercase
* @return string
*/
public static function titleize($word, $uppercase = '')
public static function titleize($words, $ucAll = false)
{
$uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords';
return $uppercase(static::humanize(static::underscore($word)));

$words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? ucwords($words) : ucfirst($words);
}

/**
Expand All @@ -383,53 +376,36 @@ public static function titleize($word, $uppercase = '')
* "who's online" will be converted to "WhoSOnline"
*
* @see variablize
* @param string $word Word to convert to camel case
* @return string UpperCamelCasedWord
* @param string $word the word to CamelCase
* @return string
*/
public static function camelize($word)
{
return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word)));
}

/**
* Converts a word "into_it_s_underscored_version"
* Converts any "CamelCased" or "ordinary Word" into an "underscored_word".
*
* Convert any "CamelCased" or "ordinary Word" into an
* "underscored_word".
*
* This can be really useful for creating friendly URLs.
*
* @access public
* @static
* @param string $word Word to underscore
* @return string Underscored word
* @param string $words the word(s) to underscore
* @return string
*/
public static function underscore($word)
public static function underscore($words)
{
return strtolower(
preg_replace(
'/[^A-Z^a-z^0-9]+/',
'_',
preg_replace(
'/([a-zd])([A-Z])/',
'1_2',
preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)
)
)
);
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words));
}

/**
* Returns a human-readable string from $word
*
* @param string $word the string to humanize
* @param bool $uppercase whether to set all words to uppercase or not
* @param bool $ucAll whether to set all words to uppercase or not
* @return string
*/
public static function humanize($word, $uppercase = false)
public static function humanize($word, $ucAll = false)
{
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
return $uppercase ? ucwords($word) : ucfirst($word);
return $ucAll ? ucwords($word) : ucfirst($word);
}

/**
Expand Down

0 comments on commit edd7ec4

Please sign in to comment.