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

Helper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 43
c 0
b 0
f 0
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A camelCase() 0 12 1
A convertToLowercase() 0 14 3
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