CsvView\View\CsvView::_generateRow PHP Method

_generateRow() protected method

Generates a single row in a csv from an array of data by writing the array to a temporary file and returning it's contents
protected _generateRow ( array | null $row = null ) : string | false
$row array | null Row data
return string | false String with the row in csv-syntax, false on fputscv failure
    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;
    }