UnderscoreKeysTransformer::transform()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Item\Transformer;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
7
class UnderscoreKeysTransformer implements TransformerInterface
8
{
9
    /**
10
     * @inheritdoc
11
     */
12 2
    public function transform(ParameterBag $item)
13
    {
14 2
        $parameters = $item->all();
15 2
        $this->underscoreKeys($parameters);
16 2
        $item->replace($parameters);
17 2
    }
18
19
    /**
20
     * @param array $arr
21
     */
22 2
    protected function underscoreKeys(array &$arr)
23
    {
24 2
        $new = [];
25
26 2
        foreach ($arr as $key => &$value) {
27 2
            if (is_array($value)) {
28 2
                $this->underscoreKeys($value);
29 2
            }
30
31 2
            $new[$this->underscore($key)] = $value;
32 2
        }
33
34 2
        $arr = $new;
35 2
    }
36
37
    /**
38
     * Copied from Doctrine's UnderscoreNamingStrategy.
39
     *
40
     * @param string $string
41
     *
42
     * @return string
43
     */
44 2
    protected function underscore($string)
45
    {
46 2
        return strtolower(
47 2
            str_replace('-', '_', preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string))
48 2
        );
49
    }
50
}
51