| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Reader\ODS;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Common\Exception\IOException;
|
|
|
8 |
use OpenSpout\Common\Helper\Escaper\ODS;
|
|
|
9 |
use OpenSpout\Reader\AbstractReader;
|
|
|
10 |
use OpenSpout\Reader\Exception\NoSheetsFoundException;
|
|
|
11 |
use OpenSpout\Reader\ODS\Helper\SettingsHelper;
|
|
|
12 |
use ZipArchive;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* @extends AbstractReader<SheetIterator>
|
|
|
16 |
*/
|
|
|
17 |
final class Reader extends AbstractReader
|
|
|
18 |
{
|
|
|
19 |
private ZipArchive $zip;
|
|
|
20 |
|
|
|
21 |
private readonly Options $options;
|
|
|
22 |
|
|
|
23 |
/** @var SheetIterator To iterator over the ODS sheets */
|
|
|
24 |
private SheetIterator $sheetIterator;
|
|
|
25 |
|
|
|
26 |
public function __construct(?Options $options = null)
|
|
|
27 |
{
|
|
|
28 |
$this->options = $options ?? new Options();
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function getSheetIterator(): SheetIterator
|
|
|
32 |
{
|
|
|
33 |
$this->ensureStreamOpened();
|
|
|
34 |
|
|
|
35 |
return $this->sheetIterator;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Returns whether stream wrappers are supported.
|
|
|
40 |
*/
|
|
|
41 |
protected function doesSupportStreamWrapper(): bool
|
|
|
42 |
{
|
|
|
43 |
return false;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Opens the file at the given file path to make it ready to be read.
|
|
|
48 |
*
|
|
|
49 |
* @param string $filePath Path of the file to be read
|
|
|
50 |
*
|
|
|
51 |
* @throws IOException If the file at the given path or its content cannot be read
|
|
|
52 |
* @throws NoSheetsFoundException If there are no sheets in the file
|
|
|
53 |
*/
|
|
|
54 |
protected function openReader(string $filePath): void
|
|
|
55 |
{
|
|
|
56 |
$this->zip = new ZipArchive();
|
|
|
57 |
|
|
|
58 |
if (true !== $this->zip->open($filePath)) {
|
|
|
59 |
throw new IOException("Could not open {$filePath} for reading.");
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$this->sheetIterator = new SheetIterator($filePath, $this->options, new ODS(), new SettingsHelper());
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Closes the reader. To be used after reading the file.
|
|
|
67 |
*/
|
|
|
68 |
protected function closeReader(): void
|
|
|
69 |
{
|
|
|
70 |
$this->zip->close();
|
|
|
71 |
}
|
|
|
72 |
}
|