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