Completed
Push — master ( 3feeea...9fb957 )
by David de
04:08
created

ArrayValueConverterMap::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Ddeboer\DataImport\ValueConverter;
4
5
/**
6
 * Converts a nested array using a converter-map
7
 *
8
 * @author Christoph Rosse <[email protected]>
9
 */
10
class ArrayValueConverterMap
11
{
12
    /**
13
     * @var array
14
     */
15
    private $converters;
16
17
    /**
18
     * @param callable[] $converters
19
     */
20 2
    public function __construct(array $converters)
21
    {
22 2
        $this->converters = $converters;
23 2
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function __invoke($input)
29
    {
30 2
        if (!is_array($input)) {
31 1
            throw new \InvalidArgumentException('Input of a ArrayValueConverterMap must be an array');
32
        }
33
34 1
        foreach ($input as $key => $item) {
35 1
            $input[$key] = $this->convertItem($item);
36 1
        }
37
38 1
        return $input;
39
    }
40
41
    /**
42
     * Convert an item of the array using the converter-map
43
     *
44
     * @param $item
45
     *
46
     * @return mixed
47
     */
48 1
    protected function convertItem($item)
49
    {
50 1
        foreach ($item as $key => $value) {
51 1
            if (!isset($this->converters[$key])) {
52
                continue;
53
            }
54
55 1
            foreach ($this->converters[$key] as $converter) {
56 1
                $item[$key] = call_user_func($converter, $item[$key]);
57 1
            }
58 1
        }
59
60 1
        return $item;
61
    }
62
}
63