From 1dccfb120386a5444c228814c763e71a272ba2da Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 30 Mar 2021 14:04:40 +0200 Subject: [PATCH] Rename ManagedStyle to PossiblyUpdatedStyle and add documentation --- .../Writer/Common/Manager/RegisteredStyle.php | 14 +++++++----- ...agedStyle.php => PossiblyUpdatedStyle.php} | 7 +++++- .../Common/Manager/Style/StyleManager.php | 12 +++++----- .../Manager/Style/StyleManagerInterface.php | 4 ++-- .../Writer/ODS/Manager/WorksheetManager.php | 22 +++++++++---------- .../Writer/XLSX/Manager/WorksheetManager.php | 22 +++++++++---------- .../Common/Manager/Style/StyleManagerTest.php | 14 ++++++------ 7 files changed, 52 insertions(+), 43 deletions(-) rename src/Spout/Writer/Common/Manager/Style/{ManagedStyle.php => PossiblyUpdatedStyle.php} (74%) diff --git a/src/Spout/Writer/Common/Manager/RegisteredStyle.php b/src/Spout/Writer/Common/Manager/RegisteredStyle.php index 5cf0e90..734c2b6 100644 --- a/src/Spout/Writer/Common/Manager/RegisteredStyle.php +++ b/src/Spout/Writer/Common/Manager/RegisteredStyle.php @@ -4,6 +4,10 @@ namespace Box\Spout\Writer\Common\Manager; use Box\Spout\Common\Entity\Style\Style; +/** + * Class RegisteredStyle + * Allow to know if this style must replace actual row style. + */ class RegisteredStyle { /** @@ -14,12 +18,12 @@ class RegisteredStyle /** * @var bool */ - private $isRowStyle; + private $isMatchingRowStyle; - public function __construct(Style $style, bool $isRowStyle) + public function __construct(Style $style, bool $isMatchingRowStyle) { $this->style = $style; - $this->isRowStyle = $isRowStyle; + $this->isMatchingRowStyle = $isMatchingRowStyle; } public function getStyle() : Style @@ -27,8 +31,8 @@ class RegisteredStyle return $this->style; } - public function isRowStyle() : bool + public function isMatchingRowStyle() : bool { - return $this->isRowStyle; + return $this->isMatchingRowStyle; } } diff --git a/src/Spout/Writer/Common/Manager/Style/ManagedStyle.php b/src/Spout/Writer/Common/Manager/Style/PossiblyUpdatedStyle.php similarity index 74% rename from src/Spout/Writer/Common/Manager/Style/ManagedStyle.php rename to src/Spout/Writer/Common/Manager/Style/PossiblyUpdatedStyle.php index 0701ac3..6ccaa29 100644 --- a/src/Spout/Writer/Common/Manager/Style/ManagedStyle.php +++ b/src/Spout/Writer/Common/Manager/Style/PossiblyUpdatedStyle.php @@ -4,7 +4,12 @@ namespace Box\Spout\Writer\Common\Manager\Style; use Box\Spout\Common\Entity\Style\Style; -class ManagedStyle +/** + * Class PossiblyUpdatedStyle + * Indicates if style is updated. + * It allow to know if style registration must be done. + */ +class PossiblyUpdatedStyle { private $style; private $isUpdated; diff --git a/src/Spout/Writer/Common/Manager/Style/StyleManager.php b/src/Spout/Writer/Common/Manager/Style/StyleManager.php index 2f21763..e2b5ebd 100644 --- a/src/Spout/Writer/Common/Manager/Style/StyleManager.php +++ b/src/Spout/Writer/Common/Manager/Style/StyleManager.php @@ -50,9 +50,9 @@ class StyleManager implements StyleManagerInterface * Typically, set "wrap text" if a cell contains a new line. * * @param Cell $cell - * @return ManagedStyle The eventually updated style + * @return PossiblyUpdatedStyle The eventually updated style */ - public function applyExtraStylesIfNeeded(Cell $cell) : ManagedStyle + public function applyExtraStylesIfNeeded(Cell $cell) : PossiblyUpdatedStyle { return $this->applyWrapTextIfCellContainsNewLine($cell); } @@ -67,9 +67,9 @@ class StyleManager implements StyleManagerInterface * on the Windows version of Excel... * * @param Cell $cell The cell the style should be applied to - * @return ManagedStyle The eventually updated style + * @return PossiblyUpdatedStyle The eventually updated style */ - protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : ManagedStyle + protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : PossiblyUpdatedStyle { $cellStyle = $cell->getStyle(); @@ -77,9 +77,9 @@ class StyleManager implements StyleManagerInterface if (!$cellStyle->hasSetWrapText() && $cell->isString() && \strpos($cell->getValue(), "\n") !== false) { $cellStyle->setShouldWrapText(); - return new ManagedStyle($cellStyle, true); + return new PossiblyUpdatedStyle($cellStyle, true); } - return new ManagedStyle($cellStyle, false); + return new PossiblyUpdatedStyle($cellStyle, false); } } diff --git a/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php b/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php index 6d01bd5..6b320b1 100644 --- a/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php @@ -24,7 +24,7 @@ interface StyleManagerInterface * Typically, set "wrap text" if a cell contains a new line. * * @param Cell $cell - * @return ManagedStyle The eventually updated style + * @return PossiblyUpdatedStyle The eventually updated style */ - public function applyExtraStylesIfNeeded(Cell $cell) : ManagedStyle; + public function applyExtraStylesIfNeeded(Cell $cell) : PossiblyUpdatedStyle; } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 52f81a0..e0366d1 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -128,8 +128,8 @@ class WorksheetManager implements WorksheetManagerInterface if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) { $registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle); $cellStyle = $registeredStyle->getStyle(); - if ($registeredStyle->isRowStyle()) { - $rowStyle = $cellStyle; + if ($registeredStyle->isMatchingRowStyle()) { + $rowStyle = $cellStyle; // Replace actual rowStyle (possibly with null id) by registered style (with id) } $data .= $this->getCellXMLWithStyle($cell, $cellStyle, $currentCellIndex, $nextCellIndex); @@ -161,25 +161,25 @@ class WorksheetManager implements WorksheetManagerInterface */ private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle { - $isRowStyle = false; + $isMatchingRowStyle = false; if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); - $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + $possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($managedStyle->isUpdated()) { - $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); + if ($possiblyUpdatedStyle->isUpdated()) { + $registeredStyle = $this->styleManager->registerStyle($possiblyUpdatedStyle->getStyle()); } else { $registeredStyle = $this->styleManager->registerStyle($rowStyle); - $isRowStyle = true; + $isMatchingRowStyle = true; } } else { $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); $cell->setStyle($mergedCellAndRowStyle); - $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($managedStyle->isUpdated()) { - $newCellStyle = $managedStyle->getStyle(); + $possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + if ($possiblyUpdatedStyle->isUpdated()) { + $newCellStyle = $possiblyUpdatedStyle->getStyle(); } else { $newCellStyle = $mergedCellAndRowStyle; } @@ -187,7 +187,7 @@ class WorksheetManager implements WorksheetManagerInterface $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } - return new RegisteredStyle($registeredStyle, $isRowStyle); + return new RegisteredStyle($registeredStyle, $isMatchingRowStyle); } private function getCellXMLWithStyle(Cell $cell, Style $style, int $currentCellIndex, int $nextCellIndex) : string diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 2108dc1..028bdef 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -163,8 +163,8 @@ EOD; foreach ($row->getCells() as $columnIndexZeroBased => $cell) { $registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle); $cellStyle = $registeredStyle->getStyle(); - if ($registeredStyle->isRowStyle()) { - $rowStyle = $cellStyle; + if ($registeredStyle->isMatchingRowStyle()) { + $rowStyle = $cellStyle; // Replace actual rowStyle (possibly with null id) by registered style (with id) } $rowXML .= $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $cellStyle->getId()); } @@ -188,26 +188,26 @@ EOD; */ private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle { - $isRowStyle = false; + $isMatchingRowStyle = false; if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); - $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + $possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($managedStyle->isUpdated()) { - $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); + if ($possiblyUpdatedStyle->isUpdated()) { + $registeredStyle = $this->styleManager->registerStyle($possiblyUpdatedStyle->getStyle()); } else { $registeredStyle = $this->styleManager->registerStyle($rowStyle); - $isRowStyle = true; + $isMatchingRowStyle = true; } } else { $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); $cell->setStyle($mergedCellAndRowStyle); - $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + $possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($managedStyle->isUpdated()) { - $newCellStyle = $managedStyle->getStyle(); + if ($possiblyUpdatedStyle->isUpdated()) { + $newCellStyle = $possiblyUpdatedStyle->getStyle(); } else { $newCellStyle = $mergedCellAndRowStyle; } @@ -215,7 +215,7 @@ EOD; $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } - return new RegisteredStyle($registeredStyle, $isRowStyle); + return new RegisteredStyle($registeredStyle, $isMatchingRowStyle); } /** diff --git a/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php b/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php index daf4abb..5bbbe5c 100644 --- a/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php +++ b/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php @@ -28,10 +28,10 @@ class StyleManagerTest extends TestCase $this->assertFalse($style->shouldWrapText()); $styleManager = $this->getStyleManager(); - $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); + $possiblyUpdatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); - $this->assertTrue($managedStyle->isUpdated()); - $this->assertTrue($managedStyle->getStyle()->shouldWrapText()); + $this->assertTrue($possiblyUpdatedStyle->isUpdated()); + $this->assertTrue($possiblyUpdatedStyle->getStyle()->shouldWrapText()); } public function testApplyExtraStylesIfNeededShouldReturnNullIfWrapTextNotNeeded() : void @@ -40,9 +40,9 @@ class StyleManagerTest extends TestCase $this->assertFalse($style->shouldWrapText()); $styleManager = $this->getStyleManager(); - $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell('oneline', $style)); + $possiblyUpdatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell('oneline', $style)); - $this->assertFalse($managedStyle->isUpdated()); + $this->assertFalse($possiblyUpdatedStyle->isUpdated()); } public function testApplyExtraStylesIfNeededShouldReturnNullIfWrapTextAlreadyApplied() : void @@ -51,8 +51,8 @@ class StyleManagerTest extends TestCase $this->assertTrue($style->shouldWrapText()); $styleManager = $this->getStyleManager(); - $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); + $possiblyUpdatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); - $this->assertFalse($managedStyle->isUpdated()); + $this->assertFalse($possiblyUpdatedStyle->isUpdated()); } }