|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Level23\Druid\InputFormats; |
|
5
|
|
|
|
|
6
|
|
|
class CsvInputFormat implements InputFormatInterface |
|
7
|
|
|
{ |
|
8
|
|
|
protected ?string $listDelimiter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string[]|null |
|
12
|
|
|
*/ |
|
13
|
|
|
protected ?array $columns = null; |
|
14
|
|
|
|
|
15
|
|
|
protected ?bool $findColumnsFromHeader; |
|
16
|
|
|
|
|
17
|
|
|
protected int $skipHeaderRows; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string[]|null $columns Specifies the columns of the data. The columns should be in the same |
|
21
|
|
|
* order with the columns of your data. |
|
22
|
|
|
* @param string|null $listDelimiter A custom delimiter for multi-value dimensions. |
|
23
|
|
|
* @param bool|null $findColumnsFromHeader If this is set, the task will find the column names from the header |
|
24
|
|
|
* row. Note that skipHeaderRows will be applied before finding column |
|
25
|
|
|
* names from the header. For example, if you set skipHeaderRows to 2 and |
|
26
|
|
|
* findColumnsFromHeader to true, the task will skip the first two lines |
|
27
|
|
|
* and then extract column information from the third line. columns will |
|
28
|
|
|
* be ignored if this is set to true. |
|
29
|
|
|
* @param int $skipHeaderRows If this is set, the task will skip the first skipHeaderRows rows. |
|
30
|
|
|
*/ |
|
31
|
16 |
|
public function __construct( |
|
32
|
|
|
array $columns = null, |
|
33
|
|
|
string $listDelimiter = null, |
|
34
|
|
|
bool $findColumnsFromHeader = null, |
|
35
|
|
|
int $skipHeaderRows = 0 |
|
36
|
|
|
) { |
|
37
|
16 |
|
$this->listDelimiter = $listDelimiter; |
|
38
|
16 |
|
$this->columns = $columns; |
|
39
|
16 |
|
$this->findColumnsFromHeader = $findColumnsFromHeader; |
|
40
|
16 |
|
$this->skipHeaderRows = $skipHeaderRows; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Return the CsvInputFormat so that it can be used in a druid query. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array<string,string|string[]|int|bool> |
|
47
|
|
|
*/ |
|
48
|
16 |
|
public function toArray(): array |
|
49
|
|
|
{ |
|
50
|
16 |
|
$result = ['type' => 'csv']; |
|
51
|
|
|
|
|
52
|
16 |
|
if (!empty($this->columns)) { |
|
53
|
8 |
|
$result['columns'] = $this->columns; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
16 |
|
if ($this->listDelimiter !== null) { |
|
57
|
6 |
|
$result['listDelimiter'] = $this->listDelimiter; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
16 |
|
if ($this->findColumnsFromHeader !== null) { |
|
61
|
6 |
|
$result['findColumnsFromHeader'] = $this->findColumnsFromHeader; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
16 |
|
if ($this->skipHeaderRows > 0) { |
|
65
|
6 |
|
$result['skipHeaderRows'] = $this->skipHeaderRows; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
16 |
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
} |