Inflector::underscore()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 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 - 2017 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
     * @param  string $string
28
     * @return string
29
     */
30
    public static function studlyCaps($string = null)
31
    {
32
        if ($string === null) {
33
            return null;
34
        }
35
36
        return preg_replace_callback(
37
            '/_([a-z])/',
38
            function ($v) {
39
                return strtoupper($v[1]);
40
            },
41
            ucfirst(strtolower($string))
42
        );
43
    }
44
45
    /**
46
     * Underscore a string.
47
     *
48
     * @param  string $string
49
     * @return string
50
     */
51
    public static function underscore($string = null)
52
    {
53
        if ($string === null) {
54
            return null;
55
        }
56
57
        return strtolower(preg_replace('/([A-Z])/', '_\1', lcfirst($string)));
58
    }
59
}
60