1 | <?php |
||
23 | abstract class WriterAbstract implements WriterInterface |
||
24 | { |
||
25 | /** @var string Path to the output file */ |
||
26 | protected $outputFilePath; |
||
27 | |||
28 | /** @var resource Pointer to the file/stream we will write to */ |
||
29 | protected $filePointer; |
||
30 | |||
31 | /** @var bool Indicates whether the writer has been opened or not */ |
||
32 | protected $isWriterOpened = false; |
||
33 | |||
34 | /** @var GlobalFunctionsHelper Helper to work with global functions */ |
||
35 | protected $globalFunctionsHelper; |
||
36 | |||
37 | /** @var HelperFactory $helperFactory */ |
||
38 | protected $helperFactory; |
||
39 | |||
40 | /** @var OptionsManagerInterface Writer options manager */ |
||
41 | protected $optionsManager; |
||
42 | |||
43 | /** @var StyleMerger Helps merge styles together */ |
||
44 | protected $styleMerger; |
||
45 | |||
46 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
47 | protected static $headerContentType; |
||
48 | |||
49 | /** |
||
50 | * @param OptionsManagerInterface $optionsManager |
||
51 | * @param StyleMerger $styleMerger |
||
52 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
53 | * @param HelperFactory $helperFactory |
||
54 | */ |
||
55 | 94 | public function __construct( |
|
56 | OptionsManagerInterface $optionsManager, |
||
57 | StyleMerger $styleMerger, |
||
58 | GlobalFunctionsHelper $globalFunctionsHelper, |
||
59 | HelperFactory $helperFactory |
||
60 | ) { |
||
61 | 94 | $this->optionsManager = $optionsManager; |
|
62 | 94 | $this->styleMerger = $styleMerger; |
|
63 | 94 | $this->globalFunctionsHelper = $globalFunctionsHelper; |
|
64 | 94 | $this->helperFactory = $helperFactory; |
|
65 | 94 | } |
|
66 | |||
67 | /** |
||
68 | * Opens the streamer and makes it ready to accept data. |
||
69 | * |
||
70 | * @throws IOException If the writer cannot be opened |
||
71 | * @return void |
||
72 | */ |
||
73 | abstract protected function openWriter(); |
||
74 | |||
75 | /** |
||
76 | * Adds a row to the currently opened writer. |
||
77 | * |
||
78 | * @param Row $row The row containing cells and styles |
||
79 | * @throws WriterNotOpenedException If the workbook is not created yet |
||
80 | * @throws IOException If unable to write data |
||
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 | * {@inheritdoc} |
||
94 | */ |
||
95 | 2 | public function setDefaultRowStyle(Style $defaultStyle) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 83 | public function openToFile($outputFilePath) |
|
117 | |||
118 | /** |
||
119 | * @codeCoverageIgnore |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function openToBrowser($outputFileName) |
||
152 | |||
153 | /** |
||
154 | * Checks if the pointer to the file/stream to write to is available. |
||
155 | * Will throw an exception if not available. |
||
156 | * |
||
157 | * @throws IOException If the pointer is not available |
||
158 | * @return void |
||
159 | */ |
||
160 | 83 | protected function throwIfFilePointerIsNotAvailable() |
|
166 | |||
167 | /** |
||
168 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
169 | * Throws an exception if already opened. |
||
170 | * |
||
171 | * @param string $message Error message |
||
172 | * @throws WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
173 | * @return void |
||
174 | */ |
||
175 | 49 | protected function throwIfWriterAlreadyOpened($message) |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 75 | public function addRow(Row $row) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 63 | public function addRows(array $rows) |
|
225 | |||
226 | /** |
||
227 | * @TODO: Move this into styleMerger |
||
228 | * |
||
229 | * @param Row $row |
||
230 | * @return $this |
||
231 | */ |
||
232 | 65 | private function applyDefaultRowStyle(Row $row) |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 74 | public function close() |
|
260 | |||
261 | /** |
||
262 | * Closes the writer and attempts to cleanup all files that were |
||
263 | * created during the writing process (temp files & final file). |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
279 | } |
||
280 |