Apply default row style in WorkbookManager
Instead of doing it in the Writer
This commit is contained in:
parent
3d0f108b1d
commit
0f15fb652c
@ -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.
|
||||
|
@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\SheetManager;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleRegistry;
|
||||
use Box\Spout\Writer\ODS\Manager\WorkbookManager;
|
||||
@ -45,6 +46,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
$fileSystemHelper = $this->helperFactory->createSpecificFileSystemHelper($optionsManager, $this->entityFactory);
|
||||
$fileSystemHelper->createBaseFilesAndFolders();
|
||||
|
||||
$styleMerger = $this->createStyleMerger();
|
||||
$styleManager = $this->createStyleManager($optionsManager);
|
||||
$worksheetManager = $this->createWorksheetManager($styleManager);
|
||||
|
||||
@ -53,6 +55,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
$optionsManager,
|
||||
$worksheetManager,
|
||||
$styleManager,
|
||||
$styleMerger,
|
||||
$fileSystemHelper,
|
||||
$this->entityFactory,
|
||||
$this
|
||||
@ -102,4 +105,12 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
|
||||
return new StyleRegistry($defaultRowStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StyleMerger
|
||||
*/
|
||||
private function createStyleMerger()
|
||||
{
|
||||
return new StyleMerger();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
|
||||
use Box\Spout\Writer\Exception\WriterNotOpenedException;
|
||||
|
||||
@ -40,26 +39,20 @@ abstract class WriterAbstract implements WriterInterface
|
||||
/** @var OptionsManagerInterface Writer options manager */
|
||||
protected $optionsManager;
|
||||
|
||||
/** @var StyleMerger Helps merge styles together */
|
||||
protected $styleMerger;
|
||||
|
||||
/** @var string Content-Type value for the header - to be defined by child class */
|
||||
protected static $headerContentType;
|
||||
|
||||
/**
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param StyleMerger $styleMerger
|
||||
* @param GlobalFunctionsHelper $globalFunctionsHelper
|
||||
* @param HelperFactory $helperFactory
|
||||
*/
|
||||
public function __construct(
|
||||
OptionsManagerInterface $optionsManager,
|
||||
StyleMerger $styleMerger,
|
||||
GlobalFunctionsHelper $globalFunctionsHelper,
|
||||
HelperFactory $helperFactory
|
||||
) {
|
||||
$this->optionsManager = $optionsManager;
|
||||
$this->styleMerger = $styleMerger;
|
||||
$this->globalFunctionsHelper = $globalFunctionsHelper;
|
||||
$this->helperFactory = $helperFactory;
|
||||
}
|
||||
@ -188,7 +181,6 @@ abstract class WriterAbstract implements WriterInterface
|
||||
// empty $dataRow should not add an empty line
|
||||
if ($row->hasCells()) {
|
||||
try {
|
||||
$this->applyDefaultRowStyle($row);
|
||||
$this->addRowToWriter($row);
|
||||
} catch (SpoutException $e) {
|
||||
// if an exception occurs while writing data,
|
||||
@ -223,21 +215,6 @@ abstract class WriterAbstract implements WriterInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: Move this into styleMerger
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -10,7 +10,6 @@ use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
|
||||
use Box\Spout\Writer\Exception\SheetNotFoundException;
|
||||
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
|
||||
@ -31,19 +30,17 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
|
||||
|
||||
/**
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param StyleMerger $styleMerger
|
||||
* @param GlobalFunctionsHelper $globalFunctionsHelper
|
||||
* @param HelperFactory $helperFactory
|
||||
* @param ManagerFactoryInterface $managerFactory
|
||||
*/
|
||||
public function __construct(
|
||||
OptionsManagerInterface $optionsManager,
|
||||
StyleMerger $styleMerger,
|
||||
GlobalFunctionsHelper $globalFunctionsHelper,
|
||||
HelperFactory $helperFactory,
|
||||
ManagerFactoryInterface $managerFactory
|
||||
) {
|
||||
parent::__construct($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory);
|
||||
parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory);
|
||||
$this->managerFactory = $managerFactory;
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\SheetManager;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\XLSX\Manager\SharedStringsManager;
|
||||
use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
|
||||
use Box\Spout\Writer\XLSX\Manager\Style\StyleRegistry;
|
||||
@ -49,6 +50,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
$xlFolder = $fileSystemHelper->getXlFolder();
|
||||
$sharedStringsManager = $this->createSharedStringsManager($xlFolder);
|
||||
|
||||
$styleMerger = $this->createStyleMerger();
|
||||
$styleManager = $this->createStyleManager($optionsManager);
|
||||
$worksheetManager = $this->createWorksheetManager($optionsManager, $styleManager, $sharedStringsManager);
|
||||
|
||||
@ -57,6 +59,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
$optionsManager,
|
||||
$worksheetManager,
|
||||
$styleManager,
|
||||
$styleMerger,
|
||||
$fileSystemHelper,
|
||||
$this->entityFactory,
|
||||
$this
|
||||
@ -112,6 +115,14 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
return new StyleRegistry($defaultRowStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StyleMerger
|
||||
*/
|
||||
private function createStyleMerger()
|
||||
{
|
||||
return new StyleMerger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $xlFolder Path to the "xl" folder
|
||||
* @return SharedStringsManager
|
||||
|
Loading…
x
Reference in New Issue
Block a user