Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for writuing column names #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/Goodby/CSV/Export/Standard/CsvFileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ public function fputcsv($fields, $delimiter = null, $enclosure = null)
fclose($fp);

/**
* Because the php_fputcsv() implementation in PHP´s source code
* has a hardcoded "\n", this method replaces the last LF code
* with what the client code wishes.
* Because php_fputcsv() implementation in PHP source code
* has hardcoded "\n", this method replace last LF code
* with what client code wish.
*/
$line = rtrim($line, "\n"). $this->newline;

// if the enclosure was '' | false
if (empty($enclosure)) {
$line = str_replace("\0", '', $line);
}


if ( is_callable($this->csvFilter) ) {
$line = call_user_func($this->csvFilter, $line);
}
Expand Down
21 changes: 14 additions & 7 deletions src/Goodby/CSV/Export/Standard/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function unstrict()
*/
public function export($filename, $rows)
{
$delimiter = $this->config->getDelimiter();
$enclosure = $this->config->getEnclosure();
$enclosure = empty($enclosure) ? "\0" : $enclosure;
$newline = $this->config->getNewline();
$fromCharset = $this->config->getFromCharset();
$toCharset = $this->config->getToCharset();
$fileMode = $this->config->getFileMode();
$delimiter = $this->config->getDelimiter();
$enclosure = $this->config->getEnclosure();
$newline = $this->config->getNewline();
$fromCharset = $this->config->getFromCharset();
$toCharset = $this->config->getToCharset();
$fileMode = $this->config->getFileMode();
$writeColumnNames = $this->config->getWriteColumnNames();

try {
$csv = new CsvFileObject($filename, $fileMode);
Expand All @@ -73,8 +73,15 @@ public function export($filename, $rows)
});
}


foreach ( $rows as $row ) {
$this->checkRowConsistency($row);

if ($writeColumnNames) {
$csv->fputcsv(array_keys($row), $delimiter, $enclosure);
$writeColumnNames = false;
}

$csv->fputcsv($row, $delimiter, $enclosure);
}
$csv->fflush();
Expand Down
26 changes: 26 additions & 0 deletions src/Goodby/CSV/Export/Standard/ExporterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class ExporterConfig
*/
private $toCharset = null;

/**
* write column names in first line of csv
* @var bool
*/
private $writeColumnNames = false;

/**
* File mode
* @var string
Expand Down Expand Up @@ -188,4 +194,24 @@ public function getFileMode()
{
return $this->fileMode;
}

/**
* Set option for writing column names
* @param bool $writeColumnNames
* @return ExporterConfig
*/
public function setWriteColumnNames($writeColumnNames)
{
$this->writeColumnNames = $writeColumnNames;
return $this;
}

/**
* Return file mode
* @return string
*/
public function getWriteColumnNames()
{
return $this->writeColumnNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public function testToCharset()
$this->assertSame(null, $config->getToCharset());
$this->assertSame('UTF-8', $config->setToCharset('UTF-8')->getToCharset());
}

public function testWriteColumnNames()
{
$config = new ExporterConfig();
$this->assertSame(false, $config->getWriteColumnNames());
$this->assertSame(true, $config->setWriteColumnNames(true)->getWriteColumnNames());
}
}
2 changes: 2 additions & 0 deletions src/Goodby/CSV/Import/Standard/Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private function notify($line)
*/
private function delegate($observer, $line)
{
$this->checkCallable($observer);

call_user_func($observer, $line);
}

Expand Down