Passed
Branch refactor_config_class (788a46)
by Jens
08:38
created

ConfigObject::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core;
7
8
use Commercetools\Core\Error\InvalidArgumentException;
9
use Commercetools\Core\Error\Message;
10
11
abstract class ConfigObject
12
{
13
    /**
14
     * @param array $configValues
15
     * @return static
16
     */
17
    public static function fromArray(array $configValues)
18
    {
19
        $config = static::of();
20
        array_walk(
21
            $configValues,
22
            function ($value, $key) use ($config) {
23
                $functionName = 'set' . $config->camelize($key);
24
                if (!is_callable([$config, $functionName])) {
25
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
26
                }
27
                $config->$functionName($value);
28
            }
29
        );
30
31
        return $config;
32
    }
33
34
    protected function camelize($scored)
35
    {
36
        return lcfirst(
37
            implode(
38
                '',
39
                array_map(
40
                    'ucfirst',
41
                    array_map(
42
                        'strtolower',
43
                        explode('_', $scored)
44
                    )
45
                )
46
            )
47
        );
48
    }
49
50
    /**
51
     * @return static
52
     */
53
    public static function of()
54
    {
55
        return new static();
56
    }
57
}
58