Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85491 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21573 invoked from network); 27 Mar 2015 03:03:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Mar 2015 03:03:24 -0000 Authentication-Results: pb1.pair.com header.from=jrbasso@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jrbasso@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.171 as permitted sender) X-PHP-List-Original-Sender: jrbasso@gmail.com X-Host-Fingerprint: 209.85.223.171 mail-ie0-f171.google.com Received: from [209.85.223.171] ([209.85.223.171:34785] helo=mail-ie0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 18/38-00585-A78C4155 for ; Thu, 26 Mar 2015 22:03:23 -0500 Received: by iedfl3 with SMTP id fl3so69269647ied.1 for ; Thu, 26 Mar 2015 20:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=wVNuN1/ao3sbUAD/f5XYsKQYcHPcTx2iuZKP8Q9D5bk=; b=WwwNoC5aE5vhTIst1RUhlYNGyyphjVhHyjVZ7bKdXAnC55Ag4YcCJDH4gNlcdm/1L+ PeWa7o/OTR9JCf2Rid3hB98ZKoUUBY/U/+2iWzeguAhl1eNN9XOPDUyWwtR7CU0W9Mng YqsJ4KxZ3NkA3R+VIdehTVycwWiAvDelyeBfyXH4k0hZEnDF3OGnj9UiVQwueGDzbcaz NqEqhRH39+/pgbBRHMZRWEA7/52Z3BJ8KgNltvFswC/cpSny5yh9bGzlHwrtZwRpZIf9 9J6UUzmilLLms2otyo05fdAYYDxW760Tajzix5XwfgprDFfTIJVYH5znc0+EdonYYnKt 60kA== MIME-Version: 1.0 X-Received: by 10.107.156.19 with SMTP id f19mr26088852ioe.45.1427425400471; Thu, 26 Mar 2015 20:03:20 -0700 (PDT) Received: by 10.64.148.6 with HTTP; Thu, 26 Mar 2015 20:03:20 -0700 (PDT) Date: Thu, 26 Mar 2015 23:03:20 -0400 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=001a1141b9acf0482805123c5fcd Subject: [RFC] Proposal - Add more cases to array_change_key_case From: jrbasso@gmail.com (Juan Basso) --001a1141b9acf0482805123c5fcd Content-Type: text/plain; charset=UTF-8 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'. Probably CASE_UPPER and CASE_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 --001a1141b9acf0482805123c5fcd--