From 82170a058b945999f8e7c4d5b3ad9ca4b2ef4f7f Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 13 Dec 2019 00:12:09 +0100 Subject: [PATCH 01/18] Add XLSX support for default column width/row height as well as custom column widths through writer or OptionsManager (and test for it) --- src/Spout/Writer/Common/Entity/Options.php | 5 + .../Manager/WorkbookManagerAbstract.php | 44 ++++++- .../Manager/WorkbookManagerInterface.php | 15 +++ .../Writer/WriterMultiSheetsAbstract.php | 38 +++++- .../Writer/XLSX/Manager/OptionsManager.php | 3 + .../Writer/XLSX/Manager/WorksheetManager.php | 97 +++++++++++++- tests/Spout/Writer/XLSX/SheetTest.php | 119 ++++++++++++++++++ 7 files changed, 311 insertions(+), 10 deletions(-) diff --git a/src/Spout/Writer/Common/Entity/Options.php b/src/Spout/Writer/Common/Entity/Options.php index d7152bb6c..391833345 100644 --- a/src/Spout/Writer/Common/Entity/Options.php +++ b/src/Spout/Writer/Common/Entity/Options.php @@ -20,4 +20,9 @@ abstract class Options // XLSX specific options const SHOULD_USE_INLINE_STRINGS = 'shouldUseInlineStrings'; + + // Cell size options + const DEFAULT_COLUMN_WIDTH = 'defaultColumnWidth'; + const DEFAULT_ROW_HEIGHT = 'defaultRowHeight'; + const COLUMN_WIDTHS = 'columnWidthDefinition'; } diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 7be5c6e1f..12c65238c 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -103,7 +103,6 @@ public function getWorkbook() * Creates a new sheet in the workbook and make it the current sheet. * The writing will resume where it stopped (i.e. data won't be truncated). * - * @throws IOException If unable to open the sheet for writing * @return Worksheet The created sheet */ public function addNewSheetAndMakeItCurrent() @@ -117,8 +116,8 @@ public function addNewSheetAndMakeItCurrent() /** * Creates a new sheet in the workbook. The current sheet remains unchanged. * - * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing * @return Worksheet The created sheet + * @throws IOException */ private function addNewSheet() { @@ -157,6 +156,16 @@ public function getCurrentWorksheet() return $this->currentWorksheet; } + /** + * Starts the current sheet and opens the file pointer + * + * @throws IOException + */ + public function startCurrentSheet() + { + $this->worksheetManager->startSheet($this->getCurrentWorksheet()); + } + /** * Sets the given sheet as the current one. New data will be written to this sheet. * The writing will resume where it stopped (i.e. data won't be truncated). @@ -210,9 +219,10 @@ private function getWorksheetFromExternalSheet($sheet) * with the creation of new worksheets if one worksheet has reached its maximum capicity. * * @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 + * @throws IOException If trying to create a new sheet and unable to open the sheet for writing + * @throws \Box\Spout\Common\Exception\InvalidArgumentException */ public function addRowToCurrentWorksheet(Row $row) { @@ -249,8 +259,10 @@ private function hasCurrentWorksheetReachedMaxRows() * * @param Worksheet $worksheet Worksheet to write the row to * @param Row $row The row to be added - * @throws WriterException If unable to write data + * * @return void + * @throws IOException + * @throws \Box\Spout\Common\Exception\InvalidArgumentException */ private function addRowToWorksheet(Worksheet $worksheet, Row $row) { @@ -276,6 +288,28 @@ private function applyDefaultRowStyle(Row $row) } } + /** + * @param float|null $width + */ + public function setDefaultColumnWidth(float $width) { + $this->worksheetManager->setDefaultColumnWidth($width); + } + + /** + * @param float|null $height + */ + public function setDefaultRowHeight(float $height) { + $this->worksheetManager->setDefaultRowHeight($height); + } + + /** + * @param float|null $width + * @param array $columns One or more columns with this width + */ + public function setColumnWidth($width, ...$columns) { + $this->worksheetManager->setColumnWidth($width, ...$columns); + } + /** * Closes the workbook and all its associated sheets. * All the necessary files are written to disk and zipped together to create the final file. diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php index aed304a02..7bb469eaf 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php @@ -42,6 +42,21 @@ public function getWorksheets(); */ public function getCurrentWorksheet(); + /** + * Starts the current sheet and opens its file pointer + */ + public function startCurrentSheet(); + + /** + * @param float $width + */ + public function setDefaultColumnWidth(float $width); + + /** + * @param float $height + */ + public function setDefaultRowHeight(float $height); + /** * Sets the given sheet as the current one. New data will be written to this sheet. * The writing will resume where it stopped (i.e. data won't be truncated). diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 8170b679c..6148738c0 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -4,6 +4,7 @@ use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Entity\Row; +use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; @@ -96,8 +97,9 @@ public function getSheets() /** * Creates a new sheet and make it the current sheet. The data will now be written to this sheet. * - * @throws WriterNotOpenedException If the writer has not been opened yet * @return Sheet The created sheet + * @throws IOException + * @throws WriterNotOpenedException If the writer has not been opened yet */ public function addNewSheetAndMakeItCurrent() { @@ -135,6 +137,36 @@ public function setCurrentSheet($sheet) $this->workbookManager->setCurrentSheet($sheet); } + /** + * @param float $width + * @throws WriterNotOpenedException + */ + public function setDefaultColumnWidth(float $width) + { + $this->throwIfWorkbookIsNotAvailable(); + $this->workbookManager->setDefaultColumnWidth($width); + } + + /** + * @param float $height + * @throws WriterNotOpenedException + */ + public function setDefaultRowHeight(float $height) + { + $this->throwIfWorkbookIsNotAvailable(); + $this->workbookManager->setDefaultRowHeight($height); + } + + /** + * @param float|null $width + * @param array $columns One or more columns with this width + * @throws WriterNotOpenedException + */ + public function setColumnWidth($width, ...$columns) { + $this->throwIfWorkbookIsNotAvailable(); + $this->workbookManager->setColumnWidth($width, ...$columns); + } + /** * Checks if the workbook has been created. Throws an exception if not created yet. * @@ -143,13 +175,15 @@ public function setCurrentSheet($sheet) */ protected function throwIfWorkbookIsNotAvailable() { - if (!$this->workbookManager->getWorkbook()) { + if (empty($this->workbookManager) || !$this->workbookManager->getWorkbook()) { throw new WriterNotOpenedException('The writer must be opened before performing this action.'); } } /** * {@inheritdoc} + * + * @throws Exception\WriterException */ protected function addRowToWriter(Row $row) { diff --git a/src/Spout/Writer/XLSX/Manager/OptionsManager.php b/src/Spout/Writer/XLSX/Manager/OptionsManager.php index 53718bcff..d83b8aefe 100644 --- a/src/Spout/Writer/XLSX/Manager/OptionsManager.php +++ b/src/Spout/Writer/XLSX/Manager/OptionsManager.php @@ -39,6 +39,9 @@ protected function getSupportedOptions() Options::DEFAULT_ROW_STYLE, Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, Options::SHOULD_USE_INLINE_STRINGS, + Options::DEFAULT_COLUMN_WIDTH, + Options::DEFAULT_ROW_HEIGHT, + Options::COLUMN_WIDTHS, ]; } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index b0e458f0d..f6ee8313d 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -62,6 +62,18 @@ class WorksheetManager implements WorksheetManagerInterface /** @var InternalEntityFactory Factory to create entities */ private $entityFactory; + /** @var float|null The default column width to use */ + private $defaultColumnWidth; + + /** @var float|null The default row height to use */ + private $defaultRowHeight; + + /** @var bool Whether rows have been written */ + private $hasWrittenRows = false; + + /** @var array Array of min-max-width arrays */ + private $columnWidths; + /** * WorksheetManager constructor. * @@ -85,6 +97,9 @@ public function __construct( InternalEntityFactory $entityFactory ) { $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); + $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); + $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); + $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; $this->rowManager = $rowManager; $this->styleManager = $styleManager; $this->styleMerger = $styleMerger; @@ -102,6 +117,39 @@ public function getSharedStringsManager() return $this->sharedStringsManager; } + /** + * @param float|null $width + */ + public function setDefaultColumnWidth($width) { + $this->defaultColumnWidth = $width; + } + + /** + * @param float|null $height + */ + public function setDefaultRowHeight($height) { + $this->defaultRowHeight = $height; + } + + /** + * @param float|null $width + * @param array $columns One or more columns with this width + */ + public function setColumnWidth($width, ...$columns) { + // Gather sequences + $sequence = []; + foreach ($columns as $i) { + $sequenceLength = count($sequence); + $previousValue = $sequence[$sequenceLength - 1]; + if ($sequenceLength > 0 && $i !== $previousValue + 1) { + $this->columnWidths[] = [$sequence[0], $previousValue, $width]; + $sequence = []; + } + $sequence[] = $i; + } + $this->columnWidths[] = [$sequence[0], $sequence[count($sequence) - 1], $width]; + } + /** * {@inheritdoc} */ @@ -113,7 +161,6 @@ public function startSheet(Worksheet $worksheet) $worksheet->setFilePointer($sheetFilePointer); fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); - fwrite($sheetFilePointer, ''); } /** @@ -153,6 +200,12 @@ public function addRow(Worksheet $worksheet, Row $row) */ private function addNonEmptyRow(Worksheet $worksheet, Row $row) { + $sheetFilePointer = $worksheet->getFilePointer(); + if (!$this->hasWrittenRows) { + fwrite($sheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); + fwrite($sheetFilePointer, $this->getXMLFragmentForColumnWidths()); + fwrite($sheetFilePointer, ''); + } $cellIndex = 0; $rowStyle = $row->getStyle(); $rowIndex = $worksheet->getLastWrittenRowIndex() + 1; @@ -167,10 +220,11 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row) $rowXML .= ''; - $wasWriteSuccessful = fwrite($worksheet->getFilePointer(), $rowXML); + $wasWriteSuccessful = fwrite($sheetFilePointer, $rowXML); if ($wasWriteSuccessful === false) { throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); } + $this->hasWrittenRows = true; } /** @@ -256,6 +310,41 @@ private function getCellXMLFragmentForNonEmptyString($cellValue) return $cellXMLFragment; } + /** + * Construct column width references xml to inject into worksheet xml file + * + * @return string + */ + public function getXMLFragmentForColumnWidths() + { + if (empty($this->columnWidths)) { + return ''; + } + $xml = ''; + foreach ($this->columnWidths as $entry) { + $xml .= ''; + } + $xml .= ''; + return $xml; + } + + /** + * Constructs default row height and width xml to inject into worksheet xml file + * + * @return string + */ + public function getXMLFragmentForDefaultCellSizing() + { + $rowHeightXml = empty($this->defaultRowHeight) ? '' : " defaultRowHeight=\"{$this->defaultRowHeight}\""; + $colWidthXml = empty($this->defaultColumnWidth) ? '' : " defaultColWidth=\"{$this->defaultColumnWidth}\""; + if (empty($colWidthXml) && empty($rowHeightXml)) { + return ''; + } + // Ensure that the required defaultRowHeight is set + $rowHeightXml = empty($rowHeightXml) ? ' defaultRowHeight="0"' : $rowHeightXml; + return ""; + } + /** * {@inheritdoc} */ @@ -267,7 +356,9 @@ public function close(Worksheet $worksheet) return; } - fwrite($worksheetFilePointer, ''); + if ($this->hasWrittenRows) { + fwrite($worksheetFilePointer, ''); + } fwrite($worksheetFilePointer, ''); fclose($worksheetFilePointer); } diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index cebd72ea4..bf1bbb87c 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -6,6 +6,7 @@ use Box\Spout\Writer\Common\Creator\WriterEntityFactory; use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Exception\InvalidSheetNameException; +use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\RowCreationHelper; use PHPUnit\Framework\TestCase; @@ -92,6 +93,124 @@ public function testSetSheetVisibilityShouldCreateSheetHidden() $this->assertContains(' state="hidden"', $xmlContents, 'The sheet visibility should have been changed to "hidden"'); } + public function testThrowsIfWorkbookIsNotInitialized() + { + $this->expectException(WriterNotOpenedException::class); + $writer = WriterEntityFactory::createXLSXWriter(); + + $writer->addRow($this->createRowFromValues([])); + } + + public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() + { + $this->expectException(WriterNotOpenedException::class); + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->setDefaultColumnWidth(10.0); + } + + public function testWritesDefaultCellSizesIfSet() + { + $fileName = 'test_writes_default_cell_sizes_if_set.xlsx'; + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setDefaultColumnWidth(10.0); + $writer->setDefaultRowHeight(10.0); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet'); + $this->assertContains(' defaultRowHeight="10', $xmlContents, 'No default row height found in sheet'); + } + + public function testWritesDefaultRequiredRowHeightIfOmitted() + { + $fileName = 'test_writes_default_required_row_height_if_omitted.xlsx'; + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setDefaultColumnWidth(10.0); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet'); + $this->assertContains(' defaultRowHeight="0', $xmlContents, 'No default row height found in sheet'); + } + + public function testWritesColumnWidths() + { + $fileName = 'test_column_widths.xlsx'; + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setColumnWidth(100.0, 1); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains('createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setColumnWidth(100.0, 1, 2, 3); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains('createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setColumnWidth(50.0, 1, 3, 4, 6); + $writer->setColumnWidth(100.0, 2, 5); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13', 'xlsx--14', 'xlsx--15', 'xlsx--16'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains('assertContains('assertContains('assertContains('assertContains(' Date: Fri, 13 Dec 2019 12:38:00 +0100 Subject: [PATCH 02/18] Add support for setting column width by range --- .../Manager/WorkbookManagerAbstract.php | 13 +++++++++++-- .../Writer/WriterMultiSheetsAbstract.php | 11 +++++++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 17 +++++++++++++---- tests/Spout/Writer/XLSX/SheetTest.php | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 12c65238c..721acb484 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -303,13 +303,22 @@ public function setDefaultRowHeight(float $height) { } /** - * @param float|null $width + * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth($width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) { $this->worksheetManager->setColumnWidth($width, ...$columns); } + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + */ + public function setColumnWidthForRange(float $width, int $start, int $end) { + $this->worksheetManager->setColumnWidthForRange($width, $start, $end); + } + /** * Closes the workbook and all its associated sheets. * All the necessary files are written to disk and zipped together to create the final file. diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 6148738c0..c0519ea2c 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -167,6 +167,17 @@ public function setColumnWidth($width, ...$columns) { $this->workbookManager->setColumnWidth($width, ...$columns); } + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + * @throws WriterNotOpenedException + */ + public function setColumnWidthForRange(float $width, int $start, int $end) { + $this->throwIfWorkbookIsNotAvailable(); + $this->workbookManager->setColumnWidthForRange($width, $start, $end); + } + /** * Checks if the workbook has been created. Throws an exception if not created yet. * diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index f6ee8313d..bcf75cb6b 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -132,22 +132,31 @@ public function setDefaultRowHeight($height) { } /** - * @param float|null $width + * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth($width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) { // Gather sequences $sequence = []; foreach ($columns as $i) { $sequenceLength = count($sequence); $previousValue = $sequence[$sequenceLength - 1]; if ($sequenceLength > 0 && $i !== $previousValue + 1) { - $this->columnWidths[] = [$sequence[0], $previousValue, $width]; + $this->setColumnWidthForRange($width, $sequence[0], $previousValue); $sequence = []; } $sequence[] = $i; } - $this->columnWidths[] = [$sequence[0], $sequence[count($sequence) - 1], $width]; + $this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]); + } + + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + */ + public function setColumnWidthForRange(float $width, int $start, int $end) { + $this->columnWidths[] = [$start, $end, $width]; } /** diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index bf1bbb87c..7fa5f774a 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -211,6 +211,25 @@ public function testWritesMultipleColumnWidthsInRanges() $this->assertContains('createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setColumnWidthForRange(50.0, 1, 3); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains(' Date: Fri, 13 Dec 2019 12:43:36 +0100 Subject: [PATCH 03/18] PSR-2 code style --- .../Manager/WorkbookManagerAbstract.php | 14 ++++++---- .../Writer/WriterMultiSheetsAbstract.php | 18 ++++++------ .../Writer/XLSX/Manager/WorksheetManager.php | 28 +++++++++++-------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 721acb484..e1b41bb94 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -171,8 +171,8 @@ public function startCurrentSheet() * The writing will resume where it stopped (i.e. data won't be truncated). * * @param Sheet $sheet The "external" sheet to set as current - * @throws SheetNotFoundException If the given sheet does not exist in the workbook * @return void + * @throws SheetNotFoundException If the given sheet does not exist in the workbook */ public function setCurrentSheet(Sheet $sheet) { @@ -291,14 +291,16 @@ private function applyDefaultRowStyle(Row $row) /** * @param float|null $width */ - public function setDefaultColumnWidth(float $width) { + public function setDefaultColumnWidth(float $width) + { $this->worksheetManager->setDefaultColumnWidth($width); } /** * @param float|null $height */ - public function setDefaultRowHeight(float $height) { + public function setDefaultRowHeight(float $height) + { $this->worksheetManager->setDefaultRowHeight($height); } @@ -306,7 +308,8 @@ public function setDefaultRowHeight(float $height) { * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth(float $width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) + { $this->worksheetManager->setColumnWidth($width, ...$columns); } @@ -315,7 +318,8 @@ public function setColumnWidth(float $width, ...$columns) { * @param int $start First column index of the range * @param int $end Last column index of the range */ - public function setColumnWidthForRange(float $width, int $start, int $end) { + public function setColumnWidthForRange(float $width, int $start, int $end) + { $this->worksheetManager->setColumnWidthForRange($width, $start, $end); } diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index c0519ea2c..0bf3bf672 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -50,8 +50,8 @@ public function __construct( * This must be set before opening the writer. * * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached - * @throws WriterAlreadyOpenedException If the writer was already opened * @return WriterMultiSheetsAbstract + * @throws WriterAlreadyOpenedException If the writer was already opened */ public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) { @@ -76,8 +76,8 @@ protected function openWriter() /** * Returns all the workbook's sheets * - * @throws WriterNotOpenedException If the writer has not been opened yet * @return Sheet[] All the workbook's sheets + * @throws WriterNotOpenedException If the writer has not been opened yet */ public function getSheets() { @@ -112,8 +112,8 @@ public function addNewSheetAndMakeItCurrent() /** * Returns the current sheet * - * @throws WriterNotOpenedException If the writer has not been opened yet * @return Sheet The current sheet + * @throws WriterNotOpenedException If the writer has not been opened yet */ public function getCurrentSheet() { @@ -127,9 +127,9 @@ public function getCurrentSheet() * The writing will resume where it stopped (i.e. data won't be truncated). * * @param Sheet $sheet The sheet to set as current - * @throws WriterNotOpenedException If the writer has not been opened yet - * @throws SheetNotFoundException If the given sheet does not exist in the workbook * @return void + * @throws SheetNotFoundException If the given sheet does not exist in the workbook + * @throws WriterNotOpenedException If the writer has not been opened yet */ public function setCurrentSheet($sheet) { @@ -162,7 +162,8 @@ public function setDefaultRowHeight(float $height) * @param array $columns One or more columns with this width * @throws WriterNotOpenedException */ - public function setColumnWidth($width, ...$columns) { + public function setColumnWidth($width, ...$columns) + { $this->throwIfWorkbookIsNotAvailable(); $this->workbookManager->setColumnWidth($width, ...$columns); } @@ -173,7 +174,8 @@ public function setColumnWidth($width, ...$columns) { * @param int $end Last column index of the range * @throws WriterNotOpenedException */ - public function setColumnWidthForRange(float $width, int $start, int $end) { + public function setColumnWidthForRange(float $width, int $start, int $end) + { $this->throwIfWorkbookIsNotAvailable(); $this->workbookManager->setColumnWidthForRange($width, $start, $end); } @@ -181,8 +183,8 @@ public function setColumnWidthForRange(float $width, int $start, int $end) { /** * Checks if the workbook has been created. Throws an exception if not created yet. * - * @throws WriterNotOpenedException If the workbook is not created yet * @return void + * @throws WriterNotOpenedException If the workbook is not created yet */ protected function throwIfWorkbookIsNotAvailable() { diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index bcf75cb6b..ba682a535 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -120,14 +120,16 @@ public function getSharedStringsManager() /** * @param float|null $width */ - public function setDefaultColumnWidth($width) { + public function setDefaultColumnWidth($width) + { $this->defaultColumnWidth = $width; } /** * @param float|null $height */ - public function setDefaultRowHeight($height) { + public function setDefaultRowHeight($height) + { $this->defaultRowHeight = $height; } @@ -135,7 +137,8 @@ public function setDefaultRowHeight($height) { * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth(float $width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) + { // Gather sequences $sequence = []; foreach ($columns as $i) { @@ -155,7 +158,8 @@ public function setColumnWidth(float $width, ...$columns) { * @param int $start First column index of the range * @param int $end Last column index of the range */ - public function setColumnWidthForRange(float $width, int $start, int $end) { + public function setColumnWidthForRange(float $width, int $start, int $end) + { $this->columnWidths[] = [$start, $end, $width]; } @@ -176,8 +180,8 @@ public function startSheet(Worksheet $worksheet) * Checks if the sheet has been sucessfully created. Throws an exception if not. * * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file - * @throws IOException If the sheet data file cannot be opened for writing * @return void + * @throws IOException If the sheet data file cannot be opened for writing */ private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) { @@ -203,9 +207,9 @@ public function addRow(Worksheet $worksheet, Row $row) * * @param Worksheet $worksheet The worksheet to add the row to * @param Row $row The row to be written - * @throws IOException If the data cannot be written - * @throws InvalidArgumentException If a cell value's type is not supported * @return void + * @throws InvalidArgumentException If a cell value's type is not supported + * @throws IOException If the data cannot be written */ private function addNonEmptyRow(Worksheet $worksheet, Row $row) { @@ -244,8 +248,8 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row) * @param Style $rowStyle * @param int $rowIndex * @param int $cellIndex - * @throws InvalidArgumentException If the given value cannot be processed * @return string + * @throws InvalidArgumentException If the given value cannot be processed */ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndex, $cellIndex) { @@ -266,8 +270,8 @@ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndex, * @param int $cellNumber * @param Cell $cell * @param int $styleId - * @throws InvalidArgumentException If the given value cannot be processed * @return string + * @throws InvalidArgumentException If the given value cannot be processed */ private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) { @@ -278,7 +282,7 @@ private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) if ($cell->isString()) { $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); } elseif ($cell->isBoolean()) { - $cellXML .= ' t="b">' . (int) ($cell->getValue()) . ''; + $cellXML .= ' t="b">' . (int)($cell->getValue()) . ''; } elseif ($cell->isNumeric()) { $cellXML .= '>' . $cell->getValue() . ''; } elseif ($cell->isEmpty()) { @@ -300,8 +304,8 @@ private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) * Returns the XML fragment for a cell containing a non empty string * * @param string $cellValue The cell value - * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell * @return string The XML fragment representing the cell + * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell */ private function getCellXMLFragmentForNonEmptyString($cellValue) { @@ -331,7 +335,7 @@ public function getXMLFragmentForColumnWidths() } $xml = ''; foreach ($this->columnWidths as $entry) { - $xml .= ''; + $xml .= ''; } $xml .= ''; return $xml; From 09a624ef8e62e4c14acc53119d1575d11a96a967 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Mon, 16 Dec 2019 20:01:48 +0100 Subject: [PATCH 04/18] Avoid $previousValue if length is < 1 --- src/Spout/Writer/XLSX/Manager/WorksheetManager.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index ba682a535..abfc4768e 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -143,10 +143,12 @@ public function setColumnWidth(float $width, ...$columns) $sequence = []; foreach ($columns as $i) { $sequenceLength = count($sequence); - $previousValue = $sequence[$sequenceLength - 1]; - if ($sequenceLength > 0 && $i !== $previousValue + 1) { - $this->setColumnWidthForRange($width, $sequence[0], $previousValue); - $sequence = []; + if ($sequenceLength > 0) { + $previousValue = $sequence[$sequenceLength - 1]; + if ($i !== $previousValue + 1) { + $this->setColumnWidthForRange($width, $sequence[0], $previousValue); + $sequence = []; + } } $sequence[] = $i; } From 6db98717226e0b7b746c1c21064e52814cb6a83a Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Tue, 17 Dec 2019 20:31:26 +0100 Subject: [PATCH 05/18] Extract common functionality to trait for easier reuse, fix default row height for XLSX --- .../Writer/Common/Manager/ManagesCellSize.php | 66 +++++++++++++++++++ .../Manager/WorksheetManagerInterface.php | 23 +++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 63 ++---------------- tests/Spout/Writer/XLSX/SheetTest.php | 5 +- 4 files changed, 97 insertions(+), 60 deletions(-) create mode 100644 src/Spout/Writer/Common/Manager/ManagesCellSize.php diff --git a/src/Spout/Writer/Common/Manager/ManagesCellSize.php b/src/Spout/Writer/Common/Manager/ManagesCellSize.php new file mode 100644 index 000000000..44cf2a12c --- /dev/null +++ b/src/Spout/Writer/Common/Manager/ManagesCellSize.php @@ -0,0 +1,66 @@ +defaultColumnWidth = $width; + } + + /** + * @param float|null $height + */ + public function setDefaultRowHeight($height) + { + $this->defaultRowHeight = $height; + } + + /** + * @param float $width + * @param array $columns One or more columns with this width + */ + public function setColumnWidth(float $width, ...$columns) + { + // Gather sequences + $sequence = []; + foreach ($columns as $i) { + $sequenceLength = count($sequence); + if ($sequenceLength > 0) { + $previousValue = $sequence[$sequenceLength - 1]; + if ($i !== $previousValue + 1) { + $this->setColumnWidthForRange($width, $sequence[0], $previousValue); + $sequence = []; + } + } + $sequence[] = $i; + } + $this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]); + } + + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + */ + public function setColumnWidthForRange(float $width, int $start, int $end) + { + $this->columnWidths[] = [$start, $end, $width]; + } + +} diff --git a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php index bb54ee643..bb6758a7f 100644 --- a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php @@ -11,6 +11,29 @@ */ interface WorksheetManagerInterface { + /** + * @param float|null $width + */ + public function setDefaultColumnWidth($width); + + /** + * @param float|null $height + */ + public function setDefaultRowHeight($height); + + /** + * @param float $width + * @param array $columns One or more columns with this width + */ + public function setColumnWidth(float $width, ...$columns); + + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + */ + public function setColumnWidthForRange(float $width, int $start, int $end); + /** * Adds a row to the worksheet. * diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index abfc4768e..3c7208d11 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -14,6 +14,7 @@ use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Helper\CellHelper; +use Box\Spout\Writer\Common\Manager\ManagesCellSize; use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; @@ -25,6 +26,8 @@ */ class WorksheetManager implements WorksheetManagerInterface { + use ManagesCellSize; + /** * Maximum number of characters a cell can contain * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] @@ -62,18 +65,9 @@ class WorksheetManager implements WorksheetManagerInterface /** @var InternalEntityFactory Factory to create entities */ private $entityFactory; - /** @var float|null The default column width to use */ - private $defaultColumnWidth; - - /** @var float|null The default row height to use */ - private $defaultRowHeight; - /** @var bool Whether rows have been written */ private $hasWrittenRows = false; - /** @var array Array of min-max-width arrays */ - private $columnWidths; - /** * WorksheetManager constructor. * @@ -117,54 +111,6 @@ public function getSharedStringsManager() return $this->sharedStringsManager; } - /** - * @param float|null $width - */ - public function setDefaultColumnWidth($width) - { - $this->defaultColumnWidth = $width; - } - - /** - * @param float|null $height - */ - public function setDefaultRowHeight($height) - { - $this->defaultRowHeight = $height; - } - - /** - * @param float $width - * @param array $columns One or more columns with this width - */ - public function setColumnWidth(float $width, ...$columns) - { - // Gather sequences - $sequence = []; - foreach ($columns as $i) { - $sequenceLength = count($sequence); - if ($sequenceLength > 0) { - $previousValue = $sequence[$sequenceLength - 1]; - if ($i !== $previousValue + 1) { - $this->setColumnWidthForRange($width, $sequence[0], $previousValue); - $sequence = []; - } - } - $sequence[] = $i; - } - $this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]); - } - - /** - * @param float $width The width to set - * @param int $start First column index of the range - * @param int $end Last column index of the range - */ - public function setColumnWidthForRange(float $width, int $start, int $end) - { - $this->columnWidths[] = [$start, $end, $width]; - } - /** * {@inheritdoc} */ @@ -226,7 +172,8 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row) $rowIndex = $worksheet->getLastWrittenRowIndex() + 1; $numCells = $row->getNumCells(); - $rowXML = ''; + $hasCustomHeight = $this->defaultRowHeight > 0 ? '1' : '0'; + $rowXML = ""; foreach ($row->getCells() as $cell) { $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex); diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index 7fa5f774a..91c28f952 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -117,7 +117,7 @@ public function testWritesDefaultCellSizesIfSet() $writer = WriterEntityFactory::createXLSXWriter(); $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); - $writer->setDefaultRowHeight(10.0); + $writer->setDefaultRowHeight(20.0); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close(); @@ -126,7 +126,8 @@ public function testWritesDefaultCellSizesIfSet() $this->assertContains('assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet'); - $this->assertContains(' defaultRowHeight="10', $xmlContents, 'No default row height found in sheet'); + $this->assertContains(' defaultRowHeight="20', $xmlContents, 'No default row height found in sheet'); + $this->assertContains(' customHeight="1"', $xmlContents, 'No row height override flag found in row'); } public function testWritesDefaultRequiredRowHeightIfOmitted() From 26ad59033c6adcbb4caa776d1f2a0d6b0eae93e4 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Tue, 17 Dec 2019 22:05:01 +0100 Subject: [PATCH 06/18] Add support for default cell sizes for ODS files --- .../Writer/ODS/Manager/Style/StyleManager.php | 13 ++++- .../Writer/ODS/Manager/WorksheetManager.php | 46 ++++++++++++++- tests/Spout/Writer/ODS/SheetTest.php | 56 ++++++++++++++++--- 3 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index 5b3bb0d5a..23af8265a 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -4,6 +4,7 @@ use Box\Spout\Common\Entity\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Worksheet; +use Box\Spout\Writer\Common\Manager\ManagesCellSize; use Box\Spout\Writer\ODS\Helper\BorderHelper; /** @@ -12,6 +13,8 @@ */ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager { + use ManagesCellSize; + /** @var StyleRegistry */ protected $styleRegistry; @@ -161,12 +164,16 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets) $content .= $this->getStyleSectionContent($style); } - $content .= <<<'EOD' + $useOptimalRowHeight = empty($this->defaultRowHeight) ? 'true' : 'false'; + $defaultRowHeight = empty($this->defaultRowHeight) ? '15pt' : "{$this->defaultRowHeight}pt"; + $defaultColumnWidth = empty($this->defaultColumnWidth) ? '' : "style:column-width=\"{$this->defaultColumnWidth}pt\""; + + $content .= << - + - + EOD; diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 6d76fa0b8..f0fc26e3d 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -9,6 +9,7 @@ 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\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; @@ -39,17 +40,25 @@ class WorksheetManager implements WorksheetManagerInterface * @param StyleMerger $styleMerger * @param ODSEscaper $stringsEscaper * @param StringHelper $stringHelper + * @param OptionsManager|null $optionsManager */ public function __construct( StyleManager $styleManager, StyleMerger $styleMerger, ODSEscaper $stringsEscaper, - StringHelper $stringHelper + StringHelper $stringHelper, + $optionsManager = null ) { $this->styleManager = $styleManager; $this->styleMerger = $styleMerger; $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; + + if ($optionsManager) { + $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); + $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); + $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; + } } /** @@ -229,4 +238,39 @@ public function close(Worksheet $worksheet) fclose($worksheetFilePointer); } + + /** + * @param float|null $width + */ + public function setDefaultColumnWidth($width) + { + $this->styleManager->setDefaultColumnWidth($width); + } + + /** + * @param float|null $height + */ + public function setDefaultRowHeight($height) + { + $this->styleManager->setDefaultRowHeight($height); + } + + /** + * @param float $width + * @param array $columns One or more columns with this width + */ + public function setColumnWidth(float $width, ...$columns) + { + $this->styleManager->setColumnWidth($width, ...$columns); + } + + /** + * @param float $width The width to set + * @param int $start First column index of the range + * @param int $end Last column index of the range + */ + public function setColumnWidthForRange(float $width, int $start, int $end) + { + $this->styleManager->setColumnWidthForRange($width, $start, $end); + } } diff --git a/tests/Spout/Writer/ODS/SheetTest.php b/tests/Spout/Writer/ODS/SheetTest.php index a964a591b..512b77eb0 100644 --- a/tests/Spout/Writer/ODS/SheetTest.php +++ b/tests/Spout/Writer/ODS/SheetTest.php @@ -6,6 +6,7 @@ use Box\Spout\Writer\Common\Creator\WriterEntityFactory; use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Exception\InvalidSheetNameException; +use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\RowCreationHelper; use PHPUnit\Framework\TestCase; @@ -82,7 +83,7 @@ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed() */ public function testSetSheetVisibilityShouldCreateSheetHidden() { - $fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx'; + $fileName = 'test_set_visibility_should_create_sheet_hidden.ods'; $this->writeDataToHiddenSheet($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); @@ -92,12 +93,41 @@ public function testSetSheetVisibilityShouldCreateSheetHidden() $this->assertContains(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"'); } - /** - * @param string $fileName - * @param string $sheetName - * @return Sheet - */ - private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName) + function testThrowsIfWorkbookIsNotInitialized() + { + $this->expectException(WriterNotOpenedException::class); + $writer = WriterEntityFactory::createODSWriter(); + + $writer->addRow($this->createRowFromValues([])); + } + + public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() + { + $this->expectException(WriterNotOpenedException::class); + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->setDefaultColumnWidth(10.0); + } + + public function testWritesDefaultCellSizesIfSet() + { + $fileName = 'test_writes_default_cell_sizes_if_set.ods'; + $writer = $this->writerForFile($fileName); + + $writer->setDefaultColumnWidth(100.0); + $writer->setDefaultRowHeight(20.0); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); + $writer->close(); + + $resourcePath = $this->getGeneratedResourcePath($fileName); + $pathToWorkbookFile = $resourcePath . '#content.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains(' style:column-width="100pt"', $xmlContents, 'No default col width found in sheet'); + $this->assertContains(' style:row-height="20pt"', $xmlContents, 'No default row height found in sheet'); + $this->assertContains(' style:use-optimal-row-height="false', $xmlContents, 'No optimal row height override found in sheet'); + } + + private function writerForFile($fileName) { $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); @@ -105,6 +135,18 @@ private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName) $writer = WriterEntityFactory::createODSWriter(); $writer->openToFile($resourcePath); + return $writer; + } + + /** + * @param string $fileName + * @param string $sheetName + * @return void + */ + private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName) + { + $writer = $this->writerForFile($fileName); + $sheet = $writer->getCurrentSheet(); $sheet->setName($sheetName); From ceda150aa3d7b72a0525c2a73f18ac7eeb63b739 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Thu, 19 Dec 2019 20:31:46 +0100 Subject: [PATCH 07/18] Rename default column style --- src/Spout/Writer/ODS/Manager/Style/StyleManager.php | 2 +- src/Spout/Writer/ODS/Manager/WorksheetManager.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index 23af8265a..d18efb644 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -169,7 +169,7 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets) $defaultColumnWidth = empty($this->defaultColumnWidth) ? '' : "style:column-width=\"{$this->defaultColumnWidth}pt\""; $content .= << + diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index f0fc26e3d..3b47a8b42 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -103,7 +103,7 @@ public function getTableElementStartAsString(Worksheet $worksheet) $tableStyleName = 'ta' . ($externalSheet->getIndex() + 1); $tableElement = ''; - $tableElement .= ''; + $tableElement .= ''; return $tableElement; } From db197de7f9116ff0d6bac983db90b32c1d532508 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Thu, 19 Dec 2019 22:35:55 +0100 Subject: [PATCH 08/18] Add support for custom column widths in ODS exports --- .../Writer/ODS/Manager/Style/StyleManager.php | 45 +++++++++++ .../Writer/ODS/Manager/WorksheetManager.php | 2 +- tests/Spout/Writer/ODS/SheetTest.php | 81 +++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index d18efb644..e56302857 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -188,6 +188,15 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets) EOD; } + // Sort column widths since ODS cares about order + usort($this->columnWidths, function($a, $b) { + if ($a[0] == $b[0]) { + return 0; + } + return ($a[0] < $b[0]) ? -1 : 1; + }); + $content .= $this->getTableColumnStylesXMLContent(); + $content .= ''; return $content; @@ -340,4 +349,40 @@ private function getBackgroundColorXMLContent($style) $style->getBackgroundColor() ); } + + public function getTableColumnStylesXMLContent(): string + { + if (empty($this->columnWidths)) { + return ''; + } + + $content = ''; + foreach ($this->columnWidths as $styleIndex => $entry) { + $content .= << + + +EOD; + } + return $content; + } + + public function getStyledTableColumnXMLContent(int $maxNumColumns): string + { + if (empty($this->columnWidths)) { + return ''; + } + + $content = ''; + foreach ($this->columnWidths as $styleIndex => $entry) { + $numCols = $entry[1] - $entry[0] + 1; + $content .= << +EOD; + } + // Note: This assumes the column widths are contiguous and default width is + // only applied to columns after the last custom column with a custom width + $content .= ''; + return $content; + } } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 3b47a8b42..6623d674f 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -103,7 +103,7 @@ public function getTableElementStartAsString(Worksheet $worksheet) $tableStyleName = 'ta' . ($externalSheet->getIndex() + 1); $tableElement = ''; - $tableElement .= ''; + $tableElement .= $this->styleManager->getStyledTableColumnXMLContent($worksheet->getMaxNumColumns()); return $tableElement; } diff --git a/tests/Spout/Writer/ODS/SheetTest.php b/tests/Spout/Writer/ODS/SheetTest.php index 512b77eb0..fac513a50 100644 --- a/tests/Spout/Writer/ODS/SheetTest.php +++ b/tests/Spout/Writer/ODS/SheetTest.php @@ -127,6 +127,87 @@ public function testWritesDefaultCellSizesIfSet() $this->assertContains(' style:use-optimal-row-height="false', $xmlContents, 'No optimal row height override found in sheet'); } + public function testWritesColumnWidths() + { + $fileName = 'test_column_widths.ods'; + $writer = $this->writerForFile($fileName); + + $writer->setColumnWidth(100.0, 1); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); + $writer->close(); + + $resourcePath = $this->getGeneratedResourcePath($fileName); + $pathToWorkbookFile = $resourcePath . '#content.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('', $xmlContents, 'No matching custom col style definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching table-column-properties found in sheet'); + $this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet'); + $this->assertContains('table:number-columns-repeated="1"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet'); + } + + public function testWritesMultipleColumnWidths() + { + $fileName = 'test_multiple_column_widths.ods'; + $writer = $this->writerForFile($fileName); + + $writer->setColumnWidth(100.0, 1, 2, 3); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13'])); + $writer->close(); + + $resourcePath = $this->getGeneratedResourcePath($fileName); + $pathToWorkbookFile = $resourcePath . '#content.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('', $xmlContents, 'No matching custom col style definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching table-column-properties found in sheet'); + $this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet'); + $this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet'); + } + + public function testWritesMultipleColumnWidthsInRanges() + { + $fileName = 'test_multiple_column_widths_in_ranges.ods'; + $writer = $this->writerForFile($fileName); + + $writer->setColumnWidth(50.0, 1, 3, 4, 6); + $writer->setColumnWidth(100.0, 2, 5); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13', 'ods--14', 'ods--15', 'ods--16'])); + $writer->close(); + + $resourcePath = $this->getGeneratedResourcePath($fileName); + $pathToWorkbookFile = $resourcePath . '#content.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('', $xmlContents, 'No matching custom col style 0 definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching custom col style 1 definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching custom col style 2 definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching custom col style 3 definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching custom col style 4 definition found in sheet'); + $this->assertContains('', $xmlContents, 'No matching table-column-properties found in sheet'); + $this->assertContains('', $xmlContents, 'No matching table-column-properties found in sheet'); + $this->assertContains('', $xmlContents, 'No matching table:number-columns-repeated count found in sheet'); + } + + public function testCanTakeColumnWidthsAsRange() + { + $fileName = 'test_column_widths_as_ranges.ods'; + $writer = $this->writerForFile($fileName); + + $writer->setColumnWidthForRange(150.0, 1, 3); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13'])); + $writer->close(); + + $resourcePath = $this->getGeneratedResourcePath($fileName); + $pathToWorkbookFile = $resourcePath . '#content.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('', $xmlContents, 'No matching custom col style 0 definition found in sheet'); + $this->assertContains('style:column-width="150pt"/>', $xmlContents, 'No matching table-column-properties found in sheet'); + $this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet'); + $this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet'); + } + private function writerForFile($fileName) { $this->createGeneratedFolderIfNeeded($fileName); From d2dadd4d1767353d4729297763b9c6cc7b6c6e28 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Thu, 19 Dec 2019 22:55:23 +0100 Subject: [PATCH 09/18] Ignore generated test files in subdirectories --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 048320b4d..c05524696 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ /.idea *.iml -/tests/resources/generated +/tests/**/resources/generated /tests/coverage /vendor /composer.lock From aa5ec507c23cd770d557c8cf9538dd91ab11202b Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Thu, 19 Dec 2019 23:05:16 +0100 Subject: [PATCH 10/18] Set empty array as default for column widths --- src/Spout/Writer/Common/Manager/ManagesCellSize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spout/Writer/Common/Manager/ManagesCellSize.php b/src/Spout/Writer/Common/Manager/ManagesCellSize.php index 44cf2a12c..60de4cf72 100644 --- a/src/Spout/Writer/Common/Manager/ManagesCellSize.php +++ b/src/Spout/Writer/Common/Manager/ManagesCellSize.php @@ -13,7 +13,7 @@ trait ManagesCellSize private $defaultRowHeight; /** @var array Array of min-max-width arrays */ - private $columnWidths; + private $columnWidths = []; /** * @param float|null $width From bb839040830f16e46f3582d0a7e6f46df413bf67 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 20 Dec 2019 22:47:16 +0100 Subject: [PATCH 11/18] Remove null phpdoc where typehint is float --- src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index e49fc7661..8c019f5bf 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -289,7 +289,7 @@ private function applyDefaultRowStyle(Row $row) } /** - * @param float|null $width + * @param float $width */ public function setDefaultColumnWidth(float $width) { @@ -297,7 +297,7 @@ public function setDefaultColumnWidth(float $width) } /** - * @param float|null $height + * @param float $height */ public function setDefaultRowHeight(float $height) { From ffec80422b5da0a26121e009ba32bfd67abaca10 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 20 Dec 2019 23:33:28 +0100 Subject: [PATCH 12/18] Attempt to satisfy php-cs-fixer --- .../Writer/Common/Manager/ManagesCellSize.php | 2 -- .../Manager/WorkbookManagerAbstract.php | 8 +++---- .../Writer/ODS/Manager/Style/StyleManager.php | 15 ++++++++----- .../Writer/WriterMultiSheetsAbstract.php | 13 ++++++----- .../Writer/XLSX/Manager/WorksheetManager.php | 22 +++++++++---------- tests/Spout/Writer/ODS/SheetTest.php | 2 +- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/ManagesCellSize.php b/src/Spout/Writer/Common/Manager/ManagesCellSize.php index 60de4cf72..00ac8c1ff 100644 --- a/src/Spout/Writer/Common/Manager/ManagesCellSize.php +++ b/src/Spout/Writer/Common/Manager/ManagesCellSize.php @@ -1,9 +1,7 @@ columnWidths, function($a, $b) { + usort($this->columnWidths, function ($a, $b) { if ($a[0] == $b[0]) { return 0; } @@ -329,9 +329,12 @@ private function getCellAlignmentSectionContent($style) private function transformCellAlignment($cellAlignment) { switch ($cellAlignment) { - case CellAlignment::LEFT: return 'start'; - case CellAlignment::RIGHT: return 'end'; - default: return $cellAlignment; + case CellAlignment::LEFT: + return 'start'; + case CellAlignment::RIGHT: + return 'end'; + default: + return $cellAlignment; } } @@ -398,7 +401,7 @@ private function getBackgroundColorXMLContent($style) return \sprintf(' fo:background-color="#%s" ', $style->getBackgroundColor()); } - public function getTableColumnStylesXMLContent(): string + public function getTableColumnStylesXMLContent() : string { if (empty($this->columnWidths)) { return ''; @@ -415,7 +418,7 @@ public function getTableColumnStylesXMLContent(): string return $content; } - public function getStyledTableColumnXMLContent(int $maxNumColumns): string + public function getStyledTableColumnXMLContent(int $maxNumColumns) : string { if (empty($this->columnWidths)) { return ''; diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 0bf3bf672..77d58d8a6 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -50,14 +50,15 @@ public function __construct( * This must be set before opening the writer. * * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached - * @return WriterMultiSheetsAbstract * @throws WriterAlreadyOpenedException If the writer was already opened + * @return WriterMultiSheetsAbstract */ public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically); + $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + $shouldCreateNewSheetsAutomatically); return $this; } @@ -97,9 +98,9 @@ public function getSheets() /** * Creates a new sheet and make it the current sheet. The data will now be written to this sheet. * - * @return Sheet The created sheet * @throws IOException * @throws WriterNotOpenedException If the writer has not been opened yet + * @return Sheet The created sheet */ public function addNewSheetAndMakeItCurrent() { @@ -112,8 +113,8 @@ public function addNewSheetAndMakeItCurrent() /** * Returns the current sheet * - * @return Sheet The current sheet * @throws WriterNotOpenedException If the writer has not been opened yet + * @return Sheet The current sheet */ public function getCurrentSheet() { @@ -127,9 +128,9 @@ public function getCurrentSheet() * The writing will resume where it stopped (i.e. data won't be truncated). * * @param Sheet $sheet The sheet to set as current - * @return void * @throws SheetNotFoundException If the given sheet does not exist in the workbook * @throws WriterNotOpenedException If the writer has not been opened yet + * @return void */ public function setCurrentSheet($sheet) { @@ -183,8 +184,8 @@ public function setColumnWidthForRange(float $width, int $start, int $end) /** * Checks if the workbook has been created. Throws an exception if not created yet. * - * @return void * @throws WriterNotOpenedException If the workbook is not created yet + * @return void */ protected function throwIfWorkbookIsNotAvailable() { diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index f85a051bf..1d19f4795 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -128,8 +128,8 @@ public function startSheet(Worksheet $worksheet) * Checks if the sheet has been sucessfully created. Throws an exception if not. * * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file - * @return void * @throws IOException If the sheet data file cannot be opened for writing + * @return void */ private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) { @@ -155,9 +155,9 @@ public function addRow(Worksheet $worksheet, Row $row) * * @param Worksheet $worksheet The worksheet to add the row to * @param Row $row The row to be written - * @return void * @throws InvalidArgumentException If a cell value's type is not supported * @throws IOException If the data cannot be written + * @return void */ private function addNonEmptyRow(Worksheet $worksheet, Row $row) { @@ -192,13 +192,13 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row) * Applies styles to the given style, merging the cell's style with its row's style * Then builds and returns xml for the cell. * - * @param Cell $cell + * @param Cell $cell * @param Style $rowStyle - * @param int $rowIndexOneBased - * @param int $columnIndexZeroBased + * @param int $rowIndexOneBased + * @param int $columnIndexZeroBased * - * @return string * @throws InvalidArgumentException If the given value cannot be processed + * @return string */ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexOneBased, $columnIndexZeroBased) { @@ -215,13 +215,13 @@ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexO /** * Builds and returns xml for a single cell. * - * @param int $rowIndexOneBased - * @param int $columnIndexZeroBased + * @param int $rowIndexOneBased + * @param int $columnIndexZeroBased * @param Cell $cell - * @param int $styleId + * @param int $styleId * - * @return string * @throws InvalidArgumentException If the given value cannot be processed + * @return string */ private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId) { @@ -257,8 +257,8 @@ private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell * Returns the XML fragment for a cell containing a non empty string * * @param string $cellValue The cell value - * @return string The XML fragment representing the cell * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell + * @return string The XML fragment representing the cell */ private function getCellXMLFragmentForNonEmptyString($cellValue) { diff --git a/tests/Spout/Writer/ODS/SheetTest.php b/tests/Spout/Writer/ODS/SheetTest.php index fac513a50..a8e0c8b3d 100644 --- a/tests/Spout/Writer/ODS/SheetTest.php +++ b/tests/Spout/Writer/ODS/SheetTest.php @@ -93,7 +93,7 @@ public function testSetSheetVisibilityShouldCreateSheetHidden() $this->assertContains(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"'); } - function testThrowsIfWorkbookIsNotInitialized() + public function testThrowsIfWorkbookIsNotInitialized() { $this->expectException(WriterNotOpenedException::class); $writer = WriterEntityFactory::createODSWriter(); From 36573eaa8a2e01a54e082b287fb1883191c7c8ce Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 20 Dec 2019 23:59:17 +0100 Subject: [PATCH 13/18] Satisfy php-cs-fixer --- src/Spout/Writer/Common/Manager/ManagesCellSize.php | 1 - .../Writer/Common/Manager/WorkbookManagerAbstract.php | 1 - src/Spout/Writer/ODS/Manager/Style/StyleManager.php | 5 ++++- src/Spout/Writer/ODS/Manager/WorksheetManager.php | 2 +- src/Spout/Writer/WriterMultiSheetsAbstract.php | 8 +++++--- src/Spout/Writer/XLSX/Manager/WorksheetManager.php | 4 +++- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/ManagesCellSize.php b/src/Spout/Writer/Common/Manager/ManagesCellSize.php index 00ac8c1ff..8a517f0d9 100644 --- a/src/Spout/Writer/Common/Manager/ManagesCellSize.php +++ b/src/Spout/Writer/Common/Manager/ManagesCellSize.php @@ -60,5 +60,4 @@ public function setColumnWidthForRange(float $width, int $start, int $end) { $this->columnWidths[] = [$start, $end, $width]; } - } diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 1f2331fe0..272484d16 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -15,7 +15,6 @@ use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Exception\SheetNotFoundException; -use Box\Spout\Writer\Exception\WriterException; /** * Class WorkbookManagerAbstract diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index eabff06e6..32500abc3 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -191,9 +191,10 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets) // Sort column widths since ODS cares about order usort($this->columnWidths, function ($a, $b) { - if ($a[0] == $b[0]) { + if ($a[0] === $b[0]) { return 0; } + return ($a[0] < $b[0]) ? -1 : 1; }); $content .= $this->getTableColumnStylesXMLContent(); @@ -415,6 +416,7 @@ public function getTableColumnStylesXMLContent() : string EOD; } + return $content; } @@ -434,6 +436,7 @@ public function getStyledTableColumnXMLContent(int $maxNumColumns) : string // Note: This assumes the column widths are contiguous and default width is // only applied to columns after the last custom column with a custom width $content .= ''; + return $content; } } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index a11474902..5a3ec50fc 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -102,7 +102,7 @@ public function getTableElementStartAsString(Worksheet $worksheet) $escapedSheetName = $this->stringsEscaper->escape($externalSheet->getName()); $tableStyleName = 'ta' . ($externalSheet->getIndex() + 1); - $tableElement = ''; + $tableElement = ''; $tableElement .= $this->styleManager->getStyledTableColumnXMLContent($worksheet->getMaxNumColumns()); return $tableElement; diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 77d58d8a6..b349b0489 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -57,8 +57,10 @@ public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAuto { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); - $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, - $shouldCreateNewSheetsAutomatically); + $this->optionsManager->setOption( + Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + $shouldCreateNewSheetsAutomatically + ); return $this; } @@ -77,8 +79,8 @@ protected function openWriter() /** * Returns all the workbook's sheets * - * @return Sheet[] All the workbook's sheets * @throws WriterNotOpenedException If the writer has not been opened yet + * @return Sheet[] All the workbook's sheets */ public function getSheets() { diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 1d19f4795..e430b3c24 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -232,7 +232,7 @@ private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell if ($cell->isString()) { $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); } elseif ($cell->isBoolean()) { - $cellXML .= ' t="b">' . (int)($cell->getValue()) . ''; + $cellXML .= ' t="b">' . (int) ($cell->getValue()) . ''; } elseif ($cell->isNumeric()) { $cellXML .= '>' . $cell->getValue() . ''; } elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) { @@ -291,6 +291,7 @@ public function getXMLFragmentForColumnWidths() $xml .= ''; } $xml .= ''; + return $xml; } @@ -308,6 +309,7 @@ public function getXMLFragmentForDefaultCellSizing() } // Ensure that the required defaultRowHeight is set $rowHeightXml = empty($rowHeightXml) ? ' defaultRowHeight="0"' : $rowHeightXml; + return ""; } From 26e5f966fb28360b7d2127541e2e72ba4251cd52 Mon Sep 17 00:00:00 2001 From: Martin Wind Date: Sun, 29 Mar 2020 15:10:11 +0200 Subject: [PATCH 14/18] Use options manager for default row height and column width --- src/Spout/Writer/WriterMultiSheetsAbstract.php | 18 ++++++++++++------ .../Writer/XLSX/Manager/WorksheetManager.php | 17 ++++------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index b349b0489..1fb5ee90f 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -142,22 +142,28 @@ public function setCurrentSheet($sheet) /** * @param float $width - * @throws WriterNotOpenedException + * @throws WriterAlreadyOpenedException */ public function setDefaultColumnWidth(float $width) { - $this->throwIfWorkbookIsNotAvailable(); - $this->workbookManager->setDefaultColumnWidth($width); + $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); + $this->optionsManager->setOption( + Options::DEFAULT_COLUMN_WIDTH, + $width + ); } /** * @param float $height - * @throws WriterNotOpenedException + * @throws WriterAlreadyOpenedException */ public function setDefaultRowHeight(float $height) { - $this->throwIfWorkbookIsNotAvailable(); - $this->workbookManager->setDefaultRowHeight($height); + $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); + $this->optionsManager->setOption( + Options::DEFAULT_ROW_HEIGHT, + $height + ); } /** diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index e430b3c24..fe057d383 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -65,9 +65,6 @@ class WorksheetManager implements WorksheetManagerInterface /** @var InternalEntityFactory Factory to create entities */ private $entityFactory; - /** @var bool Whether rows have been written */ - private $hasWrittenRows = false; - /** * WorksheetManager constructor. * @@ -122,6 +119,9 @@ public function startSheet(Worksheet $worksheet) $worksheet->setFilePointer($sheetFilePointer); \fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); + \fwrite($sheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); + \fwrite($sheetFilePointer, $this->getXMLFragmentForColumnWidths()); + \fwrite($sheetFilePointer, ''); } /** @@ -162,12 +162,6 @@ public function addRow(Worksheet $worksheet, Row $row) private function addNonEmptyRow(Worksheet $worksheet, Row $row) { $sheetFilePointer = $worksheet->getFilePointer(); - if (!$this->hasWrittenRows) { - fwrite($sheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); - fwrite($sheetFilePointer, $this->getXMLFragmentForColumnWidths()); - fwrite($sheetFilePointer, ''); - } - $rowStyle = $row->getStyle(); $rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1; $numCells = $row->getNumCells(); @@ -185,7 +179,6 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row) if ($wasWriteSuccessful === false) { throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); } - $this->hasWrittenRows = true; } /** @@ -324,9 +317,7 @@ public function close(Worksheet $worksheet) return; } - if ($this->hasWrittenRows) { - \fwrite($worksheetFilePointer, ''); - } + \fwrite($worksheetFilePointer, ''); \fwrite($worksheetFilePointer, ''); \fclose($worksheetFilePointer); } From de3241b57cbd707f46a237ad1e5c109eacb90211 Mon Sep 17 00:00:00 2001 From: Martin Wind Date: Sun, 29 Mar 2020 20:32:24 +0200 Subject: [PATCH 15/18] Delay sheetData and add sheetDataStarted flag to worksheet --- src/Spout/Writer/Common/Entity/Worksheet.php | 20 +++++++++++++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 22 +++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Spout/Writer/Common/Entity/Worksheet.php b/src/Spout/Writer/Common/Entity/Worksheet.php index 74c4976f0..c7a098706 100644 --- a/src/Spout/Writer/Common/Entity/Worksheet.php +++ b/src/Spout/Writer/Common/Entity/Worksheet.php @@ -23,6 +23,9 @@ class Worksheet /** @var int Index of the last written row */ private $lastWrittenRowIndex; + /** @var bool has the sheet data header been written */ + private $sheetDataStarted = false; + /** * Worksheet constructor. * @@ -36,6 +39,7 @@ public function __construct($worksheetFilePath, Sheet $externalSheet) $this->externalSheet = $externalSheet; $this->maxNumColumns = 0; $this->lastWrittenRowIndex = 0; + $this->sheetDataStarted = false; } /** @@ -110,4 +114,20 @@ public function getId() // sheet index is zero-based, while ID is 1-based return $this->externalSheet->getIndex() + 1; } + + /** + * @return bool + */ + public function getSheetDataStarted() + { + return $this->sheetDataStarted; + } + + /** + * @param bool $sheetDataStarted + */ + public function setSheetDataStarted($sheetDataStarted) + { + $this->sheetDataStarted = $sheetDataStarted; + } } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index fe057d383..efdaf049a 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -119,9 +119,22 @@ public function startSheet(Worksheet $worksheet) $worksheet->setFilePointer($sheetFilePointer); \fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); - \fwrite($sheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); - \fwrite($sheetFilePointer, $this->getXMLFragmentForColumnWidths()); - \fwrite($sheetFilePointer, ''); + } + + /** + * Writes the sheet data header + * + * @param Worksheet $worksheet The worksheet to add the row to + * @return void + */ + private function ensureSheetDataStated(Worksheet $worksheet) { + if (!$worksheet->getSheetDataStarted()) { + $worksheetFilePointer = $worksheet->getFilePointer(); + \fwrite($worksheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); + \fwrite($worksheetFilePointer, $this->getXMLFragmentForColumnWidths()); + \fwrite($worksheetFilePointer, ''); + $worksheet->setSheetDataStarted(true); + } } /** @@ -161,6 +174,7 @@ public function addRow(Worksheet $worksheet, Row $row) */ private function addNonEmptyRow(Worksheet $worksheet, Row $row) { + $this->ensureSheetDataStated($worksheet); $sheetFilePointer = $worksheet->getFilePointer(); $rowStyle = $row->getStyle(); $rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1; @@ -316,7 +330,7 @@ public function close(Worksheet $worksheet) if (!\is_resource($worksheetFilePointer)) { return; } - + $this->ensureSheetDataStated($worksheet); \fwrite($worksheetFilePointer, ''); \fwrite($worksheetFilePointer, ''); \fclose($worksheetFilePointer); From 1eee2ea5db6b364087e41fb476309bf5fc535ab2 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Tue, 26 May 2020 23:12:57 +0200 Subject: [PATCH 16/18] Satisfy php-cs-fixer --- src/Spout/Writer/WriterMultiSheetsAbstract.php | 6 +++--- .../Writer/XLSX/Manager/WorksheetManager.php | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 1fb5ee90f..0161877e0 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -161,9 +161,9 @@ public function setDefaultRowHeight(float $height) { $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); $this->optionsManager->setOption( - Options::DEFAULT_ROW_HEIGHT, - $height - ); + Options::DEFAULT_ROW_HEIGHT, + $height + ); } /** diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index efdaf049a..7c33e060f 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -123,17 +123,18 @@ public function startSheet(Worksheet $worksheet) /** * Writes the sheet data header - * + * * @param Worksheet $worksheet The worksheet to add the row to * @return void */ - private function ensureSheetDataStated(Worksheet $worksheet) { + private function ensureSheetDataStated(Worksheet $worksheet) + { if (!$worksheet->getSheetDataStarted()) { - $worksheetFilePointer = $worksheet->getFilePointer(); - \fwrite($worksheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); - \fwrite($worksheetFilePointer, $this->getXMLFragmentForColumnWidths()); - \fwrite($worksheetFilePointer, ''); - $worksheet->setSheetDataStarted(true); + $worksheetFilePointer = $worksheet->getFilePointer(); + \fwrite($worksheetFilePointer, $this->getXMLFragmentForDefaultCellSizing()); + \fwrite($worksheetFilePointer, $this->getXMLFragmentForColumnWidths()); + \fwrite($worksheetFilePointer, ''); + $worksheet->setSheetDataStarted(true); } } From 9727bec850921a00e54958ef23ad41e47d675fa8 Mon Sep 17 00:00:00 2001 From: Martin Wind Date: Sat, 13 Jun 2020 20:43:55 +0200 Subject: [PATCH 17/18] Fix ODS column widths --- src/Spout/Writer/ODS/Creator/ManagerFactory.php | 2 +- src/Spout/Writer/ODS/Manager/OptionsManager.php | 3 +++ .../Writer/ODS/Manager/Style/StyleManager.php | 13 +++++++++++++ src/Spout/Writer/ODS/Manager/WorksheetManager.php | 10 +--------- tests/Spout/Writer/ODS/SheetTest.php | 14 ++++++-------- tests/Spout/Writer/XLSX/SheetTest.php | 12 +++--------- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Spout/Writer/ODS/Creator/ManagerFactory.php b/src/Spout/Writer/ODS/Creator/ManagerFactory.php index f38c5000b..cf7877f11 100644 --- a/src/Spout/Writer/ODS/Creator/ManagerFactory.php +++ b/src/Spout/Writer/ODS/Creator/ManagerFactory.php @@ -93,7 +93,7 @@ private function createStyleManager(OptionsManagerInterface $optionsManager) { $styleRegistry = $this->createStyleRegistry($optionsManager); - return new StyleManager($styleRegistry); + return new StyleManager($styleRegistry, $optionsManager); } /** diff --git a/src/Spout/Writer/ODS/Manager/OptionsManager.php b/src/Spout/Writer/ODS/Manager/OptionsManager.php index a6fb564ef..698108a9c 100644 --- a/src/Spout/Writer/ODS/Manager/OptionsManager.php +++ b/src/Spout/Writer/ODS/Manager/OptionsManager.php @@ -34,6 +34,9 @@ protected function getSupportedOptions() Options::TEMP_FOLDER, Options::DEFAULT_ROW_STYLE, Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, + Options::DEFAULT_COLUMN_WIDTH, + Options::DEFAULT_ROW_HEIGHT, + Options::COLUMN_WIDTHS, ]; } diff --git a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php index 32500abc3..93ed008ec 100644 --- a/src/Spout/Writer/ODS/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/ODS/Manager/Style/StyleManager.php @@ -4,6 +4,8 @@ use Box\Spout\Common\Entity\Style\BorderPart; use Box\Spout\Common\Entity\Style\CellAlignment; +use Box\Spout\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\ManagesCellSize; use Box\Spout\Writer\ODS\Helper\BorderHelper; @@ -19,6 +21,17 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager /** @var StyleRegistry */ protected $styleRegistry; + /** + * @param StyleRegistry $styleRegistry + */ + public function __construct(StyleRegistry $styleRegistry, OptionsManagerInterface $optionsManager) + { + parent::__construct($styleRegistry); + $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); + $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); + $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; + } + /** * Returns the content of the "styles.xml" file, given a list of styles. * diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 5a3ec50fc..05e1ffd65 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -40,25 +40,17 @@ class WorksheetManager implements WorksheetManagerInterface * @param StyleMerger $styleMerger * @param ODSEscaper $stringsEscaper * @param StringHelper $stringHelper - * @param OptionsManager|null $optionsManager */ public function __construct( StyleManager $styleManager, StyleMerger $styleMerger, ODSEscaper $stringsEscaper, - StringHelper $stringHelper, - $optionsManager = null + StringHelper $stringHelper ) { $this->styleManager = $styleManager; $this->styleMerger = $styleMerger; $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; - - if ($optionsManager) { - $this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH)); - $this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT)); - $this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? []; - } } /** diff --git a/tests/Spout/Writer/ODS/SheetTest.php b/tests/Spout/Writer/ODS/SheetTest.php index a8e0c8b3d..b5fec7bd0 100644 --- a/tests/Spout/Writer/ODS/SheetTest.php +++ b/tests/Spout/Writer/ODS/SheetTest.php @@ -101,20 +101,18 @@ public function testThrowsIfWorkbookIsNotInitialized() $writer->addRow($this->createRowFromValues([])); } - public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() - { - $this->expectException(WriterNotOpenedException::class); - $writer = WriterEntityFactory::createXLSXWriter(); - $writer->setDefaultColumnWidth(10.0); - } - public function testWritesDefaultCellSizesIfSet() { $fileName = 'test_writes_default_cell_sizes_if_set.ods'; - $writer = $this->writerForFile($fileName); + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createODSWriter(); $writer->setDefaultColumnWidth(100.0); $writer->setDefaultRowHeight(20.0); + $writer->openToFile($resourcePath); + $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); $writer->close(); diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index 91c28f952..5a914e84d 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -101,13 +101,6 @@ public function testThrowsIfWorkbookIsNotInitialized() $writer->addRow($this->createRowFromValues([])); } - public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded() - { - $this->expectException(WriterNotOpenedException::class); - $writer = WriterEntityFactory::createXLSXWriter(); - $writer->setDefaultColumnWidth(10.0); - } - public function testWritesDefaultCellSizesIfSet() { $fileName = 'test_writes_default_cell_sizes_if_set.xlsx'; @@ -115,9 +108,9 @@ public function testWritesDefaultCellSizesIfSet() $resourcePath = $this->getGeneratedResourcePath($fileName); $writer = WriterEntityFactory::createXLSXWriter(); - $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); $writer->setDefaultRowHeight(20.0); + $writer->openToFile($resourcePath); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close(); @@ -137,8 +130,9 @@ public function testWritesDefaultRequiredRowHeightIfOmitted() $resourcePath = $this->getGeneratedResourcePath($fileName); $writer = WriterEntityFactory::createXLSXWriter(); - $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); + $writer->openToFile($resourcePath); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close(); From 73f2a522c070a53516d9ced5e680acbfef0897bd Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 30 Oct 2020 07:53:17 +0100 Subject: [PATCH 18/18] Satisfy php-cs-fixer --- src/Spout/Reader/ODS/SheetIterator.php | 4 ++-- src/Spout/Reader/XLSX/Manager/SharedStringsManager.php | 2 +- src/Spout/Reader/XLSX/RowIterator.php | 2 +- src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php | 2 +- src/Spout/Writer/ODS/Creator/ManagerFactory.php | 2 +- src/Spout/Writer/ODS/Manager/WorksheetManager.php | 1 - src/Spout/Writer/WriterAbstract.php | 2 +- src/Spout/Writer/XLSX/Creator/ManagerFactory.php | 2 +- tests/Spout/Reader/CSV/SpoutTestStream.php | 4 ++-- tests/Spout/Writer/XLSX/SheetTest.php | 2 +- 10 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Spout/Reader/ODS/SheetIterator.php b/src/Spout/Reader/ODS/SheetIterator.php index f35b85234..c7b8cd9dc 100644 --- a/src/Spout/Reader/ODS/SheetIterator.php +++ b/src/Spout/Reader/ODS/SheetIterator.php @@ -28,13 +28,13 @@ class SheetIterator implements IteratorInterface const XML_ATTRIBUTE_TABLE_STYLE_NAME = 'table:style-name'; const XML_ATTRIBUTE_TABLE_DISPLAY = 'table:display'; - /** @var string $filePath Path of the file to be read */ + /** @var string Path of the file to be read */ protected $filePath; /** @var \Box\Spout\Common\Manager\OptionsManagerInterface Reader's options manager */ protected $optionsManager; - /** @var InternalEntityFactory $entityFactory Factory to create entities */ + /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; /** @var XMLReader The XMLReader object that will help read sheet's XML data */ diff --git a/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php b/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php index caaeed767..8850a69bb 100644 --- a/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php +++ b/src/Spout/Reader/XLSX/Manager/SharedStringsManager.php @@ -43,7 +43,7 @@ class SharedStringsManager /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; - /** @var HelperFactory $helperFactory Factory to create helpers */ + /** @var HelperFactory Factory to create helpers */ protected $helperFactory; /** @var CachingStrategyFactory Factory to create shared strings caching strategies */ diff --git a/src/Spout/Reader/XLSX/RowIterator.php b/src/Spout/Reader/XLSX/RowIterator.php index 4af4530d9..a54b8b192 100644 --- a/src/Spout/Reader/XLSX/RowIterator.php +++ b/src/Spout/Reader/XLSX/RowIterator.php @@ -35,7 +35,7 @@ class RowIterator implements IteratorInterface /** @var string Path of the XLSX file being read */ protected $filePath; - /** @var string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml */ + /** @var string Path of the sheet data XML file as in [Content_Types].xml */ protected $sheetDataXMLFilePath; /** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */ diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 272484d16..604de65ce 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -43,7 +43,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface /** @var InternalEntityFactory Factory to create entities */ protected $entityFactory; - /** @var ManagerFactoryInterface $managerFactory Factory to create managers */ + /** @var ManagerFactoryInterface Factory to create managers */ protected $managerFactory; /** @var Worksheet The worksheet where data will be written to */ diff --git a/src/Spout/Writer/ODS/Creator/ManagerFactory.php b/src/Spout/Writer/ODS/Creator/ManagerFactory.php index cf7877f11..3605a37d7 100644 --- a/src/Spout/Writer/ODS/Creator/ManagerFactory.php +++ b/src/Spout/Writer/ODS/Creator/ManagerFactory.php @@ -22,7 +22,7 @@ class ManagerFactory implements ManagerFactoryInterface /** @var InternalEntityFactory */ protected $entityFactory; - /** @var HelperFactory $helperFactory */ + /** @var HelperFactory */ protected $helperFactory; /** diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 05e1ffd65..38cd6c5ed 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -9,7 +9,6 @@ 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\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; diff --git a/src/Spout/Writer/WriterAbstract.php b/src/Spout/Writer/WriterAbstract.php index d96a6280f..bbaa735a3 100644 --- a/src/Spout/Writer/WriterAbstract.php +++ b/src/Spout/Writer/WriterAbstract.php @@ -33,7 +33,7 @@ abstract class WriterAbstract implements WriterInterface /** @var GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; - /** @var HelperFactory $helperFactory */ + /** @var HelperFactory */ protected $helperFactory; /** @var OptionsManagerInterface Writer options manager */ diff --git a/src/Spout/Writer/XLSX/Creator/ManagerFactory.php b/src/Spout/Writer/XLSX/Creator/ManagerFactory.php index f27a2f2f5..aa3bcd5ce 100644 --- a/src/Spout/Writer/XLSX/Creator/ManagerFactory.php +++ b/src/Spout/Writer/XLSX/Creator/ManagerFactory.php @@ -24,7 +24,7 @@ class ManagerFactory implements ManagerFactoryInterface /** @var InternalEntityFactory */ protected $entityFactory; - /** @var HelperFactory $helperFactory */ + /** @var HelperFactory */ protected $helperFactory; /** diff --git a/tests/Spout/Reader/CSV/SpoutTestStream.php b/tests/Spout/Reader/CSV/SpoutTestStream.php index 3bfd06e1c..d66e8117c 100644 --- a/tests/Spout/Reader/CSV/SpoutTestStream.php +++ b/tests/Spout/Reader/CSV/SpoutTestStream.php @@ -14,10 +14,10 @@ class SpoutTestStream const PATH_TO_CSV_RESOURCES = 'tests/resources/csv/'; const CSV_EXTENSION = '.csv'; - /** @var int $position */ + /** @var int */ private $position; - /** @var resource $fileHandle */ + /** @var resource */ private $fileHandle; /** diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index 5a914e84d..f0caf1ba8 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -132,7 +132,7 @@ public function testWritesDefaultRequiredRowHeightIfOmitted() $writer = WriterEntityFactory::createXLSXWriter(); $writer->setDefaultColumnWidth(10.0); $writer->openToFile($resourcePath); - + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close();