Passed
Push — master ( fe8da9...4bb742 )
by Saulius
02:35
created

KeysWithValue::createKeys()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\ArrayOperation;
14
15
class KeysWithValue implements KeysWithValueInterface
16
{
17
    /**
18
     * @var MergeInterface
19
     */
20
    private $mergeOperation;
21
22 1
    public function __construct(MergeInterface $mergeOperation)
23
    {
24 1
        $this->mergeOperation = $mergeOperation;
25 1
    }
26
27 4
    public function execute(array $array): array
28
    {
29 4
        return $this->createKeys($array);
30
    }
31
32 4
    private function createKeys(array $array, string $prefix = ''): array
33
    {
34 4
        $keysWithValue = [];
35
36 4
        foreach ($array as $key => $value) {
37 4
            if (\is_array($value) && !empty($value)) {
38 3
                $keysWithValue = $this->mergeOperation->execute($keysWithValue, $this->createKeys($value, $prefix.$key.'.'));
39
            } else {
40 4
                $keysWithValue[$prefix.$key] = $value;
41
            }
42
        }
43
44 4
        return $keysWithValue;
45
    }
46
}
47