Test Setup Failed
Push — master ( 1b5216...e31da6 )
by Gabriel
12:30
created

Helper::objectToArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 20
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
     *
17
     * @return string camelCased output string
18
     */
19
    public static function camelCase($str)
20
    {
21
        $str = self::convertToLowercase($str);
22
23
        return preg_replace_callback(
24
            '/_([a-z])/',
25
            function ($match) {
26
                return strtoupper($match[1]);
27
            },
28
            $str
29
        );
30
    }
31
32
    /**
33
     * @param $obj
34
     *
35
     * @return array
36
     */
37
    public static function objectToArray($obj)
38
    {
39
        if (is_object($obj)) {
40
            $obj = (array) $obj;
41
        }
42
        if (is_array($obj)) {
43
            $new = [];
44
            foreach ($obj as $key => $val) {
45
                $new[$key] = self::objectToArray($val);
46
            }
47
        } else {
48
            $new = $obj;
49
        }
50
51
        return $new;
52
    }
53
54
    /**
55
     * Convert strings with underscores to be all lowercase before camelCase is preformed.
56
     *
57
     * @param  string $str The input string
58
     *
59
     * @return string The output string
60
     */
61
    protected static function convertToLowercase($str)
62
    {
63
        $explodedStr = explode('_', $str);
64
65
        if (count($explodedStr) > 1) {
66
            $lowerCasedStr = [];
67
            foreach ($explodedStr as $value) {
68
                $lowerCasedStr[] = strtolower($value);
69
            }
70
            $str = implode('_', $lowerCasedStr);
71
        }
72
73
        return $str;
74
    }
75
}
76