setName($filename); } return $this ->setName($filename) ->setHeader($header) ->setContent($records, $callback) ->getResponse() ; } /** * Set the name of the exported file * * @param string $name * * @return CsvExport */ public function setName($name) { if (substr($name, -4) == '.csv') { //$name = substr($name, 0, -4); } $this->name = $name; return $this; } /** * Set the header record * * @param array $record * * @return CsvExport */ public function setHeader($record) { $this->header = $record; return $this; } /** * Set the content records along with an optional callback to render them into CSV fields * * @param array|\Traversable $records * @param callable $callback * * @return CsvExport */ public function setContent($records, callable $callback = null) { $this->content = $records; $this->callback = $callback; return $this; } /** * Prepare the response with the CSV export and return it * * @return HttpResponse * @throws \Exception if any exceptions are thrown within the content callback */ public function getResponse() { if (method_exists($this->controller, 'getResponse')) { /** @var HttpResponse $response */ $response = $this->controller->getResponse(); } else { $response = new HttpResponse; } $fp = fopen('php://output', 'w'); ob_start(); fwrite($fp, implode(";",$this->header)."\n"); foreach ($this->content as $i => $item) { try { $fields = $this->callback ? call_user_func($this->callback, $item) : $item; if (!is_array($fields)) { throw new \RuntimeException('CsvExport can only accept arrays, '. gettype($fields) .' provided at index '. $i .'. Either use arrays when setting the records or use a callback to convert each records into an array.'); } fwrite($fp, implode(";",$fields)."\n"); //fputcsv($fp, $fields,";",chr(0)); } catch (\Exception $ex) { ob_end_clean(); throw $ex; } } fclose($fp); $response->setContent(ob_get_clean()); $response->getHeaders()->addHeaders(array( 'Content-Type' => 'application/octet-stream', 'Content-Disposition' => 'attachment;filename="'. str_replace('"', '\\"', $this->name) .'"', )); return $response; } }