Completed
Push — master ( 12b91f...1a2e50 )
by Stefan
04:30
created

Writer::addRows()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

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