public function register(Application $app) { $app['csv.exporter.config'] = $app->share(function () { $config = new ExporterConfig(); return $config->setDelimiter(";")->setEnclosure('"')->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8'); }); $app['csv.exporter'] = $app->share(function ($app) { return new Exporter($app['csv.exporter.config']); }); $app['csv.lexer.config'] = $app->share(function ($app) { $lexer = new LexerConfig(); $lexer->setDelimiter(';')->setEnclosure('"')->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8'); return $lexer; }); $app['csv.lexer'] = $app->share(function ($app) { return new Lexer($app['csv.lexer.config']); }); $app['csv.interpreter'] = $app->share(function ($app) { return new Interpreter(); }); $app['csv.response'] = $app->protect(function ($callback) use($app) { // set headers to fix ie issues $response = new StreamedResponse($callback, 200, ['Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', 'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Cache-Control' => 'post-check=0, pre-check=0', 'Pragma' => 'no-cache', 'Content-Type' => 'text/csv', 'Cache-Control' => 'max-age=3600, must-revalidate', 'Content-Disposition' => 'max-age=3600, must-revalidate']); $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'export.csv')); }); }
public function testColumnHeaders() { $columnHeaders = array('Header 1', 'Header 2', 'Header 3'); $config = new ExporterConfig(); $this->assertSame(array(), $config->getColumnHeaders()); $this->assertSame($columnHeaders, $config->setColumnHeaders($columnHeaders)->getColumnHeaders()); }
public function __construct($host, $database, $user, $pass) { $this->database = new \PDO("mysql:host={$host};dbname={$database}", $user, $pass); $config = new ExporterConfig(); $config->setDelimiter(";")->setEnclosure("'")->setEscape("\\")->setToCharset('SJIS-win')->setFromCharset('UTF-8')->setFileMode(CsvFileObject::FILE_MODE_WRITE); $this->exporter = new Exporter($config); }
/** * {@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(); }
/** * @param array $attributes array( '中文描述' => 'key' ); * @param string $title * @param array $data * @param string $fromCharSet * @param string $toCharSet * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function export_csv(array $attributes, $title = 'csv-data-dump', $data = array(), $fromCharSet = 'UTF-8', $toCharSet = 'UTF-8') { $title .= '-' . date('Ymd-His'); $config = new ExporterConfig(); $config->setDelimiter(';')->setEnclosure("'")->setEscape("\\")->setFromCharset($fromCharSet)->setToCharset($toCharSet); $head = array([], []); foreach ($attributes as $key => $val) { $head[0][] = $key; $head[1][] = $val; } $data = array_merge($head, $data); $headers = array('Content-type' => "application/csv; filename=\"{$title}.csv\"", 'Content-Disposition' => "attachement; filename=\"{$title}.csv\"", 'Cache-Control' => "no-cache"); $response = \Response::stream(function () use($config, $data) { $exporter = new Exporter($config); $exporter->export('php://output', $data); }, 200, $headers); return $response; }
/** * Handles response for csv-request. * * @param ViewHandler $handler * @param View $view * @param Request $request * @param string $format * * @return Response * * @throws ObjectNotSupportedException */ public function createResponse(ViewHandler $handler, View $view, Request $request, $format) { if (!$view->getData() instanceof ListRepresentation) { throw new ObjectNotSupportedException($view); } $viewData = $view->getData(); $data = new CallbackCollection($viewData->getData(), [$this, 'prepareData']); $fileName = sprintf('%s.csv', $viewData->getRel()); $config = new ExporterConfig(); $exporter = new Exporter($config); $data->rewind(); if ($row = $data->current()) { $config->setColumnHeaders(array_keys($row)); } $config->setDelimiter($this->convertValue($request->get('delimiter', ';'), self::$delimiterMap)); $config->setNewline($this->convertValue($request->get('newLine', '\\n'), self::$newLineMap)); $config->setEnclosure($request->get('enclosure', '"')); $config->setEscape($request->get('escape', '\\')); $response = new StreamedResponse(); $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $fileName, $fileName); $response->headers->set('Content-Type', 'text/csv'); $response->headers->set('Content-Disposition', $disposition); $response->setCallback(function () use($data, $exporter) { $exporter->export('php://output', $data); }); $response->send(); return $response; }
public function testStaticCreateWithSetExporterConfig() { $response = CsvResponse::create(array(array('1', 'alice', '*****@*****.**'), array('2', 'bob', '*****@*****.**'), array('3', 'carol', '*****@*****.**'))); $config = new ExporterConfig(); $config->setDelimiter("\t"); $response->setExporterConfig($config); ob_start(); $response->sendContent(); $content = ob_get_contents(); ob_end_clean(); $this->assertContains("1\talice\talice@example.com", $content); $this->assertContains("2\tbob\tbob@example.com", $content); $this->assertContains("3\tcarol\tcarol@example.com", $content); }
public function testToCharset() { $config = new ExporterConfig(); $this->assertSame(null, $config->getToCharset()); $this->assertSame('UTF-8', $config->setToCharset('UTF-8')->getToCharset()); }
public function test_multiple_line_columns() { $csv = 'vfs://output/multiple-lines.csv'; $this->assertFileNotExists($csv); $config = new ExporterConfig(); $config->setNewline("\r\n"); $exporter = new Exporter($config); $exporter->export($csv, array(array("line1\r\nline2\r\nline3", "single-line"), array("line1\r\nline2\r\nline3", "single-line"), array("line1\r\nline2\r\nline3", "single-line"))); $this->assertFileEquals(__DIR__ . '/csv_files/multiple-lines.csv', $csv); }
public function test_unseekable_wrapper_and_custom_newline_code() { $config = new ExporterConfig(); $config->setNewline("\r\n"); $exporter = new Exporter($config); ob_start(); $exporter->export('php://output', array(array('a', 'b', 'c'), array('1', '2', '3'))); $output = ob_get_clean(); $expectedCount = "a,b,c\r\n1,2,3\r\n"; $this->assertSame($expectedCount, $output); }