getFileName(); header('Expires: Sat, 01 Jan 2000 00:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-type: text/comma-separated-values'); header('Content-Disposition: attachment; filename="' . $this->_escapeAttachmentName($fileName) . '"'); return $this; } protected function _escapeAttachmentName($name) { $disabledCharacters = ['<', '>', '\\', '"', '/', ':', '|', '?', '*']; return str_replace($disabledCharacters, '', $name); } public function export() { $rowCallback = $this->getRowCallback(); $columns = $this->getColumns(); $fp = fopen('php://output', 'w'); $this->_sendHeaders(); fputcsv($fp, $columns); $blankRow = array_fill_keys(array_keys($columns), ''); while (($row = $rowCallback())) { $row = array_intersect_key($row, $columns); $row = array_merge($blankRow, $row); fputcsv($fp, $row); } fclose($fp); exit; } public function setColumns(array $columns) { $this->_columns = $columns; return $this; } public function getColumns() { if (null == $this->_columns) { throw new Exception('Required option is missing "' . __CLASS__ . '->_columns"'); } return $this->_columns; } public function setFileName($fileName) { $this->_fileName = Qs_String::fill($fileName, ['date' => '"' . date('m.d.Y')], '{}'); return $this; } public function getFileName() { if (null == $this->_fileName) { throw new Exception('Required option is missing "' . __CLASS__ . '->_fileName"'); } return $this->_fileName; } public function setRowCallback(Closure $rowCallback) { if (!$rowCallback instanceof Closure) { throw new Exception('Wrong callback'); } $this->_rowCallback = $rowCallback; return $this; } public function getRowCallback() { return $this->_rowCallback; } }