/** * 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 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 testNewline() { $config = new ExporterConfig(); $this->assertSame("\r\n", $config->getNewline()); $this->assertSame("\r", $config->setNewline("\r")->getNewline()); }
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); }