MDL-56012 lib: Update spout to version 2.6.0
[moodle.git] / lib / spout / src / Spout / Reader / AbstractReader.php
blobcb476abab3463fa11251f6951cbc50838d01ed4c
1 <?php
3 namespace Box\Spout\Reader;
5 use Box\Spout\Common\Exception\IOException;
6 use Box\Spout\Reader\Exception\ReaderNotOpenedException;
8 /**
9 * Class AbstractReader
11 * @package Box\Spout\Reader
12 * @abstract
14 abstract class AbstractReader implements ReaderInterface
16 /** @var bool Indicates whether the stream is currently open */
17 protected $isStreamOpened = false;
19 /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
20 protected $globalFunctionsHelper;
22 /** @var bool Whether date/time values should be returned as PHP objects or be formatted as strings */
23 protected $shouldFormatDates = false;
25 /**
26 * Returns whether stream wrappers are supported
28 * @return bool
30 abstract protected function doesSupportStreamWrapper();
32 /**
33 * Opens the file at the given file path to make it ready to be read
35 * @param string $filePath Path of the file to be read
36 * @return void
38 abstract protected function openReader($filePath);
40 /**
41 * Returns an iterator to iterate over sheets.
43 * @return \Iterator To iterate over sheets
45 abstract public function getConcreteSheetIterator();
47 /**
48 * Closes the reader. To be used after reading the file.
50 * @return AbstractReader
52 abstract protected function closeReader();
54 /**
55 * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
56 * @return AbstractReader
58 public function setGlobalFunctionsHelper($globalFunctionsHelper)
60 $this->globalFunctionsHelper = $globalFunctionsHelper;
61 return $this;
64 /**
65 * Sets whether date/time values should be returned as PHP objects or be formatted as strings.
67 * @param bool $shouldFormatDates
68 * @return AbstractReader
70 public function setShouldFormatDates($shouldFormatDates)
72 $this->shouldFormatDates = $shouldFormatDates;
73 return $this;
76 /**
77 * Prepares the reader to read the given file. It also makes sure
78 * that the file exists and is readable.
80 * @api
81 * @param string $filePath Path of the file to be read
82 * @return void
83 * @throws \Box\Spout\Common\Exception\IOException If the file at the given path does not exist, is not readable or is corrupted
85 public function open($filePath)
87 if ($this->isStreamWrapper($filePath) && (!$this->doesSupportStreamWrapper() || !$this->isSupportedStreamWrapper($filePath))) {
88 throw new IOException("Could not open $filePath for reading! Stream wrapper used is not supported for this type of file.");
91 if (!$this->isPhpStream($filePath)) {
92 // we skip the checks if the provided file path points to a PHP stream
93 if (!$this->globalFunctionsHelper->file_exists($filePath)) {
94 throw new IOException("Could not open $filePath for reading! File does not exist.");
95 } else if (!$this->globalFunctionsHelper->is_readable($filePath)) {
96 throw new IOException("Could not open $filePath for reading! File is not readable.");
100 try {
101 $fileRealPath = $this->getFileRealPath($filePath);
102 $this->openReader($fileRealPath);
103 $this->isStreamOpened = true;
104 } catch (\Exception $exception) {
105 throw new IOException("Could not open $filePath for reading! ({$exception->getMessage()})");
110 * Returns the real path of the given path.
111 * If the given path is a valid stream wrapper, returns the path unchanged.
113 * @param string $filePath
114 * @return string
116 protected function getFileRealPath($filePath)
118 if ($this->isSupportedStreamWrapper($filePath)) {
119 return $filePath;
122 // Need to use realpath to fix "Can't open file" on some Windows setup
123 return realpath($filePath);
127 * Returns the scheme of the custom stream wrapper, if the path indicates a stream wrapper is used.
128 * For example, php://temp => php, s3://path/to/file => s3...
130 * @param string $filePath Path of the file to be read
131 * @return string|null The stream wrapper scheme or NULL if not a stream wrapper
133 protected function getStreamWrapperScheme($filePath)
135 $streamScheme = null;
136 if (preg_match('/^(\w+):\/\//', $filePath, $matches)) {
137 $streamScheme = $matches[1];
139 return $streamScheme;
143 * Checks if the given path is an unsupported stream wrapper
144 * (like local path, php://temp, mystream://foo/bar...).
146 * @param string $filePath Path of the file to be read
147 * @return bool Whether the given path is an unsupported stream wrapper
149 protected function isStreamWrapper($filePath)
151 return ($this->getStreamWrapperScheme($filePath) !== null);
155 * Checks if the given path is an supported stream wrapper
156 * (like php://temp, mystream://foo/bar...).
157 * If the given path is a local path, returns true.
159 * @param string $filePath Path of the file to be read
160 * @return bool Whether the given path is an supported stream wrapper
162 protected function isSupportedStreamWrapper($filePath)
164 $streamScheme = $this->getStreamWrapperScheme($filePath);
165 return ($streamScheme !== null) ?
166 in_array($streamScheme, $this->globalFunctionsHelper->stream_get_wrappers()) :
167 true;
171 * Checks if a path is a PHP stream (like php://output, php://memory, ...)
173 * @param string $filePath Path of the file to be read
174 * @return bool Whether the given path maps to a PHP stream
176 protected function isPhpStream($filePath)
178 $streamScheme = $this->getStreamWrapperScheme($filePath);
179 return ($streamScheme === 'php');
183 * Returns an iterator to iterate over sheets.
185 * @api
186 * @return \Iterator To iterate over sheets
187 * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException If called before opening the reader
189 public function getSheetIterator()
191 if (!$this->isStreamOpened) {
192 throw new ReaderNotOpenedException('Reader should be opened first.');
195 return $this->getConcreteSheetIterator();
199 * Closes the reader, preventing any additional reading
201 * @api
202 * @return void
204 public function close()
206 if ($this->isStreamOpened) {
207 $this->closeReader();
209 $sheetIterator = $this->getConcreteSheetIterator();
210 if ($sheetIterator) {
211 $sheetIterator->end();
214 $this->isStreamOpened = false;