1 | <?php |
||
14 | abstract class AbstractReader implements ReaderInterface |
||
15 | { |
||
16 | /** @var bool Indicates whether the stream is currently open */ |
||
17 | protected $isStreamOpened = false; |
||
18 | |||
19 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
20 | protected $globalFunctionsHelper; |
||
21 | |||
22 | /** @var \Box\Spout\Reader\Common\ReaderOptions Reader's customized options */ |
||
23 | protected $options; |
||
24 | |||
25 | /** |
||
26 | * Returns the reader's current options |
||
27 | * |
||
28 | * @return \Box\Spout\Reader\Common\ReaderOptions |
||
29 | */ |
||
30 | abstract protected function getOptions(); |
||
31 | |||
32 | /** |
||
33 | * Returns whether stream wrappers are supported |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | abstract protected function doesSupportStreamWrapper(); |
||
38 | |||
39 | /** |
||
40 | * Opens the file at the given file path to make it ready to be read |
||
41 | * |
||
42 | * @param string $filePath Path of the file to be read |
||
43 | * @return void |
||
44 | */ |
||
45 | abstract protected function openReader($filePath); |
||
46 | |||
47 | /** |
||
48 | * Returns an iterator to iterate over sheets. |
||
49 | * |
||
50 | * @return \Iterator To iterate over sheets |
||
51 | */ |
||
52 | abstract protected function getConcreteSheetIterator(); |
||
53 | |||
54 | /** |
||
55 | * Closes the reader. To be used after reading the file. |
||
56 | * |
||
57 | * @return AbstractReader |
||
58 | */ |
||
59 | abstract protected function closeReader(); |
||
60 | |||
61 | /** |
||
62 | * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
||
63 | * @return AbstractReader |
||
64 | */ |
||
65 | 306 | public function setGlobalFunctionsHelper($globalFunctionsHelper) |
|
70 | |||
71 | /** |
||
72 | * Sets whether date/time values should be returned as PHP objects or be formatted as strings. |
||
73 | * |
||
74 | * @api |
||
75 | * @param bool $shouldFormatDates |
||
76 | * @return AbstractReader |
||
77 | */ |
||
78 | 186 | public function setShouldFormatDates($shouldFormatDates) |
|
83 | |||
84 | /** |
||
85 | * Sets whether empty rows should be returned or skipped. |
||
86 | * |
||
87 | * @api |
||
88 | * @param bool $shouldPreserveEmptyRows |
||
89 | * @return AbstractReader |
||
90 | */ |
||
91 | 249 | public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows) |
|
96 | |||
97 | /** |
||
98 | * Prepares the reader to read the given file. It also makes sure |
||
99 | * that the file exists and is readable. |
||
100 | * |
||
101 | * @api |
||
102 | * @param string $filePath Path of the file to be read |
||
103 | * @return void |
||
104 | * @throws \Box\Spout\Common\Exception\IOException If the file at the given path does not exist, is not readable or is corrupted |
||
105 | */ |
||
106 | 303 | public function open($filePath) |
|
107 | { |
||
108 | 303 | if ($this->isStreamWrapper($filePath) && (!$this->doesSupportStreamWrapper() || !$this->isSupportedStreamWrapper($filePath))) { |
|
109 | 15 | throw new IOException("Could not open $filePath for reading! Stream wrapper used is not supported for this type of file."); |
|
110 | } |
||
111 | |||
112 | 288 | if (!$this->isPhpStream($filePath)) { |
|
113 | // we skip the checks if the provided file path points to a PHP stream |
||
114 | 288 | if (!$this->globalFunctionsHelper->file_exists($filePath)) { |
|
115 | 9 | throw new IOException("Could not open $filePath for reading! File does not exist."); |
|
116 | 279 | } else if (!$this->globalFunctionsHelper->is_readable($filePath)) { |
|
117 | 3 | throw new IOException("Could not open $filePath for reading! File is not readable."); |
|
118 | } |
||
119 | 276 | } |
|
120 | |||
121 | try { |
||
122 | 276 | $fileRealPath = $this->getFileRealPath($filePath); |
|
123 | 276 | $this->openReader($fileRealPath); |
|
124 | 261 | $this->isStreamOpened = true; |
|
125 | 276 | } catch (\Exception $exception) { |
|
126 | 15 | throw new IOException("Could not open $filePath for reading! ({$exception->getMessage()})"); |
|
127 | } |
||
128 | 261 | } |
|
129 | |||
130 | /** |
||
131 | * Returns the real path of the given path. |
||
132 | * If the given path is a valid stream wrapper, returns the path unchanged. |
||
133 | * |
||
134 | * @param string $filePath |
||
135 | * @return string |
||
136 | */ |
||
137 | 276 | protected function getFileRealPath($filePath) |
|
146 | |||
147 | /** |
||
148 | * Returns the scheme of the custom stream wrapper, if the path indicates a stream wrapper is used. |
||
149 | * For example, php://temp => php, s3://path/to/file => s3... |
||
150 | * |
||
151 | * @param string $filePath Path of the file to be read |
||
152 | * @return string|null The stream wrapper scheme or NULL if not a stream wrapper |
||
153 | */ |
||
154 | 303 | protected function getStreamWrapperScheme($filePath) |
|
162 | |||
163 | /** |
||
164 | * Checks if the given path is an unsupported stream wrapper |
||
165 | * (like local path, php://temp, mystream://foo/bar...). |
||
166 | * |
||
167 | * @param string $filePath Path of the file to be read |
||
168 | * @return bool Whether the given path is an unsupported stream wrapper |
||
169 | */ |
||
170 | 303 | protected function isStreamWrapper($filePath) |
|
174 | |||
175 | /** |
||
176 | * Checks if the given path is an supported stream wrapper |
||
177 | * (like php://temp, mystream://foo/bar...). |
||
178 | * If the given path is a local path, returns true. |
||
179 | * |
||
180 | * @param string $filePath Path of the file to be read |
||
181 | * @return bool Whether the given path is an supported stream wrapper |
||
182 | */ |
||
183 | 279 | protected function isSupportedStreamWrapper($filePath) |
|
190 | |||
191 | /** |
||
192 | * Checks if a path is a PHP stream (like php://output, php://memory, ...) |
||
193 | * |
||
194 | * @param string $filePath Path of the file to be read |
||
195 | * @return bool Whether the given path maps to a PHP stream |
||
196 | */ |
||
197 | 288 | protected function isPhpStream($filePath) |
|
202 | |||
203 | /** |
||
204 | * Returns an iterator to iterate over sheets. |
||
205 | * |
||
206 | * @api |
||
207 | * @return \Iterator To iterate over sheets |
||
208 | * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException If called before opening the reader |
||
209 | */ |
||
210 | 264 | public function getSheetIterator() |
|
218 | |||
219 | /** |
||
220 | * Closes the reader, preventing any additional reading |
||
221 | * |
||
222 | * @api |
||
223 | * @return void |
||
224 | */ |
||
225 | 249 | public function close() |
|
238 | } |
||
239 |