Passed
Push — master ( 0e441f...fe9525 )
by Daniel
02:35
created

Formatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A format() 0 10 3
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv\Formatters;
13
14
15
/**
16
 * Class Formatter
17
 *
18
 * @author  Dan McAdams
19
 * @package RoadBunch\Csv\Formatters
20
 */
21
class Formatter implements FormatterInterface
22
{
23
    /** @var callable */
24
    protected $callback;
25
26
    /**
27
     * Formatter constructor.
28
     *
29
     * @param callable $callback
30
     */
31 9
    public function __construct(callable $callback)
32
    {
33 9
        $this->callback = $callback;
34 9
    }
35
36
    /**
37
     * @param array $header an array of strings to be formatted
38
     *
39
     * @return array
40
     */
41 6
    public function format(array $header): array
42
    {
43 6
        $ret = [];
44 6
        foreach ($header as $value) {
45 6
            if (!is_string($value)) {
46 1
                throw new \InvalidArgumentException('All elements of the array must be strings');
47
            }
48 5
            $ret[] = call_user_func($this->callback, $value);
49
        }
50 5
        return $ret;
51
    }
52
}
53