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 HelperFactory $helperFactory */ |
||
40 | protected $helperFactory; |
||
41 | |||
42 | /** @var OptionsManagerInterface Writer options manager */ |
||
43 | protected $optionsManager; |
||
44 | |||
45 | /** @var StyleMerger Helps merge styles together */ |
||
46 | protected $styleMerger; |
||
47 | |||
48 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
49 | protected static $headerContentType; |
||
50 | |||
51 | /** |
||
52 | * @param OptionsManagerInterface $optionsManager |
||
53 | * @param StyleMerger $styleMerger |
||
54 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
55 | * @param HelperFactory $helperFactory |
||
56 | */ |
||
57 | 107 | public function __construct( |
|
68 | |||
69 | /** |
||
70 | * Opens the streamer and makes it ready to accept data. |
||
71 | * |
||
72 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
73 | * @return void |
||
74 | */ |
||
75 | abstract protected function openWriter(); |
||
76 | |||
77 | /** |
||
78 | * Adds a row to the currently opened writer. |
||
79 | * |
||
80 | * @param Row $row The row containing cells and styles |
||
81 | * @return void |
||
82 | */ |
||
83 | abstract protected function addRowToWriter(Row $row); |
||
84 | |||
85 | /** |
||
86 | * Closes the streamer, preventing any additional writing. |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | abstract protected function closeWriter(); |
||
91 | |||
92 | /** |
||
93 | * Sets the default styles for all rows added with "addRow" |
||
94 | * |
||
95 | * @param Style $defaultStyle |
||
96 | * @return WriterAbstract |
||
97 | */ |
||
98 | 2 | public function setDefaultRowStyle($defaultStyle) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 96 | public function openToFile($outputFilePath) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function openToBrowser($outputFileName) |
||
154 | |||
155 | /** |
||
156 | * Checks if the pointer to the file/stream to write to is available. |
||
157 | * Will throw an exception if not available. |
||
158 | * |
||
159 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
160 | * @return void |
||
161 | */ |
||
162 | 96 | protected function throwIfFilePointerIsNotAvailable() |
|
168 | |||
169 | /** |
||
170 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
171 | * Throws an exception if already opened. |
||
172 | * |
||
173 | * @param string $message Error message |
||
174 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
175 | * @return void |
||
176 | */ |
||
177 | 52 | protected function throwIfWriterAlreadyOpened($message) |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 80 | public function addRow(Row $row) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | public function withRow(\Closure $callback) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 62 | public function addRows(array $dataRows) |
|
233 | |||
234 | /** |
||
235 | * @param array $dataRow |
||
236 | * @param Style|null $style |
||
237 | * @return Row |
||
238 | */ |
||
239 | protected function createRowFromArray(array $dataRow, Style $style = null) |
||
255 | |||
256 | /** |
||
257 | * @TODO: Move this into styleMerger |
||
258 | * |
||
259 | * @param Row $row |
||
260 | * @return $this |
||
261 | */ |
||
262 | 69 | private function applyDefaultRowStyle(Row $row) |
|
271 | |||
272 | /** |
||
273 | * Closes the writer. This will close the streamer as well, preventing new data |
||
274 | * to be written to the file. |
||
275 | * |
||
276 | * @api |
||
277 | * @return void |
||
278 | */ |
||
279 | 79 | public function close() |
|
293 | |||
294 | /** |
||
295 | * Closes the writer and attempts to cleanup all files that were |
||
296 | * created during the writing process (temp files & final file). |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
312 | } |
||
313 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: