Skip to content

Commit

Permalink
CsvWriter: Added option to prepend header row with column names
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom authored and ddeboer committed May 30, 2015
1 parent 5726169 commit d27a581
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ class CsvWriter extends AbstractStreamWriter
* @var boolean
*/
private $utf8Encoding = false;
private $row = 1;

/**
* @var boolean
*/
protected $prependHeaderRow;

/**
* @param string $delimiter The delimiter
* @param string $enclosure The enclosure
* @param resource $stream
* @param boolean $utf8Encoding
*/
public function __construct($delimiter = ';', $enclosure = '"', $stream = null, $utf8Encoding = false)
public function __construct($delimiter = ';', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false)
{
parent::__construct($stream);

$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->utf8Encoding = $utf8Encoding;

$this->prependHeaderRow = $prependHeaderRow;
}

/**
Expand All @@ -54,6 +62,11 @@ public function prepare()
*/
public function writeItem(array $item)
{
if ($this->prependHeaderRow && 1 == $this->row++) {
$headers = array_keys($item);
fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure);
}

fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure);
}
}

0 comments on commit d27a581

Please sign in to comment.