KeysWithValue::createKeys()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
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