Passed
Push — master ( d6195c...1d634b )
by Andrea
30:07 queued 21s
created

StringUtils   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
dl 0
loc 49
ccs 9
cts 15
cp 0.6
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toCamelCase() 0 13 3
A underscore() 0 7 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\String;
4
5
class StringUtils
6
{
7
    /**
8
     * Translates a string with underscores into camel case (e.g. first_name -&gt; firstName).
9
     *
10
     * @param array $parametri
11
     *
12
     * @return string $str translated into camel caps
13
     */
14 3
    public static function toCamelCase($parametri = array())
15
    {
16 3
        $str = $parametri['str'];
17 3
        $capitalise_first_char = isset($parametri['primamaiuscola']) ? $parametri['primamaiuscola'] : false;
18
19 3
        if ($capitalise_first_char) {
20 3
            $str[0] = strtoupper($str[0]);
21
        }
22 3
        $func = function ($matches) {
23 2
            return strtoupper($matches[1]);
24 3
        };
25
26 3
        return preg_replace_callback('/_([a-z])/', $func, $str);
27
    }
28
29
      /**
30
        * Transforms an under_scored_string to a camelCasedOne
31
        */
32
    /*function camelize($scored) {
33
        return lcfirst(
34
        implode(
35
            '',
36
            array_map(
37
            'ucfirst',
38
            array_map(
39
                'strtolower',
40
                explode(
41
                '_', $scored)))));
42
    }*/
43
44
    /**
45
     * Transforms a camelCasedString to an under_scored_one
46
    */
47
    public static function underscore($cameled)
48
    {
49
        return implode(
50
            '_',
51
            array_map(
52
                'strtolower',
53
                preg_split('/([A-Z]{1}[^A-Z]*)/', $cameled, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)
54
            )
55
        );
56
    }
57
}
58