Row objects and Cell styling

This commit introduces Row and Cell entities, that will replace the arrays passed in previously.
It also adds support for Cell styling (instead of Row styling only).
This commit is contained in:
Adrien Loison committed 2017-11-05 02:12:28 +01:00
1 parent fec27e9056
commit 7274226b75
32 files changed
+1113 -859

No files matched your search

@@ -7,8 +7,8 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
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\Style\Style;
use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
@@ -198,21 +198,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
}
/**
* Adds data to the current sheet.
* Adds a row to the current sheet.
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
* with the creation of new worksheets if one worksheet has reached its maximum capicity.
*
* @param array $dataRow Array containing data to be written. Cannot be empty.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $style Style to be applied to the row.
* @param Row $row The row to be added
* @throws IOException If trying to create a new sheet and unable to open the sheet for writing
* @throws WriterException If unable to write data
* @return void
*/
public function addRowToCurrentWorksheet($dataRow, Style $style)
public function addRowToCurrentWorksheet(Row $row)
{
$currentWorksheet = $this->getCurrentWorksheet();
$hasReachedMaxRows = $this->hasCurrentWorkseetReachedMaxRows();
$hasReachedMaxRows = $this->hasCurrentWorksheetReachedMaxRows();
// if we reached the maximum number of rows for the current sheet...
if ($hasReachedMaxRows) {
@@ -220,19 +218,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
$currentWorksheet = $this->addNewSheetAndMakeItCurrent();
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style);
$this->addRowToWorksheet($currentWorksheet, $row);
} else {
// otherwise, do nothing as the data won't be written anyways
}
} else {
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style);
$this->addRowToWorksheet($currentWorksheet, $row);
}
}
/**
* @return bool Whether the current worksheet has reached the maximum number of rows per sheet.
*/
private function hasCurrentWorkseetReachedMaxRows()
private function hasCurrentWorksheetReachedMaxRows()
{
$currentWorksheet = $this->getCurrentWorksheet();
@@ -240,24 +238,20 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
}
/**
* Adds data with the given style to the given sheet.
* Adds a row to the given sheet.
*
* @param Worksheet $worksheet Worksheet to write the row to
* @param array $dataRow Array containing data to be written. Cannot be empty.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $style Style to be applied to the row.
* @param Row $row The row to be added
* @throws WriterException If unable to write data
* @return void
*/
private function addRowWithStyleToWorksheet(Worksheet $worksheet, $dataRow, Style $style)
private function addRowToWorksheet(Worksheet $worksheet, Row $row)
{
$updatedStyle = $this->styleManager->applyExtraStylesIfNeeded($style, $dataRow);
$registeredStyle = $this->styleManager->registerStyle($updatedStyle);
$this->worksheetManager->addRow($worksheet, $dataRow, $registeredStyle);
$this->worksheetManager->addRow($worksheet, $row);
// update max num columns for the worksheet
$currentMaxNumColumns = $worksheet->getMaxNumColumns();
$cellsCount = count($dataRow);
$cellsCount = count($row->getCells());
$worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount));
}