Skip to content

Commit

Permalink
Merge pull request #2 from keboola/webrouse-COM-487-optional-header
Browse files Browse the repository at this point in the history
The header does not have to be written to the file
  • Loading branch information
michaljurecko authored Oct 26, 2020
2 parents 73df423 + 6d6c288 commit 0ad746e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Keboola/CsvTable/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class Table extends CsvWriter {
/** @var array */
protected $header = [];

public function __construct(string $name, array $header = [], Temp $temp = null, $delimiter = CsvOptions::DEFAULT_DELIMITER, $enclosure = CsvOptions::DEFAULT_ENCLOSURE, $lineBreak = "\n")
public function __construct(string $name, array $header = [], bool $writeHeader = true, Temp $temp = null, $delimiter = CsvOptions::DEFAULT_DELIMITER, $enclosure = CsvOptions::DEFAULT_ENCLOSURE, $lineBreak = "\n")
{
$this->temp = $temp ? $temp : new Temp('csv-table');
$this->header = $header;
$tmpFile = $this->temp ->createTmpFile($name);
parent::__construct($tmpFile->getPathname(), $delimiter, $enclosure, $lineBreak);

if (!empty($this->header)) {
if (!empty($this->header) && $writeHeader) {
$this->writeRow($this->header);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Keboola/CsvTable/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function testCreate()
);
}

public function testDontWriteHeader()
{
$table = new Table('filename_suffix', ['first_col', 'second_col'], false);
$this->assertFileExists($table->getPathName());
$this->assertEquals('', file_get_contents($table->getPathName()));
$this->assertEquals(
array (
0 => 'first_col',
1 => 'second_col',
),
$table->getHeader()
);
}

public function testPrimaryKey()
{
$table = new Table('pk', ['id', 'user', 'data']);
Expand Down

0 comments on commit 0ad746e

Please sign in to comment.