Test Setup Failed
Push — feature/issue-46-add-repeatabl... ( 599383 )
by Stefan
06:26
created

Writer::setPrintTitleRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Size\SizeCalculator;
6
use OneSheet\Style\Style;
7
use OneSheet\Style\Styler;
8
9
class Writer
10
{
11
    /**
12
     * @var SheetFile
13
     */
14
    private $sheetFile;
15
16
    /**
17
     * @var Styler
18
     */
19
    private $styler;
20
21
    /**
22
     * @var Sheet
23
     */
24
    private $sheet;
25
26
    /**
27
     * @var Workbook
28
     */
29
    private $workbook;
30
31
    /**
32
     * @var resource
33
     */
34
    private $output;
35
36
    /**
37
     * If a $fontsDirectory is supplied it will be scanned for usable ttf/otf fonts
38
     * to be used for cell auto-sizing. Keep in mind though - viewers of an excel
39
     * file have to have that font on their machine. XLSX does not embed fonts!
40
     *
41
     * @param string|null $fontsDirectory
42
     * @throws \Exception
43
     */
44
    public function __construct($fontsDirectory = null)
45
    {
46
        $this->sheetFile = new SheetFile();
47
        $this->sheetFile->fwrite(str_repeat(' ', 1024 * 1024) . '<sheetData>');
48
        $this->styler = new Styler();
49
        $this->sheet = new Sheet(new CellBuilder(), new SizeCalculator($fontsDirectory));
50
        $this->workbook = new Workbook();
51
    }
52
53
    /**
54
     * All cells _above_ this cell (e.g. A2) will be frozen/fixed.
55
     *
56
     * @param string $cellId
57
     */
58
    public function setFreezePaneCellId($cellId)
59
    {
60
        $this->sheet->setFreezePaneCellId($cellId);
61
    }
62
63
    /**
64
     * Set the range of rows to repeat at the top of each page when printing the
65
     * excel file. $startRow=1 and $endRow=1 will repeat the first row only.
66
     *
67
     * @param int $startRow
68
     * @param int $endRow
69
     */
70
    public function setPrintTitleRange($startRow, $endRow)
71
    {
72
        $this->workbook->setPrintTitleRange($startRow, $endRow);
73
    }
74
75
    /**
76
     * Set fixed column widths per cell (no ranges) and array index
77
     * 0 being the first column.
78
     * If used alongside cell autosizing, these should be set
79
     * after the last row has been added.
80
     *
81
     * @param array $columnWidths
82
     * @throws \InvalidArgumentException
83
     */
84
    public function setFixedColumnWidths(array $columnWidths)
85
    {
86
        $this->sheet->setFixedColumnWidths($columnWidths);
87
    }
88
89
    /**
90
     * Set lower and/or upper limits for column widths.
91
     *
92
     * @param float|null $minWidth
93
     * @param float|null $maxWidth
94
     */
95
    public function setColumnWidthLimits($minWidth = null, $maxWidth = null)
96
    {
97
        $this->sheet->setColumnWidthLimits($minWidth, $maxWidth);
98
    }
99
100
    /**
101
     * Start recording row specs for column auto-sizing.
102
     *
103
     * @return Writer
104
     */
105
    public function enableCellAutosizing()
106
    {
107
        $this->sheet->enableCellAutosizing();
108
        return $this;
109
    }
110
111
    /**
112
     * Stop recording row specs for column autosizing.
113
     *
114
     * @return Writer
115
     */
116
    public function disableCellAutosizing()
117
    {
118
        $this->sheet->disableCellAutosizing();
119
        return $this;
120
    }
121
122
    /**
123
     * Add multiple rows at once.
124
     *
125
     * @param array|\Traversable $rows
126
     * @param Style              $style
127
     * @throws \InvalidArgumentException
128
     */
129
    public function addRows($rows, Style $style = null)
130
    {
131
        if (!is_array($rows) && false === $rows instanceof \Traversable) {
132
            throw new \InvalidArgumentException('Expected array or traversable object as rows');
133
        }
134
135
        foreach ($rows as $row) {
136
            $this->addRow($row, $style);
137
        }
138
    }
139
140
    /**
141
     * Add a single new row to the sheet and supply an optional style.
142
     *
143
     * @param array $row
144
     * @param Style $style
145
     */
146
    public function addRow(array $row, Style $style = null)
147
    {
148
        if (!empty($row)) {
149
            $style = $style instanceof Style ? $style : $this->styler->getDefaultStyle();
150
            $this->styler->addStyle($style);
151
            $this->sheetFile->fwrite($this->sheet->addRow($row, $style));
152
        }
153
    }
154
155
    /**
156
     * Wrap things up and write xlsx.
157
     *
158
     * @param string $fileName
159
     */
160
    public function writeToFile($fileName = 'report.xlsx')
161
    {
162
        $this->output = fopen($fileName, 'w');
163
        $finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile, $this->workbook);
164
        $finalizer->finalize($this->output);
165
    }
166
167
    /**
168
     * Wrap things up and send xlsx to browser.
169
     *
170
     * @param string $fileName
171
     */
172
    public function writeToBrowser($fileName = 'report.xlsx')
173
    {
174
        $this->output = fopen('php://output', 'w');
175
        $finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile, $this->workbook);
176
        $this->sendHeaders($fileName);
177
        $finalizer->finalize($this->output);
178
    }
179
180
    /**
181
     * Send headers for browser output.
182
     *
183
     * @param string $fileName
184
     */
185
    private function sendHeaders($fileName)
186
    {
187
        if (!headers_sent()) {
188
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
189
            header('Content-Disposition: attachment; filename="' . $fileName . '"');
190
            header('Cache-Control: max-age=0');
191
            header('Pragma: public');
192
        }
193
    }
194
195
    /**
196
     * Return array of available fonts & paths as key value pairs.
197
     *
198
     * @return array
199
     */
200
    public function getFonts()
201
    {
202
        return $this->sheet->getFonts();
203
    }
204
}
205