1: <?php
2:
3: /**
4: * Orchestra: A minimalist object-oriented superset for WordPress using Phalcon.
5: *
6: * This source file is subject to the MIT license that is bundled
7: * with this package in the file LICENSE and is available through
8: * the world-wide-web at the following URI:
9: * http://opensource.org/licenses/MIT
10: *
11: * @copyright Akufen Atelier Creatif
12: * @author Nicholas Charbonneau <nicholas@akufen.ca>
13: * @license http://opensource.org/licenses/MIT
14: * @version 0.1.3
15: * @link https://github.com/akufenstudio/orchestra
16: */
17:
18: namespace Akufen\Orchestra\Helpers;
19:
20: /**
21: * Akufen\Orchestra\Helpers\Strings
22: *
23: * A helper for easy string manipulation
24: *
25: * @package Helpers
26: */
27: class Strings
28: {
29: /**
30: * Converts a camel case string to an underscored one
31: *
32: * @param String $string The string to convert
33: * @return $string The converted string
34: */
35: public static function toUnderscores($string)
36: {
37: // String must be string
38: $string = strval($string);
39:
40: // Match all camel cases
41: preg_match_all(
42: '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!',
43: $string,
44: $matches
45: );
46:
47: // Lowercase all matches
48: $return = $matches[0];
49: foreach ($return as &$match) {
50: $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
51: }
52:
53: // Return underscored string
54: return implode('_', $return);
55: }
56: }
57: