Completed
Push — master ( de9ece...1b5216 )
by Gabriel
05:03
created

Helper::convertToLowercase()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient;
4
5
/**
6
 * Class Helper
7
 * @package ByTIC\MFinante
8
 */
9
class Helper
10
{
11
12
    /**
13
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
14
     *
15
     * @param  string $str The input string
16
     * @return string camelCased output string
17
     */
18
    public static function camelCase($str)
19
    {
20
        $str = self::convertToLowercase($str);
21
22
        return preg_replace_callback(
23
            '/_([a-z])/',
24
            function ($match) {
25
                return strtoupper($match[1]);
26
            },
27
            $str
28
        );
29
    }
30
31
    /**
32
     * Convert strings with underscores to be all lowercase before camelCase is preformed.
33
     *
34
     * @param  string $str The input string
35
     * @return string The output string
36
     */
37
    protected static function convertToLowercase($str)
38
    {
39
        $explodedStr = explode('_', $str);
40
41
        if (count($explodedStr) > 1) {
42
            $lowerCasedStr = [];
43
            foreach ($explodedStr as $value) {
44
                $lowerCasedStr[] = strtolower($value);
45
            }
46
            $str = implode('_', $lowerCasedStr);
47
        }
48
49
        return $str;
50
    }
51
}
52