Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit a8eb7ad

Browse files
authored
Shared strings table without uniqueCount and count should work (#269)
Use file based strategy in this case
1 parent ffea887 commit a8eb7ad

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function getInstance()
7878
* Returns the best caching strategy, given the number of unique shared strings
7979
* and the amount of memory available.
8080
*
81-
* @param int $sharedStringsUniqueCount Number of unique shared strings
81+
* @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown)
8282
* @param string|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored
8383
* @return CachingStrategyInterface The best caching strategy
8484
*/
@@ -95,11 +95,16 @@ public function getBestCachingStrategy($sharedStringsUniqueCount, $tempFolder =
9595
* Returns whether it is safe to use in-memory caching, given the number of unique shared strings
9696
* and the amount of memory available.
9797
*
98-
* @param int $sharedStringsUniqueCount Number of unique shared strings
98+
* @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown)
9999
* @return bool
100100
*/
101101
protected function isInMemoryStrategyUsageSafe($sharedStringsUniqueCount)
102102
{
103+
// if the number of shared strings in unknown, do not use "in memory" strategy
104+
if ($sharedStringsUniqueCount === null) {
105+
return false;
106+
}
107+
103108
$memoryAvailable = $this->getMemoryLimitInKB();
104109

105110
if ($memoryAvailable === -1) {

src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected function getSharedStringsFilePath()
147147
* Returns the shared strings unique count, as specified in <sst> tag.
148148
*
149149
* @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader instance
150-
* @return int Number of unique shared strings in the sharedStrings.xml file
150+
* @return int|null Number of unique shared strings in the sharedStrings.xml file
151151
* @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml is invalid and can't be read
152152
*/
153153
protected function getSharedStringsUniqueCount($xmlReader)
@@ -167,13 +167,13 @@ protected function getSharedStringsUniqueCount($xmlReader)
167167
$uniqueCount = $xmlReader->getAttribute('count');
168168
}
169169

170-
return intval($uniqueCount);
170+
return ($uniqueCount !== null) ? intval($uniqueCount) : null;
171171
}
172172

173173
/**
174174
* Returns the best shared strings caching strategy.
175175
*
176-
* @param int $sharedStringsUniqueCount
176+
* @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown)
177177
* @return CachingStrategyInterface
178178
*/
179179
protected function getBestSharedStringsCachingStrategy($sharedStringsUniqueCount)

tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase
1515
public function dataProviderForTestGetBestCachingStrategy()
1616
{
1717
return [
18+
[null, -1, 'FileBasedStrategy'],
1819
[CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE, -1, 'FileBasedStrategy'],
1920
[CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 10, -1, 'FileBasedStrategy'],
2021
[CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE - 10, -1, 'InMemoryStrategy'],
@@ -27,7 +28,7 @@ public function dataProviderForTestGetBestCachingStrategy()
2728
/**
2829
* @dataProvider dataProviderForTestGetBestCachingStrategy
2930
*
30-
* @param int $sharedStringsUniqueCount
31+
* @param int|null $sharedStringsUniqueCount
3132
* @param int $memoryLimitInKB
3233
* @param string $expectedStrategyClassName
3334
* @return void

tests/Spout/Reader/XLSX/ReaderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ public function testReadShouldSupportSheetWithSharedStringsMissingUniqueCountAtt
126126
$this->assertEquals($expectedRows, $allRows);
127127
}
128128

129+
/**
130+
* @return void
131+
*/
132+
public function testReadShouldSupportSheetWithSharedStringsMissingUniqueCountAndCountAttributes()
133+
{
134+
$allRows = $this->getAllRowsForFile('one_sheet_with_shared_strings_missing_unique_count_and_count.xlsx');
135+
136+
$expectedRows = [
137+
['s1--A1', 's1--B1'],
138+
['s1--A2', 's1--B2'],
139+
];
140+
$this->assertEquals($expectedRows, $allRows);
141+
}
142+
129143
/**
130144
* @return void
131145
*/
Binary file not shown.

0 commit comments

Comments
 (0)