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

ArrayValueConverterMap   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 49
ccs 15
cts 16
cp 0.9375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 8 2
A convertItem() 0 14 4
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