Passed
Push — master ( d6e98a...6cf9ac )
by Daniel
01:52
created

Formatter::format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 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\Formatter;
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 10
    public function __construct(callable $callback)
32
    {
33 10
        $this->callback = $callback;
34 10
    }
35
36
    /**
37
     * @param array $header an array of strings to be formatted
38
     *
39
     * @return array
40
     */
41 7
    public function format(array $header): array
42
    {
43 7
        $formatted = [];
44 7
        foreach ($header as $value) {
45 7
            if (!is_string($value)) {
46 1
                throw new \InvalidArgumentException('All elements of the array must be strings');
47
            }
48 6
            $formatted[] = call_user_func($this->callback, $value);
49
        }
50 6
        return $formatted;
51
    }
52
}
53