示例#1
0
文件: Exporter.php 项目: Exquance/csv
 /**
  * {@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 testEnclosure()
 {
     $config = new ExporterConfig();
     $this->assertSame('"', $config->getEnclosure());
     $this->assertSame('enc', $config->setEnclosure('enc')->getEnclosure());
 }