1 | <?php |
||
18 | class SharedStringsManager |
||
19 | { |
||
20 | /** Main namespace for the sharedStrings.xml file */ |
||
21 | const MAIN_NAMESPACE_FOR_SHARED_STRINGS_XML = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; |
||
22 | |||
23 | /** Definition of XML nodes names used to parse data */ |
||
24 | const XML_NODE_SST = 'sst'; |
||
25 | const XML_NODE_SI = 'si'; |
||
26 | const XML_NODE_R = 'r'; |
||
27 | const XML_NODE_T = 't'; |
||
28 | |||
29 | /** Definition of XML attributes used to parse data */ |
||
30 | const XML_ATTRIBUTE_COUNT = 'count'; |
||
31 | const XML_ATTRIBUTE_UNIQUE_COUNT = 'uniqueCount'; |
||
32 | const XML_ATTRIBUTE_XML_SPACE = 'xml:space'; |
||
33 | const XML_ATTRIBUTE_VALUE_PRESERVE = 'preserve'; |
||
34 | |||
35 | /** @var string Path of the XLSX file being read */ |
||
36 | protected $filePath; |
||
37 | |||
38 | /** @var string Temporary folder where the temporary files to store shared strings will be stored */ |
||
39 | protected $tempFolder; |
||
40 | |||
41 | /** @var WorkbookRelationshipsManager Helps retrieving workbook relationships */ |
||
42 | protected $workbookRelationshipsManager; |
||
43 | |||
44 | /** @var EntityFactory Factory to create entities */ |
||
45 | protected $entityFactory; |
||
46 | |||
47 | /** @var HelperFactory $helperFactory Factory to create helpers */ |
||
48 | protected $helperFactory; |
||
49 | |||
50 | /** @var CachingStrategyFactory Factory to create shared strings caching strategies */ |
||
51 | protected $cachingStrategyFactory; |
||
52 | |||
53 | /** @var CachingStrategyInterface The best caching strategy for storing shared strings */ |
||
54 | protected $cachingStrategy; |
||
55 | |||
56 | /** |
||
57 | * @param string $filePath Path of the XLSX file being read |
||
58 | * @param string $tempFolder Temporary folder where the temporary files to store shared strings will be stored |
||
59 | * @param WorkbookRelationshipsManager $workbookRelationshipsManager Helps retrieving workbook relationships |
||
60 | * @param EntityFactory $entityFactory Factory to create entities |
||
61 | * @param HelperFactory $helperFactory Factory to create helpers |
||
62 | * @param CachingStrategyFactory $cachingStrategyFactory Factory to create shared strings caching strategies |
||
63 | */ |
||
64 | 46 | public function __construct( |
|
79 | |||
80 | /** |
||
81 | * Returns whether the XLSX file contains a shared strings XML file |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 40 | public function hasSharedStrings() |
|
89 | |||
90 | /** |
||
91 | * Builds an in-memory array containing all the shared strings of the sheet. |
||
92 | * All the strings are stored in a XML file, located at 'xl/sharedStrings.xml'. |
||
93 | * It is then accessed by the sheet data, via the string index in the built table. |
||
94 | * |
||
95 | * More documentation available here: http://msdn.microsoft.com/en-us/library/office/gg278314.aspx |
||
96 | * |
||
97 | * The XML file can be really big with sheets containing a lot of data. That is why |
||
98 | * we need to use a XML reader that provides streaming like the XMLReader library. |
||
99 | * |
||
100 | * @throws \Box\Spout\Common\Exception\IOException If shared strings XML file can't be read |
||
101 | * @return void |
||
102 | */ |
||
103 | 39 | public function extractSharedStrings() |
|
134 | |||
135 | /** |
||
136 | * Returns the shared strings unique count, as specified in <sst> tag. |
||
137 | * |
||
138 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader instance |
||
139 | * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml is invalid and can't be read |
||
140 | * @return int|null Number of unique shared strings in the sharedStrings.xml file |
||
141 | */ |
||
142 | 39 | protected function getSharedStringsUniqueCount($xmlReader) |
|
161 | |||
162 | /** |
||
163 | * Returns the best shared strings caching strategy. |
||
164 | * |
||
165 | * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown) |
||
166 | * @return CachingStrategyInterface |
||
167 | */ |
||
168 | 38 | protected function getBestSharedStringsCachingStrategy($sharedStringsUniqueCount) |
|
173 | |||
174 | /** |
||
175 | * Processes the shared strings item XML node which the given XML reader is positioned on. |
||
176 | * |
||
177 | * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XML Reader positioned on a "<si>" node |
||
178 | * @param int $sharedStringIndex Index of the processed shared strings item |
||
179 | * @return void |
||
180 | */ |
||
181 | 27 | protected function processSharedStringsItem($xmlReader, $sharedStringIndex) |
|
200 | |||
201 | /** |
||
202 | * Not all text nodes' values must be extracted. |
||
203 | * Some text nodes are part of a node describing the pronunciation for instance. |
||
204 | * We'll only consider the nodes whose parents are "<si>" or "<r>". |
||
205 | * |
||
206 | * @param \DOMElement $textNode Text node to check |
||
207 | * @return bool Whether the given text node's value must be extracted |
||
208 | */ |
||
209 | 27 | protected function shouldExtractTextNodeValue($textNode) |
|
215 | |||
216 | /** |
||
217 | * If the text node has the attribute 'xml:space="preserve"', then preserve whitespace. |
||
218 | * |
||
219 | * @param \DOMElement $textNode The text node element (<t>) whose whitespace may be preserved |
||
220 | * @return bool Whether whitespace should be preserved |
||
221 | */ |
||
222 | 27 | protected function shouldPreserveWhitespace($textNode) |
|
228 | |||
229 | /** |
||
230 | * Returns the shared string at the given index, using the previously chosen caching strategy. |
||
231 | * |
||
232 | * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
||
233 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index |
||
234 | * @return string The shared string at the given index |
||
235 | */ |
||
236 | 27 | public function getStringAtIndex($sharedStringIndex) |
|
240 | |||
241 | /** |
||
242 | * Destroys the cache, freeing memory and removing any created artifacts |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | 43 | public function cleanup() |
|
252 | } |
||
253 |