Пример #1
0
 /**
  * {@inherit}
  * @throws StrictViolationException
  */
 public function export($filename, $rows)
 {
     $delimiter = $this->config->getDelimiter();
     $enclosure = $this->config->getEnclosure();
     $enclosure = empty($enclosure) ? "" : $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();
 }
Пример #2
0
 public function testNewline()
 {
     $config = new ExporterConfig();
     $this->assertSame("\r\n", $config->getNewline());
     $this->assertSame("\r", $config->setNewline("\r")->getNewline());
 }