protected function _generateRow($row = null)
{
static $fp = false;
if (empty($row)) {
return '';
}
if ($fp === false) {
$fp = fopen('php://temp', 'r+');
if ($this->viewVars['_bom']) {
fwrite($fp, chr(0xef) . chr(0xbb) . chr(0xbf));
}
if ($this->viewVars['_setSeparator']) {
fwrite($fp, "sep=" . $this->viewVars['_delimiter'] . "\n");
}
} else {
ftruncate($fp, 0);
}
if ($this->viewVars['_null'] !== '') {
foreach ($row as &$field) {
if ($field === null) {
$field = $this->viewVars['_null'];
}
}
}
$delimiter = $this->viewVars['_delimiter'];
$enclosure = $this->viewVars['_enclosure'];
$newline = $this->viewVars['_newline'];
$row = str_replace(["\r\n", "\n", "\r"], $newline, $row);
if ($enclosure === '') {
// fputcsv does not supports empty enclosure
if (fputs($fp, implode($delimiter, $row) . "\n") === false) {
return false;
}
} else {
if (fputcsv($fp, $row, $delimiter, $enclosure) === false) {
return false;
}
}
rewind($fp);
$csv = '';
while (($buffer = fgets($fp, 4096)) !== false) {
$csv .= $buffer;
}
$eol = $this->viewVars['_eol'];
if ($eol !== "\n") {
$csv = str_replace("\n", $eol, $csv);
}
$dataEncoding = $this->viewVars['_dataEncoding'];
$csvEncoding = $this->viewVars['_csvEncoding'];
if ($dataEncoding !== $csvEncoding) {
$extension = $this->viewVars['_extension'];
if ($extension === self::EXTENSION_ICONV) {
$csv = iconv($dataEncoding, $csvEncoding, $csv);
} elseif ($extension === self::EXTENSION_MBSTRING) {
$csv = mb_convert_encoding($csv, $csvEncoding, $dataEncoding);
}
}
return $csv;
}