From 8a17d6c71f04b4d045b564ddb89af82920e57af4 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Thu, 25 Mar 2021 08:44:08 +0100 Subject: [PATCH] Remove rowStyle reference and replace it by new RegisteredStyle class --- .../Writer/Common/Manager/RegisteredStyle.php | 34 +++++++++++++++++++ .../Writer/ODS/Manager/WorksheetManager.php | 30 ++++++++++------ .../Writer/XLSX/Manager/WorksheetManager.php | 20 ++++++----- 3 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/Spout/Writer/Common/Manager/RegisteredStyle.php diff --git a/src/Spout/Writer/Common/Manager/RegisteredStyle.php b/src/Spout/Writer/Common/Manager/RegisteredStyle.php new file mode 100644 index 0000000..5cf0e90 --- /dev/null +++ b/src/Spout/Writer/Common/Manager/RegisteredStyle.php @@ -0,0 +1,34 @@ +style = $style; + $this->isRowStyle = $isRowStyle; + } + + public function getStyle() : Style + { + return $this->style; + } + + public function isRowStyle() : bool + { + return $this->isRowStyle; + } +} diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 1997e9e..52f81a0 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -10,6 +10,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\Worksheet; +use Box\Spout\Writer\Common\Manager\RegisteredStyle; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; use Box\Spout\Writer\ODS\Manager\Style\StyleManager; @@ -93,7 +94,7 @@ class WorksheetManager implements WorksheetManagerInterface $escapedSheetName = $this->stringsEscaper->escape($externalSheet->getName()); $tableStyleName = 'ta' . ($externalSheet->getIndex() + 1); - $tableElement = ''; + $tableElement = ''; $tableElement .= ''; return $tableElement; @@ -104,8 +105,8 @@ class WorksheetManager implements WorksheetManagerInterface * * @param Worksheet $worksheet The worksheet to add the row to * @param Row $row The row to be added - * @throws IOException If the data cannot be written * @throws InvalidArgumentException If a cell value's type is not supported + * @throws IOException If the data cannot be written * @return void */ public function addRow(Worksheet $worksheet, Row $row) @@ -125,7 +126,13 @@ class WorksheetManager implements WorksheetManagerInterface $nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null; if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) { - $data .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $currentCellIndex, $nextCellIndex); + $registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle); + $cellStyle = $registeredStyle->getStyle(); + if ($registeredStyle->isRowStyle()) { + $rowStyle = $cellStyle; + } + + $data .= $this->getCellXMLWithStyle($cell, $cellStyle, $currentCellIndex, $nextCellIndex); $currentCellIndex = $nextCellIndex; } @@ -146,17 +153,15 @@ class WorksheetManager implements WorksheetManagerInterface /** * 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 Style $rowStyle - * @param int $currentCellIndex - * @param int $nextCellIndex * @throws InvalidArgumentException If a cell value's type is not supported - * @return string + * @return RegisteredStyle */ - private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentCellIndex, $nextCellIndex) + private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle { + $isRowStyle = false; if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); @@ -166,7 +171,7 @@ class WorksheetManager implements WorksheetManagerInterface $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); } else { $registeredStyle = $this->styleManager->registerStyle($rowStyle); - $rowStyle = $registeredStyle; + $isRowStyle = true; } } else { $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); @@ -182,7 +187,12 @@ class WorksheetManager implements WorksheetManagerInterface $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } - $styleIndex = $registeredStyle->getId() + 1; // 1-based + return new RegisteredStyle($registeredStyle, $isRowStyle); + } + + private function getCellXMLWithStyle(Cell $cell, Style $style, int $currentCellIndex, int $nextCellIndex) : string + { + $styleIndex = $style->getId() + 1; // 1-based $numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 3a210d6..2108dc1 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\Creator\InternalEntityFactory; 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\RegisteredStyle; use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; @@ -160,7 +161,12 @@ EOD; $rowXML = ''; foreach ($row->getCells() as $columnIndexZeroBased => $cell) { - $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndexOneBased, $columnIndexZeroBased); + $registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle); + $cellStyle = $registeredStyle->getStyle(); + if ($registeredStyle->isRowStyle()) { + $rowStyle = $cellStyle; + } + $rowXML .= $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $cellStyle->getId()); } $rowXML .= ''; @@ -173,18 +179,16 @@ EOD; /** * 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 Style $rowStyle - * @param int $rowIndexOneBased - * @param int $columnIndexZeroBased * * @throws InvalidArgumentException If the given value cannot be processed - * @return string + * @return RegisteredStyle */ - private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $rowIndexOneBased, $columnIndexZeroBased) + private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle { + $isRowStyle = false; if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); @@ -194,7 +198,7 @@ EOD; $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); } else { $registeredStyle = $this->styleManager->registerStyle($rowStyle); - $rowStyle = $registeredStyle; + $isRowStyle = true; } } else { $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); @@ -211,7 +215,7 @@ EOD; $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } - return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId()); + return new RegisteredStyle($registeredStyle, $isRowStyle); } /**