Passed
Branch feature/static-formatter (adf5e0)
by Daniel
02:33
created

Formatter::format()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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
use RoadBunch\Csv\Exception\FormatterResultException;
14
15
16
/**
17
 * Class Formatter
18
 *
19
 * @author  Dan McAdams
20
 * @package RoadBunch\Csv\Formatter
21
 */
22
abstract class Formatter implements FormatterInterface
23
{
24 9
    protected static function applyFilter(callable $filter, array $data): array
25
    {
26 9
        $formatted = [];
27 9
        foreach ($data as $element) {
28 9
            if (!is_string($element)) {
29 1
                throw new \InvalidArgumentException('All elements of the array must be strings');
30
            }
31 8
            if (!is_string($formattedElement = call_user_func($filter, $element))) {
32 1
                throw new FormatterResultException('Formatter must result in an array of strings');
33
            }
34 7
            $formatted[] = $formattedElement;
35
        }
36 7
        return $formatted;
37
    }
38
}
39