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