Repository navigation
Expand file tree
/
Copy pathBaseWriter.php
More file actions
82 lines (72 loc) · 2.13 KB
/
Copy pathBaseWriter.php
File metadata and controls
82 lines (72 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Faisalman\SimpleExcel\Writer;
use Faisalman\SimpleExcel\Enums\SimpleExcelException;
use Faisalman\SimpleExcel\Spreadsheet\Workbook;
/**
* SimpleExcel base class for writing spreadsheet
*
* @author Faisal Salman
* @package Faisalman\SimpleExcel
*/
abstract class BaseWriter implements IWriter
{
/**
* Holds the workbook instance
*
* @access protected
* @var Workbook
*/
protected $workbook;
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'text';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'txt';
/**
* @param Workbook reference to workbook
*/
public function __construct(&$workbook){
$this->workbook = &$workbook;
}
/**
* @param string $target File pointer
* @param array $options Options
*/
public function exportFile ($target, $options = NULL) {
// check if target is browser
if ($is_browser = (PHP_SAPI != 'cli' && $target == 'php://output')) {
if (!isset($options['filename'])) {
$options['filename'] = date('Y-m-d-H-i-s');
}
// check if no extension is set
if (!preg_match('/\.\w+$/', $options['filename'])) {
$options['filename'] = $options['filename'] . '.' . $this->file_extension;
}
// set HTTP response header
header('Content-Type: '.$this->content_type);
header('Content-Disposition: attachment; filename='.$options['filename']);
}
$fp = fopen($target, 'w');
fwrite($fp, $this->toString());
fclose($fp);
// since there must be no data below exported document
if ($is_browser) {
exit();
}
}
/**
* @return string
*/
public function toString ($options = NULL) {
throw new \BadMethodCallException('Unimplemented method', SimpleExcelException::UNIMPLEMENTED_METHOD);
}
}