1 | <?php |
||
25 | abstract class WriterAbstract implements WriterInterface |
||
26 | { |
||
27 | /** @var string Path to the output file */ |
||
28 | protected $outputFilePath; |
||
29 | |||
30 | /** @var resource Pointer to the file/stream we will write to */ |
||
31 | protected $filePointer; |
||
32 | |||
33 | /** @var bool Indicates whether the writer has been opened or not */ |
||
34 | protected $isWriterOpened = false; |
||
35 | |||
36 | /** @var GlobalFunctionsHelper Helper to work with global functions */ |
||
37 | protected $globalFunctionsHelper; |
||
38 | |||
39 | /** @var OptionsManagerInterface Writer options manager */ |
||
40 | protected $optionsManager; |
||
41 | |||
42 | /** @var StyleMerger Helps merge styles together */ |
||
43 | protected $styleMerger; |
||
44 | |||
45 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
46 | protected static $headerContentType; |
||
47 | |||
48 | /** |
||
49 | * @param OptionsManagerInterface $optionsManager |
||
50 | * @param StyleMerger $styleMerger |
||
51 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
52 | */ |
||
53 | 100 | public function __construct( |
|
63 | |||
64 | /** |
||
65 | * Opens the streamer and makes it ready to accept data. |
||
66 | * |
||
67 | * @return void |
||
68 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
69 | */ |
||
70 | abstract protected function openWriter(); |
||
71 | |||
72 | /** |
||
73 | * Adds a row to the currently opened writer. |
||
74 | * |
||
75 | * @param Row $row The row containing cells and styles |
||
76 | * @return void |
||
77 | */ |
||
78 | abstract protected function addRowToWriter(Row $row); |
||
79 | |||
80 | /** |
||
81 | * Closes the streamer, preventing any additional writing. |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | abstract protected function closeWriter(); |
||
86 | |||
87 | /** |
||
88 | * Sets the default styles for all rows added with "addRow" |
||
89 | * |
||
90 | * @param Style $defaultStyle |
||
91 | * @return WriterAbstract |
||
92 | */ |
||
93 | public function setDefaultRowStyle($defaultStyle) |
||
94 | { |
||
95 | $this->optionsManager->setOption(Options::DEFAULT_ROW_STYLE, $defaultStyle); |
||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 89 | public function openToFile($outputFilePath) |
|
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function openToBrowser($outputFileName) |
||
148 | |||
149 | /** |
||
150 | * Checks if the pointer to the file/stream to write to is available. |
||
151 | * Will throw an exception if not available. |
||
152 | * |
||
153 | * @return void |
||
154 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
155 | */ |
||
156 | 89 | protected function throwIfFilePointerIsNotAvailable() |
|
162 | |||
163 | /** |
||
164 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
165 | * Throws an exception if already opened. |
||
166 | * |
||
167 | * @param string $message Error message |
||
168 | * @return void |
||
169 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
170 | */ |
||
171 | 47 | protected function throwIfWriterAlreadyOpened($message) |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 1 | public function addRow(Row $row) |
|
182 | { |
||
183 | 1 | if ($this->isWriterOpened) { |
|
184 | 1 | if (!$row->isEmpty()) { |
|
185 | try { |
||
186 | $this->applyDefaultRowStyle($row); |
||
187 | $this->addRowToWriter($row); |
||
188 | } catch (SpoutException $e) { |
||
189 | // if an exception occurs while writing data, |
||
190 | // close the writer and remove all files created so far. |
||
191 | $this->closeAndAttemptToCleanupAllFiles(); |
||
192 | // re-throw the exception to alert developers of the error |
||
193 | 1 | throw $e; |
|
194 | } |
||
195 | } |
||
196 | } else { |
||
197 | throw new WriterNotOpenedException('The writer needs to be opened before adding row.'); |
||
198 | } |
||
199 | 1 | return $this; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | public function withRow(\Closure $callback) |
||
209 | |||
210 | /** |
||
211 | * @inheritdoc |
||
212 | */ |
||
213 | 3 | public function addRows(array $dataRows) |
|
214 | { |
||
215 | 3 | foreach ($dataRows as $dataRow) { |
|
216 | |||
217 | 3 | if(!$dataRow instanceof Row) { |
|
218 | 3 | $this->closeAndAttemptToCleanupAllFiles(); |
|
219 | 3 | throw new InvalidArgumentException(); |
|
220 | } |
||
221 | |||
222 | 1 | $this->addRow($dataRow); |
|
223 | } |
||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param array $dataRow |
||
229 | * @param Style|null $style |
||
230 | * @return Row |
||
231 | */ |
||
232 | protected function createRowFromArray(array $dataRow, Style $style = null) |
||
247 | |||
248 | /** |
||
249 | * @TODO: Move this into styleMerger |
||
250 | * |
||
251 | * @param Row $row |
||
252 | * @return $this |
||
253 | */ |
||
254 | private function applyDefaultRowStyle(Row $row) |
||
255 | { |
||
256 | $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); |
||
257 | if (null === $defaultRowStyle) { |
||
258 | return $this; |
||
259 | } |
||
260 | $mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle); |
||
261 | $row->setStyle($mergedStyle); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Closes the writer. This will close the streamer as well, preventing new data |
||
266 | * to be written to the file. |
||
267 | * |
||
268 | * @api |
||
269 | * @return void |
||
270 | */ |
||
271 | 10 | public function close() |
|
285 | |||
286 | /** |
||
287 | * Closes the writer and attempts to cleanup all files that were |
||
288 | * created during the writing process (temp files & final file). |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | 3 | private function closeAndAttemptToCleanupAllFiles() |
|
304 | } |
||
305 |
This check looks for function calls that miss required arguments.