KeysWithValue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createKeys() 0 13 4
A __construct() 0 3 1
A execute() 0 3 1
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 5
    public function execute(array $array): array
28
    {
29 5
        return $this->createKeys($array);
30
    }
31
32 5
    private function createKeys(array $array, string $prefix = ''): array
33
    {
34 5
        $keysWithValue = [];
35
36 5
        foreach ($array as $key => $value) {
37 5
            if (\is_array($value) && !empty($value)) {
38 4
                $keysWithValue = $this->mergeOperation->execute($keysWithValue, $this->createKeys($value, $prefix.$key.'.'));
39
            } else {
40 5
                $keysWithValue[$prefix.$key] = $value;
41
            }
42
        }
43
44 5
        return $keysWithValue;
45
    }
46
}
47