diff --git a/src/Spout/Writer/ODS/Creator/ManagerFactory.php b/src/Spout/Writer/ODS/Creator/ManagerFactory.php index f38c500..cf7877f 100644 --- a/src/Spout/Writer/ODS/Creator/ManagerFactory.php +++ b/src/Spout/Writer/ODS/Creator/ManagerFactory.php @@ -93,7 +93,7 @@ class ManagerFactory implements ManagerFactoryInterface { $styleRegistry = $this->createStyleRegistry($optionsManager); - return new StyleManager($styleRegistry); + return new StyleManager($styleRegistry, $optionsManager); } /** diff --git a/src/Spout/Writer/ODS/Manager/OptionsManager.php b/src/Spout/Writer/ODS/Manager/OptionsManager.php index a6fb564..698108a 100644 --- a/src/Spout/Writer/ODS/Manager/OptionsManager.php +++ b/src/Spout/Writer/ODS/Manager/OptionsManager.php @@ -34,6 +34,9 @@ class OptionsManager extends OptionsManagerAbstract Options::TEMP_FOLDER, Options::DEFAULT_ROW_STYLE, Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + Options::DEFAULT_COLUMN_WIDTH, + Options::DEFAULT_ROW_HEIGHT, + Options::COLUMN_WIDTHS, ]; } diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index 32500ab..93ed008 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -4,6 +4,8 @@ namespace Box\Spout\Writer\ODS\Manager\Style; use Box\Spout\Common\Entity\Style\BorderPart; use Box\Spout\Common\Entity\Style\CellAlignment; +use Box\Spout\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\ManagesCellSize; use Box\Spout\Writer\ODS\Helper\BorderHelper; @@ -19,6 +21,17 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager /** @var StyleRegistry */ protected $styleRegistry; + /** + * @param StyleRegistry $styleRegistry + */ + public function __construct(StyleRegistry $styleRegistry, OptionsManagerInterface $optionsManager) + { + parent::__construct($styleRegistry); + $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); + $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); + $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; + } + /** * Returns the content of the "styles.xml" file, given a list of styles. * diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 5a3ec50..05e1ffd 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -40,25 +40,17 @@ class WorksheetManager implements WorksheetManagerInterface * @param StyleMerger $styleMerger * @param ODSEscaper $stringsEscaper * @param StringHelper $stringHelper - * @param OptionsManager|null $optionsManager */ public function __construct( StyleManager $styleManager, StyleMerger $styleMerger, ODSEscaper $stringsEscaper, - StringHelper $stringHelper, - $optionsManager = null + StringHelper $stringHelper ) { $this->styleManager = $styleManager; $this->styleMerger = $styleMerger; $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; - - if ($optionsManager) { - $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); - $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); - $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; - } } /** diff --git a/tests/Spout/Writer/ODS/SheetTest.php b/tests/Spout/Writer/ODS/SheetTest.php index a8e0c8b..b5fec7b 100644 --- a/tests/Spout/Writer/ODS/SheetTest.php +++ b/tests/Spout/Writer/ODS/SheetTest.php @@ -101,20 +101,18 @@ class SheetTest extends TestCase $writer->addRow($this->createRowFromValues([])); } - public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() - { - $this->expectException(WriterNotOpenedException::class); - $writer = WriterEntityFactory::createXLSXWriter(); - $writer->setDefaultColumnWidth(10.0); - } - public function testWritesDefaultCellSizesIfSet() { $fileName = 'test_writes_default_cell_sizes_if_set.ods'; - $writer = $this->writerForFile($fileName); + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createODSWriter(); $writer->setDefaultColumnWidth(100.0); $writer->setDefaultRowHeight(20.0); + $writer->openToFile($resourcePath); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); $writer->close(); diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index 91c28f9..5a914e8 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -101,13 +101,6 @@ class SheetTest extends TestCase $writer->addRow($this->createRowFromValues([])); } - public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() - { - $this->expectException(WriterNotOpenedException::class); - $writer = WriterEntityFactory::createXLSXWriter(); - $writer->setDefaultColumnWidth(10.0); - } - public function testWritesDefaultCellSizesIfSet() { $fileName = 'test_writes_default_cell_sizes_if_set.xlsx'; @@ -115,9 +108,9 @@ class SheetTest extends TestCase $resourcePath = $this->getGeneratedResourcePath($fileName); $writer = WriterEntityFactory::createXLSXWriter(); - $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); $writer->setDefaultRowHeight(20.0); + $writer->openToFile($resourcePath); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close(); @@ -137,8 +130,9 @@ class SheetTest extends TestCase $resourcePath = $this->getGeneratedResourcePath($fileName); $writer = WriterEntityFactory::createXLSXWriter(); - $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); + $writer->openToFile($resourcePath); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close();