for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Issei\StreamedCsvResponse;
/**
* 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)
Assert::isIterable($row, 'Every value of $rows should be an array or an instance of \Traversable.');
$separator = '';
foreach ($row as $cell) {
fwrite($this->out, $separator . $this->formatCell($cell));
if ('' === $separator) {
$separator = ',';
fwrite($this->out, "\r\n");
* Returns the formatted cell.
* @param string $cell
* @return string
private function formatCell($cell)
// auto encoding
if (null !== $this->encodeTo) {
$cell = mb_convert_encoding($cell, $this->encodeTo, 'UTF-8');
// enclosing
return '"' . str_replace('"', '""', $cell) . '"';