On Jun 6, 2012, at 5:31 AM, Robin Winslow wrote:
> I've always wondered how to do camelCase and StudlyCaps in these cases. I like the idea of lower-casing all acronyms (Http, HttpManager) and I'm also interested in how OAuth or JQuery should be dealt with. I suppose the only solution is to do them just like that with two leading capitals. Thoughts?
It occurs to me that it would be useful to be able to convert between StudlyCaps, camelCase, and snake_case forms (say, for taking SQL column names and turning them into method names). Some example bits:
// camelCase and StudlyCaps to snake_case
public function camelToSnake($str)
{
$str = preg_replace('/([a-z])([A-Z])/', '$1 $2', $str);
$str = str_replace(' ', '_', strtolower($str));
return $str;
}
// snake_case to camelCase
public function snakeToCamel($str)
{
$str = ucwords(str_replace('_', ' ', $str));
$str = str_replace(' ', '', $str);
return lcfirst($str);
}
// snake_case to StudlyCaps
public function snakeToStudly($str)
{
$str = ucwords(str_replace('_', ' ', $str));
$str = str_replace(' ', '', $str);
return ucfirst($str);
}
This particular implementation means that having (e.g.) OAuthHTTP won't necessarily get translated right; it would have to be OauthHttp to reliably convert back-and-forth to oauthHttp and oauth_http.
Thoughts?
-- pmj