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

Inflector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 42
c 1
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A studlyCaps() 0 14 2
A underscore() 0 8 2
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