diff --git a/src/Goodby/CSV/Export/Standard/CsvFileObject.php b/src/Goodby/CSV/Export/Standard/CsvFileObject.php index 2116374..f91908e 100644 --- a/src/Goodby/CSV/Export/Standard/CsvFileObject.php +++ b/src/Goodby/CSV/Export/Standard/CsvFileObject.php @@ -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); } diff --git a/src/Goodby/CSV/Export/Standard/Exporter.php b/src/Goodby/CSV/Export/Standard/Exporter.php index c9bd719..1493719 100644 --- a/src/Goodby/CSV/Export/Standard/Exporter.php +++ b/src/Goodby/CSV/Export/Standard/Exporter.php @@ -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); @@ -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(); diff --git a/src/Goodby/CSV/Export/Standard/ExporterConfig.php b/src/Goodby/CSV/Export/Standard/ExporterConfig.php index 78fc107..1099169 100644 --- a/src/Goodby/CSV/Export/Standard/ExporterConfig.php +++ b/src/Goodby/CSV/Export/Standard/ExporterConfig.php @@ -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 @@ -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; + } } diff --git a/src/Goodby/CSV/Export/Tests/Standard/Unit/ExporterConfigTest.php b/src/Goodby/CSV/Export/Tests/Standard/Unit/ExporterConfigTest.php index 835c061..6e00545 100644 --- a/src/Goodby/CSV/Export/Tests/Standard/Unit/ExporterConfigTest.php +++ b/src/Goodby/CSV/Export/Tests/Standard/Unit/ExporterConfigTest.php @@ -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()); + } } diff --git a/src/Goodby/CSV/Import/Standard/Interpreter.php b/src/Goodby/CSV/Import/Standard/Interpreter.php index 28974a0..2dba701 100644 --- a/src/Goodby/CSV/Import/Standard/Interpreter.php +++ b/src/Goodby/CSV/Import/Standard/Interpreter.php @@ -84,6 +84,8 @@ private function notify($line) */ private function delegate($observer, $line) { + $this->checkCallable($observer); + call_user_func($observer, $line); }