Helper::camelCase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition;
4
5
/**
6
 * Helper class
7
 *
8
 * This class defines static utility functions
9
 */
10
class Helper
11
{
12
    /**
13
     * Initialize an object with a given array of parameters
14
     *
15
     * Parameters are automatically converted to camelCase. Any parameters which do
16
     * not match a setter on the target object are ignored.
17
     *
18
     * @param Object $target     The object to set parameters on
19
     * @param array $parameters An array of parameters to set
20
     */
21 42
    public static function initialize($target, $parameters)
22
    {
23 42
        if (is_array($parameters)) {
24 42
            foreach ($parameters as $key => $value) {
25 36
                $method = 'set'.ucfirst(static::camelCase($key));
26 36
                if (method_exists($target, $method)) {
27 36
                    $target->$method($value);
28 36
                }
29 42
            }
30 42
        }
31 42
    }
32
33
    /**
34
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
35
     *
36
     * @param  string  $str The input string
37
     * @return string camelCased output string
38
     */
39 36
    public static function camelCase($str)
40
    {
41 36
        return preg_replace_callback(
42 36
            '/_([a-z])/',
43 36
            function ($match) {
44 20
                return strtoupper($match[1]);
45 36
            },
46
            $str
47 36
        );
48
    }
49
}
50