Hi Internals,
Currently the array_change_key_case function supports only 2 case types
(CASE_UPPER and CASE_LOWER). I would like to propose 4 new cases for this
function:
- CASE_CAMEL: transform the strings in camel cased [1], ie. transform
strings like 'This String', 'this string' or 'this_string' into 'thisString' - CASE_PASCAL (or CASE_UPPER_CAMEL): same as camel case, but the first
caracter in capital. Ie, 'ThisString' - CASE_SNAKE: Change the key to have underscore between the spaces or
capital letters [2]. Ie, 'This String', 'this string' and 'thisString'
would be converted to 'this_string'. ProbablyCASE_UPPER
andCASE_LOWER
(default) can be combined with this. - CASE_HYPHEN: Similar of snake case, but using dashes instead of
underscore [3], ie 'this-string'. This case also could be combined with
CASE_UPPER
and CASE_LOWER.
[1] http://en.wikipedia.org/wiki/CamelCase
[2] http://en.wikipedia.org/wiki/Snake_case
[3] http://en.wikipedia.org/wiki/Underscore
This can be useful for apps using camel cased keys and want to export to
APIs, which usually are underscored. It can also be used to convert objects
using camel cased properties to other cases easily by something like
array_change_key_case((array)$object, CASE_SNAKE).
Extending a little bit more (probably should be another RFC), but since it
is related, having this function for regular strings (not only array keys)
would be useful too. I see frameworks implementing it (or similar to it),
ie:
- CakePHP Inflector class has methods underscore, dasherize, camelize [4]
- Zend Word Filters [5]
- Laravel Str class has methods snake, slug, camel [6]
- Doctrine underscore naming strategy (default) [7]
- I guess Symfony has similar functionality on the serializers, but I
couldn't find at the moment - Not PHP, but Ruby on Rails [8]
[4] http://api.cakephp.org/3.0/class-Cake.Utility.Inflector.html
[5]
http://framework.zend.com/manual/current/en/modules/zend.filter.word.html
[6] http://laravel.com/api/5.0/Illuminate/Support/Str.html
[7]
http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html#underscore-naming-strategy
[8] http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html
Any thoughts?
Thanks,
Juan Basso