src/Adapter/Goodby/Exporter.php
/**
     * Export data as CSV file.
     *
     * @param string $filename The filename to export to
     * @param array  $rows     The rows to export
     *
     * @return void
     * @throws \Goodby\CSV\Export\Protocol\Exception\IOException
     */
Export data as CSV file.
@param string $filename The filename to export to
@param array  $rows     The rows to export


Export data as CSV file . @param string $filename The filename to export to @param array $rows The rows to export
public function export($filename, $rows)
    {
        $this->reset();
        $delimiter     = $this->config->getDelimiter();
        $enclosure     = $this->config->getEnclosure();
        $enclosure     = empty($enclosure) ? "\0" : $enclosure;
        $newline       = $this->config->getNewline();
        $fromCharset   = $this->config->getFromCharset();
        $toCharset     = $this->config->getToCharset();
        $fileMode      = $this->config->getFileMode();
        $columnHeaders = $this->config->getColumnHeaders();
        try {
            $csv = new CsvFileObject($filename, $fileMode);
        } catch (\Exception $e) {
            throw new IOException($e->getMessage(), null, $e);
        }
        $csv->setNewline($newline);
        if ($toCharset) {
            $csv->setCsvFilter(function ($line) use ($toCharset, $fromCharset) {
                return mb_convert_encoding($line, $toCharset, $fromCharset);
            });
        }
        if (count($columnHeaders) > 0) {
            $this->checkRowConsistency($columnHeaders);
            $csv->fputcsv($columnHeaders, $delimiter, $enclosure);
        }
        foreach ($rows as $row) {
            $this->checkRowConsistency($row);
            $csv->fputcsv($row, $delimiter, $enclosure);
        }
        $csv->fflush();
    }
public function export ( $ filename , $ rows ) { $ this -> reset ( ) ; $ delimiter = $ this -> config -> getDelimiter ( ) ; $ enclosure = $ this -> config -> getEnclosure ( ) ; $ enclosure = empty ( $ enclosure ) ? " \0 " : $ enclosure ; $ newline = $ this -> config -> getNewline ( ) ; $ fromCharset = $ this -> config -> getFromCharset ( ) ; $ toCharset = $ this -> config -> getToCharset ( ) ; $ fileMode = $ this -> config -> getFileMode ( ) ; $ columnHeaders = $ this -> config -> getColumnHeaders ( ) ; try { $ csv = new CsvFileObject ( $ filename , $ fileMode ) ; } catch ( \ Exception $ e ) { throw new IOException ( $ e -> getMessage ( ) , null , $ e ) ; } $ csv -> setNewline ( $ newline ) ; if ( $ toCharset ) { $ csv -> setCsvFilter ( function ( $ line ) use ( $ toCharset , $ fromCharset ) { return mb_convert_encoding ( $ line , $ toCharset , $ fromCharset ) ; } ) ; } if ( count ( $ columnHeaders ) > 0 ) { $ this -> checkRowConsistency ( $ columnHeaders ) ; $ csv -> fputcsv ( $ columnHeaders , $ delimiter , $ enclosure ) ; } foreach ( $ rows as $ row ) { $ this -> checkRowConsistency ( $ row ) ; $ csv -> fputcsv ( $ row , $ delimiter , $ enclosure ) ; } $ csv -> fflush ( ) ; }
Export data as CSV file.
Export data as CSV file .
// reset the exporter // initialize the configuration // set the new line char // query whether we've to convert the charset // export the header // export the rows // flush the CSV file