| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Reader\XLSX; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Common\Exception\IOException; |
| 6 |
use FluentCart\OpenSpout\Common\Helper\GlobalFunctionsHelper; |
| 7 |
use FluentCart\OpenSpout\Common\Manager\OptionsManagerInterface; |
| 8 |
use FluentCart\OpenSpout\Reader\Common\Creator\InternalEntityFactoryInterface; |
| 9 |
use FluentCart\OpenSpout\Reader\Common\Entity\Options; |
| 10 |
use FluentCart\OpenSpout\Reader\ReaderAbstract; |
| 11 |
use FluentCart\OpenSpout\Reader\XLSX\Creator\InternalEntityFactory; |
| 12 |
use FluentCart\OpenSpout\Reader\XLSX\Creator\ManagerFactory; |
| 13 |
/** |
| 14 |
* This class provides support to read data from a XLSX file. |
| 15 |
*/ |
| 16 |
class Reader extends ReaderAbstract |
| 17 |
{ |
| 18 |
/** @var ManagerFactory */ |
| 19 |
protected $managerFactory; |
| 20 |
/** @var \ZipArchive */ |
| 21 |
protected $zip; |
| 22 |
/** @var \OpenSpout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */ |
| 23 |
protected $sharedStringsManager; |
| 24 |
/** @var SheetIterator To iterator over the XLSX sheets */ |
| 25 |
protected $sheetIterator; |
| 26 |
public function __construct(OptionsManagerInterface $optionsManager, GlobalFunctionsHelper $globalFunctionsHelper, InternalEntityFactoryInterface $entityFactory, ManagerFactory $managerFactory) |
| 27 |
{ |
| 28 |
parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory); |
| 29 |
$this->managerFactory = $managerFactory; |
| 30 |
} |
| 31 |
/** |
| 32 |
* @param string $tempFolder Temporary folder where the temporary files will be created |
| 33 |
* |
| 34 |
* @return Reader |
| 35 |
*/ |
| 36 |
public function setTempFolder($tempFolder) |
| 37 |
{ |
| 38 |
$this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder); |
| 39 |
return $this; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Returns whether stream wrappers are supported. |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
protected function doesSupportStreamWrapper() |
| 47 |
{ |
| 48 |
return \false; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Opens the file at the given file path to make it ready to be read. |
| 52 |
* It also parses the sharedStrings.xml file to get all the shared strings available in memory |
| 53 |
* and fetches all the available sheets. |
| 54 |
* |
| 55 |
* @param string $filePath Path of the file to be read |
| 56 |
* |
| 57 |
* @throws \OpenSpout\Common\Exception\IOException If the file at the given path or its content cannot be read |
| 58 |
* @throws \OpenSpout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file |
| 59 |
*/ |
| 60 |
protected function openReader($filePath) |
| 61 |
{ |
| 62 |
/** @var InternalEntityFactory $entityFactory */ |
| 63 |
$entityFactory = $this->entityFactory; |
| 64 |
$this->zip = $entityFactory->createZipArchive(); |
| 65 |
if (\true === $this->zip->open($filePath)) { |
| 66 |
$tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER); |
| 67 |
$this->sharedStringsManager = $this->managerFactory->createSharedStringsManager($filePath, $tempFolder, $entityFactory); |
| 68 |
if ($this->sharedStringsManager->hasSharedStrings()) { |
| 69 |
// Extracts all the strings from the sheets for easy access in the future |
| 70 |
$this->sharedStringsManager->extractSharedStrings(); |
| 71 |
} |
| 72 |
$this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager, $this->sharedStringsManager); |
| 73 |
} else { |
| 74 |
throw new IOException("Could not open {$filePath} for reading."); |
| 75 |
} |
| 76 |
} |
| 77 |
/** |
| 78 |
* Returns an iterator to iterate over sheets. |
| 79 |
* |
| 80 |
* @return SheetIterator To iterate over sheets |
| 81 |
*/ |
| 82 |
protected function getConcreteSheetIterator() |
| 83 |
{ |
| 84 |
return $this->sheetIterator; |
| 85 |
} |
| 86 |
/** |
| 87 |
* Closes the reader. To be used after reading the file. |
| 88 |
*/ |
| 89 |
protected function closeReader() |
| 90 |
{ |
| 91 |
if (null !== $this->zip) { |
| 92 |
$this->zip->close(); |
| 93 |
} |
| 94 |
if (null !== $this->sharedStringsManager) { |
| 95 |
$this->sharedStringsManager->cleanup(); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|