KeysToCamelCaseWalker::walk()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 3
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 1
        return lcfirst($camelCasedName);
32
    }
33
}
34