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