Completed
Push — rendered-cell-sizes ( a9b1a2...a767c5 )
by Stefan
02:34
created

Writer::getFonts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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