Header::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
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\Header;
13
14
use RoadBunch\Csv\Exception\FormatterException;
15
use RoadBunch\Csv\Exception\InvalidInputArrayException;
16
use RoadBunch\Csv\Formatter\FormatterInterface;
17
18
/**
19
 * Class Header
20
 *
21
 * @author  Dan McAdams
22
 * @package RoadBunch\Csv\Header
23
 */
24
class Header implements HeaderInterface
25
{
26
    /** @var array */
27
    protected $columns;
28
29
    /** @var FormatterInterface[] */
30
    protected $formatters = [];
31
32
    /**
33
     * Header constructor.
34
     *
35
     * @param array $columns array of strings of column names
36
     *
37
     * @throws InvalidInputArrayException
38
     */
39 15
    public function __construct(array $columns = [])
40
    {
41 15
        foreach ($columns as $column) {
42 11
            if (!is_string($column)) {
43 11
                throw new InvalidInputArrayException('Columns must be array of (string)');
44
            }
45
        }
46 14
        $this->columns = $columns;
47 14
    }
48
49
    /**
50
     * @param string $column
51
     *
52
     * @return void
53
     */
54 3
    public function addColumn(string $column)
55
    {
56 3
        $this->columns[] = $column;
57 3
    }
58
59
    /**
60
     * @param mixed $formatter
61
     * @return HeaderInterface
62
     * @throws FormatterException
63
     */
64 5
    public function addFormatter($formatter): HeaderInterface
65
    {
66 5
        if ($formatter instanceof FormatterInterface) {
67 1
            $this->formatters[] = $formatter;
68 1
            return $this;
69
        }
70 4
        if (is_string($formatter)) {
71 3
            return $this->addFormatterFromString($formatter);
72
        }
73 1
        throw new FormatterException('Invalid formatter provided, must implement FormatterInterface');
74
    }
75
76
    /**
77
     * @return array
78
     */
79 9
    public function getFormattedColumns(): array
80
    {
81 9
        $columns = $this->columns;
82
83 9
        foreach ($this->formatters as $formatter) {
84 1
            $columns = $formatter::format($columns);
85
        }
86 9
        return $columns;
87
    }
88
89
    /**
90
     * @param $formatter
91
     * @return $this
92
     * @throws FormatterException
93
     */
94 3
    private function addFormatterFromString($formatter): Header
95
    {
96 3
        if (class_exists($formatter) && in_array(FormatterInterface::class, class_implements($formatter))) {
97 2
            $this->formatters[] = $formatter;
98 2
            return $this;
99
        }
100 1
        throw new FormatterException(sprintf('%s is not a class or does not implement FormatterInterface',
101 1
            (string)$formatter));
102
    }
103
}
104