diff --git a/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php b/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php index 8fffdb0..36e0bfe 100644 --- a/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php +++ b/src/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactory.php @@ -78,7 +78,7 @@ class CachingStrategyFactory * Returns the best caching strategy, given the number of unique shared strings * and the amount of memory available. * - * @param int $sharedStringsUniqueCount Number of unique shared strings + * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown) * @param string|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored * @return CachingStrategyInterface The best caching strategy */ @@ -95,11 +95,16 @@ class CachingStrategyFactory * Returns whether it is safe to use in-memory caching, given the number of unique shared strings * and the amount of memory available. * - * @param int $sharedStringsUniqueCount Number of unique shared strings + * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown) * @return bool */ protected function isInMemoryStrategyUsageSafe($sharedStringsUniqueCount) { + // if the number of shared strings in unknown, do not use "in memory" strategy + if ($sharedStringsUniqueCount === null) { + return false; + } + $memoryAvailable = $this->getMemoryLimitInKB(); if ($memoryAvailable === -1) { diff --git a/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php b/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php index 350024a..0f41e90 100644 --- a/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php +++ b/src/Spout/Reader/XLSX/Helper/SharedStringsHelper.php @@ -147,7 +147,7 @@ class SharedStringsHelper * Returns the shared strings unique count, as specified in tag. * * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader instance - * @return int Number of unique shared strings in the sharedStrings.xml file + * @return int|null Number of unique shared strings in the sharedStrings.xml file * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml is invalid and can't be read */ protected function getSharedStringsUniqueCount($xmlReader) @@ -167,13 +167,13 @@ class SharedStringsHelper $uniqueCount = $xmlReader->getAttribute('count'); } - return intval($uniqueCount); + return ($uniqueCount !== null) ? intval($uniqueCount) : null; } /** * Returns the best shared strings caching strategy. * - * @param int $sharedStringsUniqueCount + * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown) * @return CachingStrategyInterface */ protected function getBestSharedStringsCachingStrategy($sharedStringsUniqueCount) diff --git a/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php b/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php index ea77b4f..e61760a 100644 --- a/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php +++ b/tests/Spout/Reader/XLSX/Helper/SharedStringsCaching/CachingStrategyFactoryTest.php @@ -15,6 +15,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase public function dataProviderForTestGetBestCachingStrategy() { return [ + [null, -1, 'FileBasedStrategy'], [CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE, -1, 'FileBasedStrategy'], [CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE + 10, -1, 'FileBasedStrategy'], [CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE - 10, -1, 'InMemoryStrategy'], @@ -27,7 +28,7 @@ class CachingStrategyFactoryTest extends \PHPUnit_Framework_TestCase /** * @dataProvider dataProviderForTestGetBestCachingStrategy * - * @param int $sharedStringsUniqueCount + * @param int|null $sharedStringsUniqueCount * @param int $memoryLimitInKB * @param string $expectedStrategyClassName * @return void diff --git a/tests/Spout/Reader/XLSX/ReaderTest.php b/tests/Spout/Reader/XLSX/ReaderTest.php index 3fa6ec1..02c91ed 100644 --- a/tests/Spout/Reader/XLSX/ReaderTest.php +++ b/tests/Spout/Reader/XLSX/ReaderTest.php @@ -126,6 +126,20 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedRows, $allRows); } + /** + * @return void + */ + public function testReadShouldSupportSheetWithSharedStringsMissingUniqueCountAndCountAttributes() + { + $allRows = $this->getAllRowsForFile('one_sheet_with_shared_strings_missing_unique_count_and_count.xlsx'); + + $expectedRows = [ + ['s1--A1', 's1--B1'], + ['s1--A2', 's1--B2'], + ]; + $this->assertEquals($expectedRows, $allRows); + } + /** * @return void */ diff --git a/tests/resources/xlsx/one_sheet_with_shared_strings_missing_unique_count_and_count.xlsx b/tests/resources/xlsx/one_sheet_with_shared_strings_missing_unique_count_and_count.xlsx new file mode 100644 index 0000000..6ef7ffd Binary files /dev/null and b/tests/resources/xlsx/one_sheet_with_shared_strings_missing_unique_count_and_count.xlsx differ