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:
1 parent
fec27e9056
commit
7274226b75
32 files changed
+1113
-859
No files matched your search
@@ -3,10 +3,14 @@
|
||||
namespace Box\Spout\Writer\Common\Creator;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
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\Manager\RowManager;
|
||||
use Box\Spout\Writer\Common\Manager\SheetManager;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
/**
|
||||
* Class EntityFactory
|
||||
@@ -47,7 +51,7 @@ class EntityFactory
|
||||
* @param mixed $cellValue
|
||||
* @return Cell
|
||||
*/
|
||||
public function createCell($cellValue)
|
||||
public static function createCell($cellValue)
|
||||
{
|
||||
return new Cell($cellValue);
|
||||
}
|
||||
@@ -59,4 +63,17 @@ class EntityFactory
|
||||
{
|
||||
return new \ZipArchive();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $cells
|
||||
* @param Style|null $style
|
||||
* @return Row
|
||||
*/
|
||||
public static function createRow(array $cells, Style $style = null)
|
||||
{
|
||||
$styleMerger = new StyleMerger();
|
||||
$rowManager = new RowManager($styleMerger);
|
||||
|
||||
return new Row($cells, $style, $rowManager);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
/**
|
||||
* Class Cell
|
||||
@@ -53,16 +55,29 @@ class Cell
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Cell constructor.
|
||||
* @param $value mixed
|
||||
* The cell style
|
||||
* @var Style
|
||||
*/
|
||||
public function __construct($value)
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* @var StyleMerger
|
||||
*/
|
||||
protected $styleMerger;
|
||||
|
||||
/**
|
||||
* @param $value mixed
|
||||
* @param Style|null $style
|
||||
*/
|
||||
public function __construct($value, Style $style = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
$this->setStyle($style);
|
||||
$this->styleMerger = new StyleMerger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value mixed|null
|
||||
* @param mixed|null $value
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
@@ -78,6 +93,22 @@ class Cell
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style|null $style
|
||||
*/
|
||||
public function setStyle($style)
|
||||
{
|
||||
$this->style = $style ?: new Style();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
@@ -88,6 +119,7 @@ class Cell
|
||||
|
||||
/**
|
||||
* Get the current value type
|
||||
*
|
||||
* @param mixed|null $value
|
||||
* @return int
|
||||
*/
|
||||
@@ -127,6 +159,7 @@ class Cell
|
||||
|
||||
/**
|
||||
* Not used at the moment
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isFormula()
|
||||
@@ -165,4 +198,20 @@ class Cell
|
||||
{
|
||||
return (string) $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style|null $style
|
||||
* @return Cell
|
||||
*/
|
||||
public function applyStyle($style)
|
||||
{
|
||||
if ($style === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$mergedStyle = $this->styleMerger->merge($this->style, $style);
|
||||
$this->setStyle($mergedStyle);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\RowManager;
|
||||
|
||||
class Row
|
||||
{
|
||||
/**
|
||||
* The cells in this row
|
||||
* @var Cell[]
|
||||
*/
|
||||
protected $cells = [];
|
||||
|
||||
/**
|
||||
* The row style
|
||||
* @var Style
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* Thw row manager
|
||||
* @var RowManager
|
||||
*/
|
||||
protected $rowManager;
|
||||
|
||||
/**
|
||||
* Row constructor.
|
||||
* @param Cell[] $cells
|
||||
* @param Style|null $style
|
||||
* @param RowManager $rowManager
|
||||
*/
|
||||
public function __construct(array $cells, $style, RowManager $rowManager)
|
||||
{
|
||||
$this
|
||||
->setCells($cells)
|
||||
->setStyle($style);
|
||||
|
||||
$this->rowManager = $rowManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Cell[] $cells
|
||||
*/
|
||||
public function getCells()
|
||||
{
|
||||
return $this->cells;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Cell[] $cells
|
||||
* @return $this
|
||||
*/
|
||||
public function setCells(array $cells)
|
||||
{
|
||||
$this->cells = [];
|
||||
foreach ($cells as $cell) {
|
||||
$this->addCell($cell);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style|null $style
|
||||
* @return Row
|
||||
*/
|
||||
public function setStyle($style)
|
||||
{
|
||||
$this->style = $style ?: new Style();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style
|
||||
* @return Row
|
||||
*/
|
||||
public function applyStyle($style)
|
||||
{
|
||||
$this->rowManager->applyStyle($this, $style);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Cell $cell
|
||||
* @return Row
|
||||
*/
|
||||
public function addCell(Cell $cell)
|
||||
{
|
||||
$this->cells[] = $cell;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a row has cells
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCells()
|
||||
{
|
||||
return $this->rowManager->hasCells($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether this row is considered empty.
|
||||
* An empty row has either no cells at all - or only empty cells
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return $this->rowManager->isEmpty($this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
class CellManager
|
||||
{
|
||||
/**
|
||||
* @var StyleMerger
|
||||
*/
|
||||
protected $styleMerger;
|
||||
|
||||
/**
|
||||
* @param StyleMerger $styleMerger
|
||||
*/
|
||||
public function __construct(StyleMerger $styleMerger)
|
||||
{
|
||||
$this->styleMerger = $styleMerger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges a Style into a cell's Style.
|
||||
*
|
||||
* @param Cell $cell
|
||||
* @param Style $style
|
||||
* @return void
|
||||
*/
|
||||
public function applyStyle(Cell $cell, Style $style)
|
||||
{
|
||||
$mergedStyle = $this->styleMerger->merge($cell->getStyle(), $style);
|
||||
$cell->setStyle($mergedStyle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
class RowManager
|
||||
{
|
||||
/**
|
||||
* @var StyleMerger
|
||||
*/
|
||||
protected $styleMerger;
|
||||
|
||||
/**
|
||||
* @param StyleMerger $styleMerger
|
||||
*/
|
||||
public function __construct(StyleMerger $styleMerger)
|
||||
{
|
||||
$this->styleMerger = $styleMerger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Row $row
|
||||
* @param Style $style
|
||||
* @return $this
|
||||
*/
|
||||
public function applyStyle(Row $row, Style $style)
|
||||
{
|
||||
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $style);
|
||||
$row->setStyle($mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a row has cells
|
||||
*
|
||||
* @param Row $row
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCells(Row $row)
|
||||
{
|
||||
return count($row->getCells()) !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether a row is considered empty.
|
||||
* An empty row has either no cells at all - or only one empty cell
|
||||
*
|
||||
* @param Row $row
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(Row $row)
|
||||
{
|
||||
$cells = $row->getCells();
|
||||
|
||||
return count($cells) === 0 || (count($cells) === 1 && $cells[0]->isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
@@ -48,13 +49,12 @@ class StyleManager implements StyleManagerInterface
|
||||
* Apply additional styles if the given row needs it.
|
||||
* Typically, set "wrap text" if a cell contains a new line.
|
||||
*
|
||||
* @param Style $style The original style
|
||||
* @param array $dataRow The row the style will be applied to
|
||||
* @return Style The updated style
|
||||
* @param Cell $cell
|
||||
* @return Style
|
||||
*/
|
||||
public function applyExtraStylesIfNeeded($style, $dataRow)
|
||||
public function applyExtraStylesIfNeeded(Cell $cell)
|
||||
{
|
||||
$updatedStyle = $this->applyWrapTextIfCellContainsNewLine($style, $dataRow);
|
||||
$updatedStyle = $this->applyWrapTextIfCellContainsNewLine($cell);
|
||||
|
||||
return $updatedStyle;
|
||||
}
|
||||
@@ -68,24 +68,22 @@ class StyleManager implements StyleManagerInterface
|
||||
* A workaround would be to encode "\n" as "_x000D_" but it does not work
|
||||
* on the Windows version of Excel...
|
||||
*
|
||||
* @param Style $style The original style
|
||||
* @param array $dataRow The row the style will be applied to
|
||||
* @return Style The eventually updated style
|
||||
* @param Cell $cell The cell the style should be applied to
|
||||
* @return \Box\Spout\Writer\Common\Entity\Style\Style The eventually updated style
|
||||
*/
|
||||
protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
|
||||
protected function applyWrapTextIfCellContainsNewLine(Cell $cell)
|
||||
{
|
||||
$cellStyle = $cell->getStyle();
|
||||
|
||||
// if the "wrap text" option is already set, no-op
|
||||
if ($style->hasSetWrapText()) {
|
||||
return $style;
|
||||
if ($cellStyle->hasSetWrapText()) {
|
||||
return $cellStyle;
|
||||
}
|
||||
|
||||
foreach ($dataRow as $cell) {
|
||||
if (is_string($cell) && strpos($cell, "\n") !== false) {
|
||||
$style->setShouldWrapText();
|
||||
break;
|
||||
}
|
||||
if ($cell->isString() && strpos($cell->getValue(), "\n") !== false) {
|
||||
$cellStyle->setShouldWrapText();
|
||||
}
|
||||
|
||||
return $style;
|
||||
return $cellStyle;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
@@ -22,9 +23,8 @@ interface StyleManagerInterface
|
||||
* Apply additional styles if the given row needs it.
|
||||
* Typically, set "wrap text" if a cell contains a new line.
|
||||
*
|
||||
* @param Style $style The original style
|
||||
* @param array $dataRow The row the style will be applied to
|
||||
* @param Cell $cell
|
||||
* @return Style The updated style
|
||||
*/
|
||||
public function applyExtraStylesIfNeeded($style, $dataRow);
|
||||
public function applyExtraStylesIfNeeded(Cell $cell);
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
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\Exception\SheetNotFoundException;
|
||||
@@ -53,18 +53,16 @@ interface WorkbookManagerInterface
|
||||
public function setCurrentSheet(Sheet $sheet);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Closes the workbook and all its associated sheets.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
|
||||
/**
|
||||
@@ -12,17 +12,15 @@ use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
interface WorksheetManagerInterface
|
||||
{
|
||||
/**
|
||||
* Adds data to the worksheet.
|
||||
* Adds a row to the worksheet.
|
||||
*
|
||||
* @param Worksheet $worksheet The worksheet to add the row to
|
||||
* @param array $dataRow Array containing data to be written. Cannot be empty.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param Style $rowStyle Style to be applied to the row. NULL means use default style.
|
||||
* @param Row $row The row to be added
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||
* @return void
|
||||
*/
|
||||
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle);
|
||||
public function addRow(Worksheet $worksheet, Row $row);
|
||||
|
||||
/**
|
||||
* Prepares the worksheet to accept data
|
||||
|
||||
Reference in new issue
Block a user