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

KeysWithValue   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
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 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