for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Issei;
/**
* Writes the csv row to stdout.
*
* {@internal Don't use this in user-land code }}
* @author Issei Murasawa <[email protected]>
*/
class CsvWriter
{
* @var resource
private $out;
* @var null
private $encodeTo;
public function __construct($encodeTo = null)
$this->out = fopen('php://output', 'wt');
if (null !== $encodeTo && 'UTF-8' !== strtoupper($encodeTo)) {
$this->encodeTo = $encodeTo;
}
public function __destruct()
fclose($this->out);
* Writes the csv to stdout.
* @param array|\Traversable $row
public function writeRow($row)
if (!is_array($row) && !$row instanceof \Traversable) {
throw new \InvalidArgumentException('Every value of $rows should be an array or an instance of \Traversable.');
$startedTraverse = false;
foreach ($row as $cell) {
if ($startedTraverse) {
fwrite($this->out, ',');
} else {
$startedTraverse = true;
if (null !== $this->encodeTo) {
$cell = mb_convert_encoding($cell, $this->encodeTo, 'UTF-8');
fwrite($this->out, '"' . str_replace('"', '""', $cell) . '"');
fwrite($this->out, "\r\n");