|
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
|
|
|
|
|
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
|
10 |
|
public function __construct(array $columns = []) |
|
40
|
|
|
{ |
|
41
|
10 |
|
foreach ($columns as $column) { |
|
42
|
4 |
|
if (!is_string($column)) { |
|
43
|
4 |
|
throw new InvalidInputArrayException('Columns must be array of (string)'); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
9 |
|
$this->columns = $columns; |
|
47
|
9 |
|
} |
|
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 FormatterInterface $formatter |
|
61
|
|
|
* @return HeaderInterface |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function addFormatter(FormatterInterface $formatter): HeaderInterface |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->formatters[] = $formatter; |
|
66
|
2 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
7 |
|
public function getFormattedColumns(): array |
|
73
|
|
|
{ |
|
74
|
7 |
|
$columns = $this->columns; |
|
75
|
|
|
|
|
76
|
7 |
|
foreach ($this->formatters as $formatter) { |
|
77
|
1 |
|
$columns = $formatter->format($columns); |
|
78
|
|
|
} |
|
79
|
7 |
|
return $columns; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|