Passed
Push — master ( 82a58b...d35cf8 )
by Tomasz
02:32
created

KeysToCamelCaseWalker::toCamelCase()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ReadModel\Walker;
5
6
class KeysToCamelCaseWalker implements ResultWalker
7
{
8 1
    public function walk(array $result): array
9
    {
10 1
        $camelCased = [];
11
12 1
        array_walk($result, function ($value, $key) use (&$camelCased) {
13 1
            $key = is_string($key) ? $this->toCamelCase($key) : $key;
14
15 1
            if (is_array($value)) {
16 1
                $camelCased[$key] = $this->walk($value);
17
            } else {
18 1
                $camelCased[$key] = $value;
19
            }
20 1
        }, array_keys($result));
21
22 1
        return $camelCased;
23
    }
24
25
    private function toCamelCase(string $value): string
26
    {
27 1
        $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
28 1
            return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
29 1
        }, $value);
30
31
        return lcfirst($camelCasedName);
32
    }
33
}
34