Apply default row style in WorkbookManager
Instead of doing it in the Writer
This commit is contained in:
1 parent
3d0f108b1d
commit
8aec9ea992
6 files changed
+50
-37
No files matched your search
@@ -7,7 +7,6 @@ use Box\Spout\Common\Exception\UnsupportedTypeException;
|
||||
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager;
|
||||
use Box\Spout\Writer\CSV\Writer as CSVWriter;
|
||||
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory;
|
||||
@@ -51,12 +50,11 @@ class WriterFactory
|
||||
private function getCSVWriter()
|
||||
{
|
||||
$optionsManager = new CSVOptionsManager();
|
||||
$styleMerger = new StyleMerger();
|
||||
$globalFunctionsHelper = new GlobalFunctionsHelper();
|
||||
|
||||
$helperFactory = new HelperFactory();
|
||||
|
||||
return new CSVWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory);
|
||||
return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,13 +64,12 @@ class WriterFactory
|
||||
{
|
||||
$styleBuilder = new StyleBuilder();
|
||||
$optionsManager = new XLSXOptionsManager($styleBuilder);
|
||||
$styleMerger = new StyleMerger();
|
||||
$globalFunctionsHelper = new GlobalFunctionsHelper();
|
||||
|
||||
$helperFactory = new XLSXHelperFactory();
|
||||
$managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
|
||||
|
||||
return new XLSXWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
|
||||
return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,12 +79,11 @@ class WriterFactory
|
||||
{
|
||||
$styleBuilder = new StyleBuilder();
|
||||
$optionsManager = new ODSOptionsManager($styleBuilder);
|
||||
$styleMerger = new StyleMerger();
|
||||
$globalFunctionsHelper = new GlobalFunctionsHelper();
|
||||
|
||||
$helperFactory = new ODSHelperFactory();
|
||||
$managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
|
||||
|
||||
return new ODSWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
|
||||
return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Box\Spout\Writer\Common\Entity\Workbook;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\Exception\SheetNotFoundException;
|
||||
use Box\Spout\Writer\Exception\WriterException;
|
||||
|
||||
@@ -26,7 +27,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
protected $workbook;
|
||||
|
||||
/** @var OptionsManagerInterface */
|
||||
protected $optionManager;
|
||||
protected $optionsManager;
|
||||
|
||||
/** @var WorksheetManagerInterface */
|
||||
protected $worksheetManager;
|
||||
@@ -34,6 +35,9 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
/** @var StyleManagerInterface Manages styles */
|
||||
protected $styleManager;
|
||||
|
||||
/** @var StyleMerger Helper to merge styles */
|
||||
protected $styleMerger;
|
||||
|
||||
/** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */
|
||||
protected $fileSystemHelper;
|
||||
|
||||
@@ -51,6 +55,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param WorksheetManagerInterface $worksheetManager
|
||||
* @param StyleManagerInterface $styleManager
|
||||
* @param StyleMerger $styleMerger
|
||||
* @param FileSystemWithRootFolderHelperInterface $fileSystemHelper
|
||||
* @param InternalEntityFactory $entityFactory
|
||||
* @param ManagerFactoryInterface $managerFactory
|
||||
@@ -60,14 +65,16 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
OptionsManagerInterface $optionsManager,
|
||||
WorksheetManagerInterface $worksheetManager,
|
||||
StyleManagerInterface $styleManager,
|
||||
StyleMerger $styleMerger,
|
||||
FileSystemWithRootFolderHelperInterface $fileSystemHelper,
|
||||
InternalEntityFactory $entityFactory,
|
||||
ManagerFactoryInterface $managerFactory
|
||||
) {
|
||||
$this->workbook = $workbook;
|
||||
$this->optionManager = $optionsManager;
|
||||
$this->optionsManager = $optionsManager;
|
||||
$this->worksheetManager = $worksheetManager;
|
||||
$this->styleManager = $styleManager;
|
||||
$this->styleMerger = $styleMerger;
|
||||
$this->fileSystemHelper = $fileSystemHelper;
|
||||
$this->entityFactory = $entityFactory;
|
||||
$this->managerFactory = $managerFactory;
|
||||
@@ -215,7 +222,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
// if we reached the maximum number of rows for the current sheet...
|
||||
if ($hasReachedMaxRows) {
|
||||
// ... continue writing in a new sheet if option set
|
||||
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
|
||||
if ($this->optionsManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
|
||||
$currentWorksheet = $this->addNewSheetAndMakeItCurrent();
|
||||
|
||||
$this->addRowToWorksheet($currentWorksheet, $row);
|
||||
@@ -247,6 +254,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
*/
|
||||
private function addRowToWorksheet(Worksheet $worksheet, Row $row)
|
||||
{
|
||||
$this->applyDefaultRowStyle($row);
|
||||
$this->worksheetManager->addRow($worksheet, $row);
|
||||
|
||||
// update max num columns for the worksheet
|
||||
@@ -255,6 +263,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
$worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Row $row
|
||||
*/
|
||||
private function applyDefaultRowStyle(Row $row)
|
||||
{
|
||||
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
|
||||
|
||||
if ($defaultRowStyle !== null) {
|
||||
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
|
||||
$row->setStyle($mergedStyle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the workbook and all its associated sheets.
|
||||
* All the necessary files are written to disk and zipped together to create the final file.
|
||||
|
||||
Reference in new issue
Block a user