KeysToCamelCaseWalker::toCamelCase()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
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 1
        return lcfirst($camelCasedName);
32
    }
33
}
34