Completed
Pull Request — master (#312)
by
unknown
06:53
created

ArrayValueConverterMap::convertItem()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
cc 4
eloc 7
nc 3
nop 1
crap 4.0218
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
        return $this->convertItem($input);
35
    }
36
37
    /**
38
     * Convert an item of the array using the converter-map
39
     *
40
     * @param $item
41
     *
42
     * @return mixed
43
     */
44 1
    protected function convertItem($item)
45
    {
46 1
        foreach ($item as $key => $value) {
47 1
            if (!isset($this->converters[$key])) {
48
                continue;
49
            }
50
51 1
            foreach ($this->converters[$key] as $converter) {
52 1
                $item[$key] = call_user_func($converter, $item[$key]);
53 1
            }
54 1
        }
55
56 1
        return $item;
57
    }
58
}
59