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

ArrayValueConverterMap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.74%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 53
rs 10
ccs 18
cts 19
cp 0.9474

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 3
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
        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