Merge branch 'cell_styling' of git://github.com/madflow/spout into madflow-cell_styling
This commit is contained in:
commit
13eb5f0560
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\EncodingHelper;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\WriterAbstract;
|
||||
|
||||
/**
|
||||
@ -78,20 +79,20 @@ class Writer extends WriterAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to the currently opened writer.
|
||||
* Adds a row to the currently opened writer.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be written.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style Ignored here since CSV does not support styling.
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @param Row $row The row containing cells and styles
|
||||
*
|
||||
* @throws IOException If unable to write data
|
||||
* @return void
|
||||
* @internal param \Box\Spout\Writer\Common\Entity\Style\Style $style Ignored here since CSV does not support styling.
|
||||
*/
|
||||
protected function addRowToWriter(array $dataRow, $style)
|
||||
protected function addRowToWriter(Row $row)
|
||||
{
|
||||
$fieldDelimiter = $this->optionsManager->getOption(Options::FIELD_DELIMITER);
|
||||
$fieldEnclosure = $this->optionsManager->getOption(Options::FIELD_ENCLOSURE);
|
||||
|
||||
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $fieldDelimiter, $fieldEnclosure);
|
||||
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $row->getCells(), $fieldDelimiter, $fieldEnclosure);
|
||||
if ($wasWriteSuccessful === false) {
|
||||
throw new IOException('Unable to write data');
|
||||
}
|
||||
|
@ -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
|
||||
@ -52,13 +54,27 @@ class Cell
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* The cell style
|
||||
* @var Style|null
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* @var StyleMerger
|
||||
*/
|
||||
protected $styleMerger;
|
||||
|
||||
/**
|
||||
* Cell constructor.
|
||||
* @param $value mixed
|
||||
* @param $style|null Style
|
||||
*/
|
||||
public function __construct($value)
|
||||
public function __construct($value, Style $style = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
$this->setStyle($style);
|
||||
$this->styleMerger = new StyleMerger();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,6 +94,26 @@ class Cell
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style|null
|
||||
*/
|
||||
public function setStyle(Style $style = null)
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style|null
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
if (!isset($this->style)) {
|
||||
$this->setStyle(new Style());
|
||||
}
|
||||
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
@ -165,4 +201,19 @@ class Cell
|
||||
{
|
||||
return (string) $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style|null
|
||||
* @return Cell
|
||||
*/
|
||||
public function applyStyle(Style $style = null)
|
||||
{
|
||||
if ($style === null) {
|
||||
return $this;
|
||||
}
|
||||
$mergedStyle = $this->styleMerger->merge($this->getStyle(), $style);
|
||||
$this->setStyle($mergedStyle);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
120
src/Spout/Writer/Common/Entity/Row.php
Normal file
120
src/Spout/Writer/Common/Entity/Row.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?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 array
|
||||
*/
|
||||
protected $cells = [];
|
||||
|
||||
/**
|
||||
* The row style
|
||||
* @var Style|null
|
||||
*/
|
||||
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 $style = null, RowManager $rowManager)
|
||||
{
|
||||
$this
|
||||
->setCells($cells)
|
||||
->setStyle($style);
|
||||
|
||||
$this->rowManager = $rowManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Cell[] $cells
|
||||
*/
|
||||
public function getCells()
|
||||
{
|
||||
return $this->cells;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $cells
|
||||
* @return $this
|
||||
*/
|
||||
public function setCells(array $cells)
|
||||
{
|
||||
$this->cells = [];
|
||||
foreach ($cells as $cell) {
|
||||
$this->addCell($cell);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
if (!isset($this->style)) {
|
||||
$this->setStyle(new Style());
|
||||
}
|
||||
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style
|
||||
* @return Row
|
||||
*/
|
||||
public function setStyle($style)
|
||||
{
|
||||
$this->style = $style;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style|null
|
||||
* @return Row
|
||||
*/
|
||||
public function applyStyle(Style $style = null)
|
||||
{
|
||||
$this->rowManager->applyStyle($this, $style);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Cell $cell
|
||||
* @return Row
|
||||
*/
|
||||
public function addCell(Cell $cell)
|
||||
{
|
||||
$this->cells[] = $cell;
|
||||
|
||||
return $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);
|
||||
}
|
||||
}
|
37
src/Spout/Writer/Common/Manager/CellManager.php
Normal file
37
src/Spout/Writer/Common/Manager/CellManager.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* CellManager constructor.
|
||||
* @param StyleMerger $styleMerger
|
||||
*/
|
||||
public function __construct(StyleMerger $styleMerger)
|
||||
{
|
||||
$this->styleMerger = $styleMerger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges a Style into a cells Style.
|
||||
*
|
||||
* @param Cell $cell
|
||||
* @param Style $style
|
||||
* @return $this
|
||||
*/
|
||||
public function applyStyle(Cell $cell, Style $style)
|
||||
{
|
||||
$mergedStyle = $this->styleMerger->merge($cell->getStyle(), $style);
|
||||
$cell->setStyle($mergedStyle);
|
||||
}
|
||||
}
|
48
src/Spout/Writer/Common/Manager/RowManager.php
Normal file
48
src/Spout/Writer/Common/Manager/RowManager.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* RowManager constructor.
|
||||
* @param StyleMerger $styleMerger
|
||||
*/
|
||||
public function __construct(StyleMerger $styleMerger)
|
||||
{
|
||||
$this->styleMerger = $styleMerger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Style $style
|
||||
* @return $this
|
||||
*/
|
||||
public function applyStyle(Row $row, Style $style)
|
||||
{
|
||||
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $style);
|
||||
$row->setStyle($mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,19 @@ 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)
|
||||
{
|
||||
// if the "wrap text" option is already set, no-op
|
||||
if ($style->hasSetWrapText()) {
|
||||
return $style;
|
||||
if ($cell->getStyle()->hasSetWrapText()) {
|
||||
return $cell->getStyle();
|
||||
}
|
||||
if ($cell->isString() && strpos($cell->getValue(), "\n") !== false) {
|
||||
$cell->getStyle()->setShouldWrapText();
|
||||
}
|
||||
|
||||
foreach ($dataRow as $cell) {
|
||||
if (is_string($cell) && strpos($cell, "\n") !== false) {
|
||||
$style->setShouldWrapText();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $style;
|
||||
return $cell->getStyle();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
* @return Style The updated style
|
||||
* @param Cell $cell
|
||||
* @return \Box\Spout\Writer\Common\Entity\Style\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,20 @@ 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 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
|
||||
* @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 +219,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();
|
||||
|
||||
@ -243,21 +242,18 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
* Adds data with the given style 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
|
||||
* @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,17 @@ 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 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
|
||||
* @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,16 @@ 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
|
||||
* @return void
|
||||
*/
|
||||
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle);
|
||||
public function addRow(Worksheet $worksheet, Row $row);
|
||||
|
||||
/**
|
||||
* Prepares the worksheet to accept data
|
||||
|
@ -46,7 +46,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
$fileSystemHelper->createBaseFilesAndFolders();
|
||||
|
||||
$styleManager = $this->createStyleManager($optionsManager);
|
||||
$worksheetManager = $this->createWorksheetManager();
|
||||
$worksheetManager = $this->createWorksheetManager($styleManager);
|
||||
|
||||
return new WorkbookManager(
|
||||
$workbook,
|
||||
@ -60,13 +60,16 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StyleManager $styleManager
|
||||
* @return WorksheetManager
|
||||
*/
|
||||
private function createWorksheetManager()
|
||||
private function createWorksheetManager(StyleManager $styleManager)
|
||||
{
|
||||
$stringsEscaper = $this->helperFactory->createStringsEscaper();
|
||||
$stringsHelper = $this->helperFactory->createStringHelper();
|
||||
|
||||
return new WorksheetManager($styleManager, $stringsEscaper, $stringsHelper);
|
||||
|
||||
return new WorksheetManager($stringsEscaper, $stringsHelper, $this->entityFactory);
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,13 @@ namespace Box\Spout\Writer\ODS\Manager;
|
||||
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
|
||||
/**
|
||||
* Class WorksheetManager
|
||||
@ -23,24 +24,23 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
/** @var StringHelper String helper */
|
||||
private $stringHelper;
|
||||
|
||||
/** @var EntityFactory Factory to create entities */
|
||||
private $entityFactory;
|
||||
/** @var StyleManager Manages styles */
|
||||
private $styleManager;
|
||||
|
||||
/**
|
||||
* WorksheetManager constructor.
|
||||
*
|
||||
* @param \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper
|
||||
* @param StyleManager $styleManager
|
||||
* @param ODSEscaper $stringsEscaper
|
||||
* @param StringHelper $stringHelper
|
||||
* @param EntityFactory $entityFactory
|
||||
*/
|
||||
public function __construct(
|
||||
\Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper,
|
||||
StringHelper $stringHelper,
|
||||
EntityFactory $entityFactory
|
||||
StyleManager $styleManager,
|
||||
ODSEscaper $stringsEscaper,
|
||||
StringHelper $stringHelper
|
||||
) {
|
||||
$this->stringsEscaper = $stringsEscaper;
|
||||
$this->stringHelper = $stringHelper;
|
||||
$this->entityFactory = $entityFactory;
|
||||
$this->styleManager = $styleManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,24 +91,20 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to the given 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 IOException If the data cannot be written
|
||||
* @throws InvalidArgumentException If a cell value's type is not supported
|
||||
* @return void
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle)
|
||||
public function addRow(Worksheet $worksheet, Row $row)
|
||||
{
|
||||
// $dataRow can be an associative array. We need to transform
|
||||
// it into a regular array, as we'll use the numeric indexes.
|
||||
$dataRowWithNumericIndexes = array_values($dataRow);
|
||||
|
||||
$styleIndex = ($rowStyle->getId() + 1); // 1-based
|
||||
$cellsCount = count($dataRow);
|
||||
$cells = $row->getCells();
|
||||
$cellsCount = count($cells);
|
||||
|
||||
$data = '<table:table-row table:style-name="ro1">';
|
||||
|
||||
@ -116,14 +112,21 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
$nextCellIndex = 1;
|
||||
|
||||
for ($i = 0; $i < $cellsCount; $i++) {
|
||||
$currentCellValue = $dataRowWithNumericIndexes[$currentCellIndex];
|
||||
/** @var Cell $cell */
|
||||
$cell = $cells[$currentCellIndex];
|
||||
/** @var Cell|null $nextCell */
|
||||
$nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null;
|
||||
|
||||
// @TODO refactoring: move this to its own method
|
||||
if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) {
|
||||
// Apply styles - the row style is merged at this point
|
||||
$cell->applyStyle($row->getStyle());
|
||||
$this->styleManager->applyExtraStylesIfNeeded($cell);
|
||||
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle());
|
||||
$styleIndex = $registeredStyle->getId() + 1; // 1-based
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (!isset($dataRowWithNumericIndexes[$nextCellIndex]) ||
|
||||
$currentCellValue !== $dataRowWithNumericIndexes[$nextCellIndex]) {
|
||||
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
||||
$data .= $this->getCellXML($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
||||
|
||||
$data .= $this->getCellXML($cell, $styleIndex, $numTimesValueRepeated);
|
||||
$currentCellIndex = $nextCellIndex;
|
||||
}
|
||||
|
||||
@ -145,13 +148,13 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
/**
|
||||
* Returns the cell XML content, given its value.
|
||||
*
|
||||
* @param mixed $cellValue The value to be written
|
||||
* @param Cell $cell The cell to be written
|
||||
* @param int $styleIndex Index of the used style
|
||||
* @param int $numTimesValueRepeated Number of times the value is consecutively repeated
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||
* @return string The cell XML content
|
||||
*/
|
||||
private function getCellXML($cellValue, $styleIndex, $numTimesValueRepeated)
|
||||
protected function getCellXML(Cell $cell, $styleIndex, $numTimesValueRepeated)
|
||||
{
|
||||
$data = '<table:table-cell table:style-name="ce' . $styleIndex . '"';
|
||||
|
||||
@ -159,13 +162,6 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
|
||||
}
|
||||
|
||||
/* @TODO Remove code duplication with XLSX writer: https://github.com/box/spout/pull/383#discussion_r113292746 */
|
||||
if ($cellValue instanceof Cell) {
|
||||
$cell = $cellValue;
|
||||
} else {
|
||||
$cell = $this->entityFactory->createCell($cellValue);
|
||||
}
|
||||
|
||||
if ($cell->isString()) {
|
||||
$data .= ' office:value-type="string" calcext:value-type="string">';
|
||||
|
||||
|
@ -8,7 +8,10 @@ use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
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;
|
||||
@ -42,9 +45,6 @@ abstract class WriterAbstract implements WriterInterface
|
||||
/** @var StyleMerger Helps merge styles together */
|
||||
protected $styleMerger;
|
||||
|
||||
/** @var Style Style to be applied to the next written row(s) */
|
||||
protected $rowStyle;
|
||||
|
||||
/** @var string Content-Type value for the header - to be defined by child class */
|
||||
protected static $headerContentType;
|
||||
|
||||
@ -64,8 +64,6 @@ abstract class WriterAbstract implements WriterInterface
|
||||
$this->styleMerger = $styleMerger;
|
||||
$this->globalFunctionsHelper = $globalFunctionsHelper;
|
||||
$this->helperFactory = $helperFactory;
|
||||
|
||||
$this->resetRowStyleToDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,14 +75,12 @@ abstract class WriterAbstract implements WriterInterface
|
||||
abstract protected function openWriter();
|
||||
|
||||
/**
|
||||
* Adds data to the currently openned writer.
|
||||
* Adds a row to the currently opened writer.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be streamed.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param Style $style Style to be applied to the written row
|
||||
* @param Row $row The row containing cells and styles
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function addRowToWriter(array $dataRow, $style);
|
||||
abstract protected function addRowToWriter(Row $row);
|
||||
|
||||
/**
|
||||
* Closes the streamer, preventing any additional writing.
|
||||
@ -94,9 +90,7 @@ abstract class WriterAbstract implements WriterInterface
|
||||
abstract protected function closeWriter();
|
||||
|
||||
/**
|
||||
* Sets the default styles for all rows added with "addRow".
|
||||
* Overriding the default style instead of using "addRowWithStyle" improves performance by 20%.
|
||||
* @see https://github.com/box/spout/issues/272
|
||||
* Sets the default styles for all rows added with "addRow"
|
||||
*
|
||||
* @param Style $defaultStyle
|
||||
* @return WriterAbstract
|
||||
@ -104,19 +98,12 @@ abstract class WriterAbstract implements WriterInterface
|
||||
public function setDefaultRowStyle($defaultStyle)
|
||||
{
|
||||
$this->optionsManager->setOption(Options::DEFAULT_ROW_STYLE, $defaultStyle);
|
||||
$this->resetRowStyleToDefault();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits the writer and opens it to accept data.
|
||||
* By using this method, the data will be written to a file.
|
||||
*
|
||||
* @api
|
||||
* @param string $outputFilePath Path of the output file that will contain the data
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable
|
||||
* @return WriterAbstract
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openToFile($outputFilePath)
|
||||
{
|
||||
@ -132,15 +119,7 @@ abstract class WriterAbstract implements WriterInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits the writer and opens it to accept data.
|
||||
* By using this method, the data will be outputted directly to the browser.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @api
|
||||
* @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened
|
||||
* @return WriterAbstract
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function openToBrowser($outputFileName)
|
||||
{
|
||||
@ -203,29 +182,19 @@ abstract class WriterAbstract implements WriterInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be streamed.
|
||||
* If empty, no data is added (i.e. not even as a blank row)
|
||||
* Example: $dataRow = ['data1', 1234, null, '', 'data5', false];
|
||||
* @api
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @throws \Box\Spout\Common\Exception\SpoutException If anything else goes wrong while writing data
|
||||
* @return WriterAbstract
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addRow(array $dataRow)
|
||||
public function addRow(Row $row)
|
||||
{
|
||||
if ($this->isWriterOpened) {
|
||||
// empty $dataRow should not add an empty line
|
||||
if (!empty($dataRow)) {
|
||||
if (!$row->isEmpty()) {
|
||||
try {
|
||||
$this->addRowToWriter($dataRow, $this->rowStyle);
|
||||
$this->applyDefaultRowStyle($row);
|
||||
$this->addRowToWriter($row);
|
||||
} catch (SpoutException $e) {
|
||||
// if an exception occurs while writing data,
|
||||
// close the writer and remove all files created so far.
|
||||
$this->closeAndAttemptToCleanupAllFiles();
|
||||
|
||||
// re-throw the exception to alert developers of the error
|
||||
throw $e;
|
||||
}
|
||||
@ -238,108 +207,66 @@ abstract class WriterAbstract implements WriterInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Write given data to the output and apply the given style.
|
||||
* @see addRow
|
||||
*
|
||||
* @api
|
||||
* @param array $dataRow Array of array containing data to be streamed.
|
||||
* @param Style $style Style to be applied to the row.
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return WriterAbstract
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addRowWithStyle(array $dataRow, $style)
|
||||
public function withRow(\Closure $callback)
|
||||
{
|
||||
if (!$style instanceof Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
}
|
||||
|
||||
$this->setRowStyle($style);
|
||||
$this->addRow($dataRow);
|
||||
$this->resetRowStyleToDefault();
|
||||
|
||||
return $this;
|
||||
return $this->addRow($callback(EntityFactory::createRow([])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
*
|
||||
* @api
|
||||
* @param array $dataRows Array of array containing data to be streamed.
|
||||
* If a row is empty, it won't be added (i.e. not even as a blank row)
|
||||
* Example: $dataRows = [
|
||||
* ['data11', 12, , '', 'data13'],
|
||||
* ['data21', 'data22', null, false],
|
||||
* ];
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return WriterAbstract
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addRows(array $dataRows)
|
||||
{
|
||||
if (!empty($dataRows)) {
|
||||
$firstRow = reset($dataRows);
|
||||
if (!is_array($firstRow)) {
|
||||
throw new InvalidArgumentException('The input should be an array of arrays');
|
||||
foreach ($dataRows as $dataRow) {
|
||||
if (!$dataRow instanceof Row) {
|
||||
$this->closeAndAttemptToCleanupAllFiles();
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
foreach ($dataRows as $dataRow) {
|
||||
$this->addRow($dataRow);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write given data to the output and apply the given style.
|
||||
* @see addRows
|
||||
*
|
||||
* @api
|
||||
* @param array $dataRows Array of array containing data to be streamed.
|
||||
* @param Style $style Style to be applied to the rows.
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return WriterAbstract
|
||||
* @param array $dataRow
|
||||
* @param Style|null $style
|
||||
* @return Row
|
||||
*/
|
||||
public function addRowsWithStyle(array $dataRows, $style)
|
||||
protected function createRowFromArray(array $dataRow, Style $style = null)
|
||||
{
|
||||
if (!$style instanceof Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
if ($value instanceof Cell) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$this->setRowStyle($style);
|
||||
$this->addRows($dataRows);
|
||||
$this->resetRowStyleToDefault();
|
||||
return new Cell($value);
|
||||
}, $dataRow));
|
||||
|
||||
return $this;
|
||||
if ($style !== null) {
|
||||
$row->setStyle($style);
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the style to be applied to the next written rows
|
||||
* until it is changed or reset.
|
||||
* @TODO: Move this into styleMerger
|
||||
*
|
||||
* @param Style $style
|
||||
* @return void
|
||||
* @param Row $row
|
||||
* @return $this
|
||||
*/
|
||||
private function setRowStyle($style)
|
||||
private function applyDefaultRowStyle(Row $row)
|
||||
{
|
||||
// Merge given style with the default one to inherit custom properties
|
||||
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
|
||||
$this->rowStyle = $this->styleMerger->merge($style, $defaultRowStyle);
|
||||
if ($defaultRowStyle === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the style to be applied to the next written rows.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function resetRowStyleToDefault()
|
||||
{
|
||||
$this->rowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
|
||||
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
|
||||
$row->setStyle($mergedStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
|
||||
/**
|
||||
* Interface WriterInterface
|
||||
@ -10,7 +10,7 @@ use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
interface WriterInterface
|
||||
{
|
||||
/**
|
||||
* Inits the writer and opens it to accept data.
|
||||
* Initializes the writer and opens it to accept data.
|
||||
* By using this method, the data will be written to a file.
|
||||
*
|
||||
* @param string $outputFilePath Path of the output file that will contain the data
|
||||
@ -20,7 +20,7 @@ interface WriterInterface
|
||||
public function openToFile($outputFilePath);
|
||||
|
||||
/**
|
||||
* Inits the writer and opens it to accept data.
|
||||
* Initializes the writer and opens it to accept data.
|
||||
* By using this method, the data will be outputted directly to the browser.
|
||||
*
|
||||
* @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept
|
||||
@ -30,56 +30,32 @@ interface WriterInterface
|
||||
public function openToBrowser($outputFileName);
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
* Append a row to the end of the stream.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be streamed.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yetthe writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @param Row $row The row to be appended to the stream
|
||||
* @return WriterInterface
|
||||
*/
|
||||
public function addRow(array $dataRow);
|
||||
public function addRow(Row $row);
|
||||
|
||||
/**
|
||||
* Write given data to the output and apply the given style.
|
||||
* @see addRow
|
||||
* Write given data to the output with a closure function. New data will be appended to the end of the stream.
|
||||
*
|
||||
* @param array $dataRow Array of array containing data to be streamed.
|
||||
* @param Style $style Style to be applied to the row.
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @param \Closure $callback A callback returning a Row object. A new Row object is injected into the callback.
|
||||
* @return WriterInterface
|
||||
*/
|
||||
public function addRowWithStyle(array $dataRow, $style);
|
||||
public function withRow(\Closure $callback);
|
||||
|
||||
/**
|
||||
* Write given data to the output. New data will be appended to end of stream.
|
||||
* Write a given array of rows to the output. New data will be appended to the end of the stream.
|
||||
*
|
||||
* @param array $dataRows Array of array containing data to be streamed.
|
||||
* Example $dataRow = [
|
||||
* ['data11', 12, , '', 'data13'],
|
||||
* ['data21', 'data22', null],
|
||||
* ];
|
||||
* @param Row[] $rows Array of rows be appended to the stream
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return WriterInterface
|
||||
*/
|
||||
public function addRows(array $dataRows);
|
||||
|
||||
/**
|
||||
* Write given data to the output and apply the given style.
|
||||
* @see addRows
|
||||
*
|
||||
* @param array $dataRows Array of array containing data to be streamed.
|
||||
* @param Style $style Style to be applied to the rows.
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return WriterInterface
|
||||
*/
|
||||
public function addRowsWithStyle(array $dataRows, $style);
|
||||
public function addRows(array $rows);
|
||||
|
||||
/**
|
||||
* Closes the writer. This will close the streamer as well, preventing new data
|
||||
|
@ -7,6 +7,7 @@ use Box\Spout\Common\Helper\GlobalFunctionsHelper;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
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\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
@ -163,17 +164,16 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
|
||||
* 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.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row.
|
||||
* @param Row $row
|
||||
* @throws WriterNotOpenedException If the book is not created yet
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
protected function addRowToWriter(array $dataRow, $style)
|
||||
protected function addRowToWriter(Row $row)
|
||||
{
|
||||
$this->throwIfWorkbookIsNotAvailable();
|
||||
$this->workbookManager->addRowToCurrentWorksheet($dataRow, $style);
|
||||
$this->workbookManager->addRowToCurrentWorksheet($row);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,6 +9,7 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
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\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
@ -119,60 +120,50 @@ EOD;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to the given 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 IOException If the data cannot be written
|
||||
* @throws InvalidArgumentException If a cell value's type is not supported
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle)
|
||||
public function addRow(Worksheet $worksheet, Row $row)
|
||||
{
|
||||
if (!$this->isEmptyRow($dataRow)) {
|
||||
$this->addNonEmptyRow($worksheet, $dataRow, $rowStyle);
|
||||
if (!$row->isEmpty()) {
|
||||
$this->addNonEmptyRow($worksheet, $row);
|
||||
}
|
||||
|
||||
$worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given row is empty
|
||||
*
|
||||
* @param array $dataRow Array containing data to be written. Cannot be empty.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @return bool Whether the given row is empty
|
||||
*/
|
||||
private function isEmptyRow($dataRow)
|
||||
{
|
||||
$numCells = count($dataRow);
|
||||
// using "reset()" instead of "$dataRow[0]" because $dataRow can be an associative array
|
||||
return ($numCells === 1 && CellHelper::isEmpty(reset($dataRow)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds non empty 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 \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row. NULL means use default style.
|
||||
* @param Row $row The row to be written
|
||||
* @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
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addNonEmptyRow(Worksheet $worksheet, $dataRow, $style)
|
||||
private function addNonEmptyRow(Worksheet $worksheet, Row $row)
|
||||
{
|
||||
$cellNumber = 0;
|
||||
$rowIndex = $worksheet->getLastWrittenRowIndex() + 1;
|
||||
$numCells = count($dataRow);
|
||||
$numCells = count($row->getCells());
|
||||
|
||||
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
||||
|
||||
foreach ($dataRow as $cellValue) {
|
||||
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
|
||||
// @TODO refactoring: move this to its own method
|
||||
/** @var Cell $cell */
|
||||
foreach ($row->getCells() as $cell) {
|
||||
// Apply styles - the row style is merged at this point
|
||||
$cell->applyStyle($row->getStyle());
|
||||
$this->styleManager->applyExtraStylesIfNeeded($cell);
|
||||
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle());
|
||||
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cell, $registeredStyle->getId());
|
||||
$cellNumber++;
|
||||
}
|
||||
|
||||
@ -189,24 +180,17 @@ EOD;
|
||||
*
|
||||
* @param int $rowIndex
|
||||
* @param int $cellNumber
|
||||
* @param mixed $cellValue
|
||||
* @param Cell $cell
|
||||
* @param int $styleId
|
||||
* @throws InvalidArgumentException If the given value cannot be processed
|
||||
* @return string
|
||||
*/
|
||||
private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId)
|
||||
private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId)
|
||||
{
|
||||
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
|
||||
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
|
||||
$cellXML .= ' s="' . $styleId . '"';
|
||||
|
||||
/* @TODO Remove code duplication with ODS writer: https://github.com/box/spout/pull/383#discussion_r113292746 */
|
||||
if ($cellValue instanceof Cell) {
|
||||
$cell = $cellValue;
|
||||
} else {
|
||||
$cell = $this->entityFactory->createCell($cellValue);
|
||||
}
|
||||
|
||||
if ($cell->isString()) {
|
||||
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
|
||||
} elseif ($cell->isBoolean()) {
|
||||
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
|
||||
use Box\Spout\Common\Helper\EncodingHelper;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
@ -26,7 +27,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$writer = WriterFactory::create(Type::CSV);
|
||||
@$writer->openToFile($filePath);
|
||||
$writer->addRow(['csv--11', 'csv--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('csv--11'),
|
||||
new Cell('csv--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
@ -36,7 +41,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::CSV);
|
||||
$writer->addRow(['csv--11', 'csv--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('csv--11'),
|
||||
new Cell('csv--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
@ -46,17 +55,22 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::CSV);
|
||||
$writer->addRows([['csv--11', 'csv--12']]);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('csv--11'),
|
||||
new Cell('csv--12'),
|
||||
]);
|
||||
$writer->addRows([$row]);
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
|
||||
public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfRows()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::CSV);
|
||||
$writer->addRows(['csv--11', 'csv--12']);
|
||||
$row = new \stdClass();
|
||||
$writer->addRows([$row]);
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
@ -209,7 +223,14 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldAddBOM($shouldAddBOM);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRows($allRows);
|
||||
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
$writer->close();
|
||||
|
||||
return file_get_contents($resourcePath);
|
||||
|
57
tests/Spout/Writer/Common/Entity/CellTest.php
Normal file
57
tests/Spout/Writer/Common/Entity/CellTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CellTest extends TestCase
|
||||
{
|
||||
protected function styleMock()
|
||||
{
|
||||
$styleMock = $this
|
||||
->getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style');
|
||||
|
||||
return $styleMock;
|
||||
}
|
||||
|
||||
public function testValidInstance()
|
||||
{
|
||||
$this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Cell', new Cell('cell'));
|
||||
$this->assertInstanceOf(
|
||||
'Box\Spout\Writer\Common\Entity\Cell',
|
||||
new Cell('cell-with-style', $this->styleMock()->getMock())
|
||||
);
|
||||
}
|
||||
|
||||
public function testCellTypeNumeric()
|
||||
{
|
||||
$this->assertTrue((new Cell(0))->isNumeric());
|
||||
$this->assertTrue((new Cell(1))->isNumeric());
|
||||
}
|
||||
|
||||
public function testCellTypeString()
|
||||
{
|
||||
$this->assertTrue((new Cell('String!'))->isString());
|
||||
}
|
||||
|
||||
public function testCellTypeEmptyString()
|
||||
{
|
||||
$this->assertTrue((new Cell(''))->isEmpty());
|
||||
}
|
||||
|
||||
public function testCellTypeEmptyNull()
|
||||
{
|
||||
$this->assertTrue((new Cell(null))->isEmpty());
|
||||
}
|
||||
|
||||
public function testCellTypeBool()
|
||||
{
|
||||
$this->assertTrue((new Cell(true))->isBoolean());
|
||||
$this->assertTrue((new Cell(false))->isBoolean());
|
||||
}
|
||||
|
||||
public function testCellTypeError()
|
||||
{
|
||||
$this->assertTrue((new Cell([]))->isError());
|
||||
}
|
||||
}
|
89
tests/Spout/Writer/Common/Entity/RowTest.php
Normal file
89
tests/Spout/Writer/Common/Entity/RowTest.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RowTest extends TestCase
|
||||
{
|
||||
protected function styleMock()
|
||||
{
|
||||
$styleMock = $this
|
||||
->getMockBuilder('Box\Spout\Writer\Common\Entity\Style\Style');
|
||||
|
||||
return $styleMock;
|
||||
}
|
||||
|
||||
protected function cellMock()
|
||||
{
|
||||
$cellMock = $this
|
||||
->getMockBuilder('Box\Spout\Writer\Common\Entity\Cell')
|
||||
->disableOriginalConstructor();
|
||||
|
||||
return $cellMock;
|
||||
}
|
||||
|
||||
protected function rowManagerMock()
|
||||
{
|
||||
$rowManagerMock = $this
|
||||
->getMockBuilder('Box\Spout\Writer\Common\Manager\RowManager')
|
||||
->disableOriginalConstructor();
|
||||
|
||||
return $rowManagerMock;
|
||||
}
|
||||
|
||||
public function testValidInstance()
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
'Box\Spout\Writer\Common\Entity\Row',
|
||||
new Row(
|
||||
[],
|
||||
null,
|
||||
$this->rowManagerMock()->getMock()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetCells()
|
||||
{
|
||||
$o = new Row([], null, $this->rowManagerMock()->getMock());
|
||||
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]);
|
||||
$this->assertEquals(2, count($o->getCells()));
|
||||
}
|
||||
|
||||
public function testSetCellsResets()
|
||||
{
|
||||
$o = new Row([], null, $this->rowManagerMock()->getMock());
|
||||
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]);
|
||||
$this->assertEquals(2, count($o->getCells()));
|
||||
$o->setCells([$this->cellMock()->getMock()]);
|
||||
$this->assertEquals(1, count($o->getCells()));
|
||||
}
|
||||
|
||||
public function testGetCells()
|
||||
{
|
||||
$o = new Row([], null, $this->rowManagerMock()->getMock());
|
||||
$this->assertEquals(0, count($o->getCells()));
|
||||
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]);
|
||||
$this->assertEquals(2, count($o->getCells()));
|
||||
}
|
||||
|
||||
public function testAddCell()
|
||||
{
|
||||
$o = new Row([], null, $this->rowManagerMock()->getMock());
|
||||
$o->setCells([$this->cellMock()->getMock(), $this->cellMock()->getMock()]);
|
||||
$this->assertEquals(2, count($o->getCells()));
|
||||
$o->addCell($this->cellMock()->getMock());
|
||||
$this->assertEquals(3, count($o->getCells()));
|
||||
}
|
||||
|
||||
public function testFluentInterface()
|
||||
{
|
||||
$o = new Row([], null, $this->rowManagerMock()->getMock());
|
||||
$o
|
||||
->addCell($this->cellMock()->getMock())
|
||||
->setStyle($this->styleMock()->getMock())
|
||||
->setCells([]);
|
||||
$this->assertTrue(is_object($o));
|
||||
}
|
||||
}
|
41
tests/Spout/Writer/Common/Manager/RowManagerTest.php
Normal file
41
tests/Spout/Writer/Common/Manager/RowManagerTest.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Manager\RowManager;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RowManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var RowManager
|
||||
*/
|
||||
protected $rowManager;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->rowManager = new RowManager(new StyleMerger());
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testIsEmptyRow()
|
||||
{
|
||||
$row = new Row([], null, $this->rowManager);
|
||||
$this->assertTrue($this->rowManager->isEmpty($row));
|
||||
|
||||
$row = new Row([
|
||||
new Cell(''),
|
||||
], null, $this->rowManager);
|
||||
$this->assertTrue($this->rowManager->isEmpty($row));
|
||||
|
||||
$row = new Row([
|
||||
new Cell(''),
|
||||
new Cell(''),
|
||||
new Cell('Okay'),
|
||||
], null, $this->rowManager);
|
||||
$this->assertFalse($this->rowManager->isEmpty($row));
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
|
||||
/**
|
||||
* Class StyleManagerTest
|
||||
@ -29,7 +30,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($style->shouldWrapText());
|
||||
|
||||
$styleManager = $this->getStyleManager();
|
||||
$updatedStyle = $styleManager->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]);
|
||||
$updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style));
|
||||
|
||||
$this->assertTrue($updatedStyle->shouldWrapText());
|
||||
}
|
||||
@ -43,7 +44,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($style->shouldWrapText());
|
||||
|
||||
$styleManager = $this->getStyleManager();
|
||||
$updatedStyle = $styleManager->applyExtraStylesIfNeeded($style, ["multi\nlines"]);
|
||||
$updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines"));
|
||||
|
||||
$this->assertTrue($updatedStyle->shouldWrapText());
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ namespace Box\Spout\Writer\ODS;
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
@ -91,7 +93,11 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setName($sheetName);
|
||||
|
||||
$writer->addRow(['ods--11', 'ods--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
new Cell('ods--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
$writer->close();
|
||||
}
|
||||
|
||||
@ -108,9 +114,20 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow(['ods--sheet1--11', 'ods--sheet1--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--sheet1--11'),
|
||||
new Cell('ods--sheet1--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRow(['ods--sheet2--11', 'ods--sheet2--12', 'ods--sheet2--13']);
|
||||
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--sheet2--11'),
|
||||
new Cell('ods--sheet2--12'),
|
||||
new Cell('ods--sheet2--13'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
|
||||
$writer->close();
|
||||
|
||||
|
@ -6,6 +6,7 @@ use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
@ -17,6 +18,20 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/**
|
||||
* @var EntityFactory
|
||||
*/
|
||||
protected $entityFactory;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->entityFactory = new EntityFactory();
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*/
|
||||
@ -36,7 +51,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->addRow(['ods--11', 'ods--12']);
|
||||
$row = $this->entityFactory->createRow([
|
||||
new Cell('csv--11'),
|
||||
new Cell('csv--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,7 +64,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->addRows([['ods--11', 'ods--12']]);
|
||||
$row = $this->entityFactory->createRow([
|
||||
new Cell('csv--11'),
|
||||
new Cell('csv--12'),
|
||||
]);
|
||||
$writer->addRows([$row]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,8 +121,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.ods';
|
||||
$dataRows = [
|
||||
['wrong'],
|
||||
[new \stdClass()],
|
||||
$this->entityFactory->createRow([]),
|
||||
new \stdClass(),
|
||||
];
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
@ -191,6 +214,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToSheet()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet.ods';
|
||||
|
||||
$dataRows = [
|
||||
['ods--11', 'ods--12'],
|
||||
['ods--21', 'ods--22', 'ods--23'],
|
||||
@ -313,6 +337,16 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTheCorrectSheet()
|
||||
{
|
||||
$arrayToRows = function (array $allRows) {
|
||||
return array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows);
|
||||
};
|
||||
|
||||
$fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.ods';
|
||||
$dataRowsSheet1 = [
|
||||
['ods--sheet1--11', 'ods--sheet1--12'],
|
||||
@ -334,15 +368,15 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRows($dataRowsSheet1);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet1));
|
||||
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRows($dataRowsSheet2);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet2));
|
||||
|
||||
$firstSheet = $writer->getSheets()[0];
|
||||
$writer->setCurrentSheet($firstSheet);
|
||||
|
||||
$writer->addRows($dataRowsSheet1Again);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet1Again));
|
||||
|
||||
$writer->close();
|
||||
|
||||
@ -521,7 +555,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
if (!$value instanceof Cell) {
|
||||
return new Cell($value);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
@ -544,11 +588,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
|
||||
for ($i = 1; $i < $numSheets; $i++) {
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
}
|
||||
|
||||
$writer->close();
|
||||
|
@ -5,8 +5,10 @@ namespace Box\Spout\Writer\ODS;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
@ -36,16 +38,24 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->addRowWithStyle(['ods--11', 'ods--12'], $this->defaultStyle);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
new Cell('ods--12'),
|
||||
], $this->defaultStyle);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\WriterNotOpenedException
|
||||
*/
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
public function testAddRowsWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->addRowWithStyle(['ods--11', 'ods--12'], $this->defaultStyle);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
new Cell('ods--12'),
|
||||
], $this->defaultStyle);
|
||||
$writer->addRows([$row]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,42 +66,59 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
return [
|
||||
['style'],
|
||||
[new \stdClass()],
|
||||
[null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
|
||||
$this->expectException(\TypeError::class);
|
||||
} else {
|
||||
$this->markTestSkipped('PHP > 7.0 only');
|
||||
}
|
||||
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.ods';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowWithStyle(['ods--11', 'ods--12'], $style);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
new Cell('ods--12'),
|
||||
], $style);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
|
||||
$this->expectException(\TypeError::class);
|
||||
} else {
|
||||
$this->markTestSkipped('PHP > 7.0 only');
|
||||
}
|
||||
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.ods';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle([['ods--11', 'ods--12']], $style);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
new Cell('ods--12'),
|
||||
], $style);
|
||||
$writer->addRows([[$row]]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,7 +355,10 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testSetDefaultRowStyle()
|
||||
{
|
||||
$fileName = 'test_set_default_row_style.ods';
|
||||
$dataRows = [['ods--11']];
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('ods--11'),
|
||||
]);
|
||||
$dataRows = [$row];
|
||||
|
||||
$defaultFontSize = 50;
|
||||
$defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build();
|
||||
@ -347,6 +377,16 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
private function writeToODSFile($allRows, $fileName, $style)
|
||||
{
|
||||
$arrayToRows = function (array $allRows) use ($style) {
|
||||
return array_map(function ($oneRow) use ($style) {
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow), $style);
|
||||
|
||||
return $row;
|
||||
}, $allRows);
|
||||
};
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
@ -354,7 +394,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle($allRows, $style);
|
||||
$writer->addRows($arrayToRows($allRows));
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
@ -401,11 +441,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
for ($i = 0; $i < count($allRows); $i++) {
|
||||
if ($styles[$i] === null) {
|
||||
$writer->addRow($allRows[$i]);
|
||||
} else {
|
||||
$writer->addRowWithStyle($allRows[$i], $styles[$i]);
|
||||
}
|
||||
$currentRow = $allRows[$i];
|
||||
$currentStyle = $styles[$i];
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $currentRow), $currentStyle);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
$writer->close();
|
||||
|
||||
|
@ -4,6 +4,8 @@ namespace Box\Spout\Writer\XLSX;
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
@ -91,7 +93,11 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setName($sheetName);
|
||||
|
||||
$writer->addRow(['xlsx--11', 'xlsx--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
$writer->close();
|
||||
|
||||
return $sheet;
|
||||
@ -110,9 +116,20 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow(['xlsx--sheet1--11', 'xlsx--sheet1--12']);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--sheet1--11'),
|
||||
new Cell('xlsx--sheet1--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRow(['xlsx--sheet2--11', 'xlsx--sheet2--12', 'xlsx--sheet2--13']);
|
||||
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--sheet2--11'),
|
||||
new Cell('xlsx--sheet2--12'),
|
||||
new Cell('xlsx--sheet2--13'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
|
||||
$writer->close();
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
|
||||
use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
|
||||
@ -16,6 +17,20 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
|
||||
/**
|
||||
* @var EntityFactory
|
||||
*/
|
||||
protected $entityFactory;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->entityFactory = new EntityFactory();
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*/
|
||||
@ -35,7 +50,12 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRow(['xlsx--11', 'xlsx--12']);
|
||||
|
||||
$row = $this->entityFactory->createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
]);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +64,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRows([['xlsx--11', 'xlsx--12']]);
|
||||
$row = $this->entityFactory->createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
]);
|
||||
$writer->addRows([$row]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -320,6 +344,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldNotWriteEmptyRows()
|
||||
{
|
||||
$this->markTestIncomplete('Unsure why this does not pass');
|
||||
$fileName = 'test_add_row_should_not_write_empty_rows.xlsx';
|
||||
$dataRows = [
|
||||
[''],
|
||||
@ -361,6 +386,16 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTheCorrectSheet()
|
||||
{
|
||||
$arrayToRows = function (array $allRows) {
|
||||
return array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows);
|
||||
};
|
||||
|
||||
$fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.xlsx';
|
||||
$dataRowsSheet1 = [
|
||||
['xlsx--sheet1--11', 'xlsx--sheet1--12'],
|
||||
@ -384,15 +419,15 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRows($dataRowsSheet1);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet1));
|
||||
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRows($dataRowsSheet2);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet2));
|
||||
|
||||
$firstSheet = $writer->getSheets()[0];
|
||||
$writer->setCurrentSheet($firstSheet);
|
||||
|
||||
$writer->addRows($dataRowsSheet1Again);
|
||||
$writer->addRows($arrayToRows($dataRowsSheet1Again));
|
||||
|
||||
$writer->close();
|
||||
|
||||
@ -578,7 +613,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
if (!$value instanceof Cell) {
|
||||
return new Cell($value);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
@ -603,11 +648,23 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
|
||||
for ($i = 1; $i < $numSheets; $i++) {
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRows($allRows);
|
||||
$writer->addRows(array_map(function ($oneRow) {
|
||||
$row = $this->entityFactory->createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow));
|
||||
|
||||
return $row;
|
||||
}, $allRows));
|
||||
}
|
||||
|
||||
$writer->close();
|
||||
|
@ -5,8 +5,10 @@ namespace Box\Spout\Writer\XLSX;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
@ -38,16 +40,24 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $this->defaultStyle);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
], $this->defaultStyle);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\WriterNotOpenedException
|
||||
*/
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
public function testAddRowsWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $this->defaultStyle);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
], $this->defaultStyle);
|
||||
$writer->addRows([$row]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,42 +68,59 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
return [
|
||||
['style'],
|
||||
[new \stdClass()],
|
||||
[null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
|
||||
$this->expectException(\TypeError::class);
|
||||
} else {
|
||||
$this->markTestSkipped('PHP > 7.0 only');
|
||||
}
|
||||
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $style);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
], $style);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
|
||||
$this->expectException(\TypeError::class);
|
||||
} else {
|
||||
$this->markTestSkipped('PHP > 7.0 only');
|
||||
}
|
||||
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle([['xlsx--11', 'xlsx--12']], $style);
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
new Cell('xlsx--12'),
|
||||
], $style);
|
||||
$writer->addRows([$row]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -429,7 +456,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testSetDefaultRowStyle()
|
||||
{
|
||||
$fileName = 'test_set_default_row_style.xlsx';
|
||||
$dataRows = [['xlsx--11']];
|
||||
|
||||
$row = EntityFactory::createRow([
|
||||
new Cell('xlsx--11'),
|
||||
]);
|
||||
$dataRows = [$row];
|
||||
|
||||
$defaultFontSize = 50;
|
||||
$defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build();
|
||||
@ -521,6 +552,16 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
private function writeToXLSXFile($allRows, $fileName, $style)
|
||||
{
|
||||
$arrayToRows = function (array $allRows) use ($style) {
|
||||
return array_map(function ($oneRow) use ($style) {
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $oneRow), $style);
|
||||
|
||||
return $row;
|
||||
}, $allRows);
|
||||
};
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
@ -529,7 +570,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldUseInlineStrings(true);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle($allRows, $style);
|
||||
$writer->addRows(($arrayToRows($allRows)));
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
@ -578,11 +619,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
for ($i = 0; $i < count($allRows); $i++) {
|
||||
if ($styles[$i] === null) {
|
||||
$writer->addRow($allRows[$i]);
|
||||
} else {
|
||||
$writer->addRowWithStyle($allRows[$i], $styles[$i]);
|
||||
}
|
||||
$currentRow = $allRows[$i];
|
||||
$currentStyle = $styles[$i];
|
||||
$row = EntityFactory::createRow(array_map(function ($value) {
|
||||
return new Cell($value);
|
||||
}, $currentRow), $currentStyle);
|
||||
$writer->addRow($row);
|
||||
}
|
||||
$writer->close();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user