diff --git a/src/Spout/Reader/XLSX/Manager/StyleManager.php b/src/Spout/Reader/XLSX/Manager/StyleManager.php index 9232ef1..193324d 100644 --- a/src/Spout/Reader/XLSX/Manager/StyleManager.php +++ b/src/Spout/Reader/XLSX/Manager/StyleManager.php @@ -48,7 +48,10 @@ class StyleManager /** @var string Path of the XLSX file being read */ protected $filePath; - /** @var string Path of the styles XML file */ + /** @var bool Whether the XLSX file contains a styles XML file */ + protected $hasStylesXMLFile; + + /** @var string|null Path of the styles XML file */ protected $stylesXMLFilePath; /** @var InternalEntityFactory Factory to create entities */ @@ -76,7 +79,10 @@ class StyleManager $this->filePath = $filePath; $this->entityFactory = $entityFactory; $this->builtinNumFmtIdIndicatingDates = array_keys(self::$builtinNumFmtIdToNumFormatMapping); - $this->stylesXMLFilePath = $workbookRelationshipsManager->getStylesXMLFilePath(); + $this->hasStylesXMLFile = $workbookRelationshipsManager->hasStylesXMLFile(); + if ($this->hasStylesXMLFile) { + $this->stylesXMLFilePath = $workbookRelationshipsManager->getStylesXMLFilePath(); + } } /** @@ -88,6 +94,10 @@ class StyleManager */ public function shouldFormatNumericValueAsDate($styleId) { + if (!$this->hasStylesXMLFile) { + return false; + } + $stylesAttributes = $this->getStylesAttributes(); // Default style (0) does not format numeric values as timestamps. Only custom styles do. diff --git a/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php b/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php index 2a9b4e0..68d8709 100644 --- a/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php +++ b/src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php @@ -75,7 +75,17 @@ class WorkbookRelationshipsManager } /** - * @return string|null The path of the styles XML file + * @return bool Whether the XLSX file contains a styles XML file + */ + public function hasStylesXMLFile() + { + $workbookRelationships = $this->getWorkbookRelationships(); + + return isset($workbookRelationships[self::RELATIONSHIP_TYPE_STYLES]); + } + + /** + * @return string The path of the styles XML file */ public function getStylesXMLFilePath() { diff --git a/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php index a8aa190..794f4bc 100644 --- a/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php +++ b/tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php @@ -19,6 +19,7 @@ class StyleManagerTest extends TestCase { $entityFactory = $this->createMock(InternalEntityFactory::class); $workbookRelationshipsManager = $this->createMock(WorkbookRelationshipsManager::class); + $workbookRelationshipsManager->method('hasStylesXMLFile')->willReturn(true); /** @var StyleManager $styleManager */ $styleManager = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\StyleManager') diff --git a/tests/Spout/Reader/XLSX/ReaderTest.php b/tests/Spout/Reader/XLSX/ReaderTest.php index e5f5b6e..51a7b0e 100644 --- a/tests/Spout/Reader/XLSX/ReaderTest.php +++ b/tests/Spout/Reader/XLSX/ReaderTest.php @@ -473,6 +473,19 @@ class ReaderTest extends TestCase $this->assertEquals($expectedRows, $allRows); } + /** + * @return void + */ + public function testReadShouldSupportMissingStylesXMLFile() + { + $allRows = $this->getAllRowsForFile('file_with_no_styles_in_workbook_xml.xlsx'); + + $expectedRows = [ + ['s1--A1', 's1--B1'], + ]; + $this->assertEquals($expectedRows, $allRows); + } + /** * @return void */ diff --git a/tests/resources/xlsx/file_with_no_styles_in_workbook_xml.xlsx b/tests/resources/xlsx/file_with_no_styles_in_workbook_xml.xlsx new file mode 100644 index 0000000..aea1af5 Binary files /dev/null and b/tests/resources/xlsx/file_with_no_styles_in_workbook_xml.xlsx differ