Completed
Pull Request — 2.0 (#75)
by Julien
02:32
created

Inflector::underscore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation;
11
12
/**
13
 * Inflector
14
 *
15
 * Turn identifiers from/to StudlyCaps/underscore.
16
 *
17
 * @package   Foundation
18
 * @copyright 2014 - 2015 Grégoire HUBERT
19
 * @author    Grégoire HUBERT
20
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
21
 */
22
class Inflector
23
{
24
    /**
25
     * Camelize a string.
26
     *
27
     * @static
28
     * @access public
29
     * @param  string $string
30
     * @return string
31
     */
32
    public static function studlyCaps($string = null)
33
    {
34
        if ($string === null) {
35
            return null;
36
        }
37
38
        return preg_replace_callback(
39
            '/_([a-z])/',
40
            function ($v) {
41
                return strtoupper($v[1]);
42
            },
43
            ucfirst(strtolower($string))
44
        );
45
    }
46
47
    /**
48
     * Underscore a string.
49
     *
50
     * @static
51
     * @access public
52
     * @param  string $string
53
     * @return string
54
     */
55
    public static function underscore($string = null)
56
    {
57
        if ($string === null) {
58
            return null;
59
        }
60
61
        return strtolower(preg_replace('/([A-Z])/', '_\1', lcfirst($string)));
62
    }
63
}
64