diff --git a/src/Spout/Common/Entity/Cell.php b/src/Spout/Common/Entity/Cell.php index 6ffda46..c1de389 100644 --- a/src/Spout/Common/Entity/Cell.php +++ b/src/Spout/Common/Entity/Cell.php @@ -2,7 +2,6 @@ namespace Box\Spout\Common\Entity; -use Box\Spout\Common\Entity\Style\EmptyStyle; use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Helper\CellTypeHelper; @@ -105,7 +104,7 @@ class Cell */ public function setStyle($style) { - $this->style = $style ?: new EmptyStyle(); + $this->style = $style ?: new Style(); } /** diff --git a/src/Spout/Common/Entity/Style/EmptyStyle.php b/src/Spout/Common/Entity/Style/EmptyStyle.php deleted file mode 100644 index 56fa034..0000000 --- a/src/Spout/Common/Entity/Style/EmptyStyle.php +++ /dev/null @@ -1,7 +0,0 @@ -shouldApplyBorder = true; $this->border = $border; + $this->isEmpty = false; return $this; } @@ -150,6 +154,7 @@ class Style $this->fontBold = true; $this->hasSetFontBold = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -178,6 +183,7 @@ class Style $this->fontItalic = true; $this->hasSetFontItalic = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -206,6 +212,7 @@ class Style $this->fontUnderline = true; $this->hasSetFontUnderline = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -234,6 +241,7 @@ class Style $this->fontStrikethrough = true; $this->hasSetFontStrikethrough = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -263,6 +271,7 @@ class Style $this->fontSize = $fontSize; $this->hasSetFontSize = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -294,6 +303,7 @@ class Style $this->fontColor = $fontColor; $this->hasSetFontColor = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -323,6 +333,7 @@ class Style $this->fontName = $fontName; $this->hasSetFontName = true; $this->shouldApplyFont = true; + $this->isEmpty = false; return $this; } @@ -353,6 +364,7 @@ class Style $this->cellAlignment = $cellAlignment; $this->hasSetCellAlignment = true; $this->shouldApplyCellAlignment = true; + $this->isEmpty = false; return $this; } @@ -389,6 +401,7 @@ class Style { $this->shouldWrapText = $shouldWrap; $this->hasSetWrapText = true; + $this->isEmpty = false; return $this; } @@ -418,6 +431,7 @@ class Style { $this->hasSetBackgroundColor = true; $this->backgroundColor = $color; + $this->isEmpty = false; return $this; } @@ -447,6 +461,7 @@ class Style { $this->hasSetFormat = true; $this->format = $format; + $this->isEmpty = false; return $this; } @@ -467,11 +482,6 @@ class Style return $this->hasSetFormat; } - public function setRegistered(bool $isRegistered = true) : void - { - $this->isRegistered = $isRegistered; - } - /** * @return bool */ @@ -485,4 +495,15 @@ class Style $this->setId($id); $this->isRegistered = true; } + + public function unregister() : void + { + $this->setId(0); + $this->isRegistered = false; + } + + public function isEmpty() : bool + { + return $this->isEmpty; + } } diff --git a/src/Spout/Writer/Common/Manager/Style/ManagedStyle.php b/src/Spout/Writer/Common/Manager/Style/ManagedStyle.php new file mode 100644 index 0000000..0701ac3 --- /dev/null +++ b/src/Spout/Writer/Common/Manager/Style/ManagedStyle.php @@ -0,0 +1,27 @@ +style = $style; + $this->isUpdated = $isUpdated; + } + + public function getStyle() : Style + { + return $this->style; + } + + public function isUpdated() : bool + { + return $this->isUpdated; + } +} diff --git a/src/Spout/Writer/Common/Manager/Style/StyleManager.php b/src/Spout/Writer/Common/Manager/Style/StyleManager.php index bf87958..2f21763 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 Style|null The eventually updated style + * @return ManagedStyle The eventually updated style */ - public function applyExtraStylesIfNeeded(Cell $cell) : ?Style + public function applyExtraStylesIfNeeded(Cell $cell) : ManagedStyle { return $this->applyWrapTextIfCellContainsNewLine($cell); } @@ -67,23 +67,19 @@ class StyleManager implements StyleManagerInterface * on the Windows version of Excel... * * @param Cell $cell The cell the style should be applied to - * @return Style|null The eventually updated style + * @return ManagedStyle The eventually updated style */ - protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : ?Style + protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : ManagedStyle { $cellStyle = $cell->getStyle(); // if the "wrap text" option is already set, no-op - if ($cellStyle->hasSetWrapText()) { - return null; - } - - if ($cell->isString() && \strpos($cell->getValue(), "\n") !== false) { + if (!$cellStyle->hasSetWrapText() && $cell->isString() && \strpos($cell->getValue(), "\n") !== false) { $cellStyle->setShouldWrapText(); - return $cellStyle; + return new ManagedStyle($cellStyle, true); } - return null; + return new ManagedStyle($cellStyle, false); } } diff --git a/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php b/src/Spout/Writer/Common/Manager/Style/StyleManagerInterface.php index 9929eeb..be77110 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 Style|null The eventually updated style + * @return ManagedStyle|null The eventually updated style */ - public function applyExtraStylesIfNeeded(Cell $cell) : ?Style; + public function applyExtraStylesIfNeeded(Cell $cell) : ManagedStyle; } diff --git a/src/Spout/Writer/Common/Manager/Style/StyleRegistry.php b/src/Spout/Writer/Common/Manager/Style/StyleRegistry.php index ea7b7a4..dcfa267 100644 --- a/src/Spout/Writer/Common/Manager/Style/StyleRegistry.php +++ b/src/Spout/Writer/Common/Manager/Style/StyleRegistry.php @@ -114,8 +114,7 @@ class StyleRegistry { // In order to be able to properly compare style, set static ID value and reset registration $currentId = $style->getId(); - $style->setId(0); - $style->setRegistered(false); + $style->unregister(); $serializedStyle = \serialize($style); diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index b7c4e6e..894c82f 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -4,7 +4,6 @@ namespace Box\Spout\Writer\ODS\Manager; use Box\Spout\Common\Entity\Cell; use Box\Spout\Common\Entity\Row; -use Box\Spout\Common\Entity\Style\EmptyStyle; use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; @@ -158,13 +157,13 @@ class WorksheetManager implements WorksheetManagerInterface */ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentCellIndex, $nextCellIndex) { - if ($cell->getStyle() instanceof EmptyStyle) { + if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); - $extraStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($extraStyle) { - $registeredStyle = $this->styleManager->registerStyle($extraStyle); + if ($managedStyle->isUpdated()) { + $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); } else { $registeredStyle = $rowStyle = $this->styleManager->registerStyle($rowStyle); } @@ -172,7 +171,13 @@ class WorksheetManager implements WorksheetManagerInterface $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); $cell->setStyle($mergedCellAndRowStyle); - $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell) ?: $mergedCellAndRowStyle; + $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + if ($managedStyle->isUpdated()) { + $newCellStyle = $managedStyle->getStyle(); + } else { + $newCellStyle = $mergedCellAndRowStyle; + } + $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index ae1ecbb..d68d8f3 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -4,7 +4,6 @@ namespace Box\Spout\Writer\XLSX\Manager; use Box\Spout\Common\Entity\Cell; use Box\Spout\Common\Entity\Row; -use Box\Spout\Common\Entity\Style\EmptyStyle; use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; @@ -186,13 +185,13 @@ EOD; */ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $rowIndexOneBased, $columnIndexZeroBased) { - if ($cell->getStyle() instanceof EmptyStyle) { + if ($cell->getStyle()->isEmpty()) { $cell->setStyle($rowStyle); - $extraStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); - if ($extraStyle) { - $registeredStyle = $this->styleManager->registerStyle($extraStyle); + if ($managedStyle->isUpdated()) { + $registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle()); } else { $registeredStyle = $rowStyle = $this->styleManager->registerStyle($rowStyle); } @@ -200,7 +199,14 @@ EOD; $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); $cell->setStyle($mergedCellAndRowStyle); - $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell) ?: $mergedCellAndRowStyle; + $managedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + + if ($managedStyle->isUpdated()) { + $newCellStyle = $managedStyle->getStyle(); + } else { + $newCellStyle = $mergedCellAndRowStyle; + } + $registeredStyle = $this->styleManager->registerStyle($newCellStyle); } diff --git a/tests/Spout/Common/Manager/OptionsManagerTest.php b/tests/Spout/Common/Manager/OptionsManagerTest.php index b1cac58..d5fe5d8 100644 --- a/tests/Spout/Common/Manager/OptionsManagerTest.php +++ b/tests/Spout/Common/Manager/OptionsManagerTest.php @@ -14,7 +14,7 @@ class OptionsManagerTest extends TestCase */ protected $optionsManager; - protected function setUp(): void + protected function setUp() : void { $this->optionsManager = new class() extends OptionsManagerAbstract { protected function getSupportedOptions() diff --git a/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php index 22df444..f344b35 100644 --- a/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php +++ b/tests/Spout/Reader/XLSX/Manager/SharedStringsManagerTest.php @@ -25,7 +25,7 @@ class SharedStringsManagerTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->sharedStringsManager = null; } @@ -33,7 +33,7 @@ class SharedStringsManagerTest extends TestCase /** * @return void */ - public function tearDown(): void + public function tearDown() : void { if ($this->sharedStringsManager !== null) { $this->sharedStringsManager->cleanup(); diff --git a/tests/Spout/Writer/CSV/WriterPerfTest.php b/tests/Spout/Writer/CSV/WriterPerfTest.php index 4172bf5..cbfcae0 100644 --- a/tests/Spout/Writer/CSV/WriterPerfTest.php +++ b/tests/Spout/Writer/CSV/WriterPerfTest.php @@ -61,7 +61,7 @@ class WriterPerfTest extends TestCase */ private function getNumWrittenRows($resourcePath) { - $lineCountResult = `wc -l $resourcePath`; + $lineCountResult = shell_exec("wc -l $resourcePath"); return (int) $lineCountResult; } diff --git a/tests/Spout/Writer/Common/Entity/SheetTest.php b/tests/Spout/Writer/Common/Entity/SheetTest.php index 3c50734..e0cf8ee 100644 --- a/tests/Spout/Writer/Common/Entity/SheetTest.php +++ b/tests/Spout/Writer/Common/Entity/SheetTest.php @@ -18,7 +18,7 @@ class SheetTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->sheetManager = new SheetManager(new StringHelper()); } diff --git a/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php b/tests/Spout/Writer/Common/Manager/Style/StyleManagerTest.php index d326f9d..daf4abb 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(); - $updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); + $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); - $this->assertNotNull($updatedStyle); - $this->assertTrue($updatedStyle->shouldWrapText()); + $this->assertTrue($managedStyle->isUpdated()); + $this->assertTrue($managedStyle->getStyle()->shouldWrapText()); } public function testApplyExtraStylesIfNeededShouldReturnNullIfWrapTextNotNeeded() : void @@ -40,9 +40,9 @@ class StyleManagerTest extends TestCase $this->assertFalse($style->shouldWrapText()); $styleManager = $this->getStyleManager(); - $updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell('oneline', $style)); + $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell('oneline', $style)); - $this->assertNull($updatedStyle); + $this->assertFalse($managedStyle->isUpdated()); } public function testApplyExtraStylesIfNeededShouldReturnNullIfWrapTextAlreadyApplied() : void @@ -51,8 +51,8 @@ class StyleManagerTest extends TestCase $this->assertTrue($style->shouldWrapText()); $styleManager = $this->getStyleManager(); - $updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); + $managedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style)); - $this->assertNull($updatedStyle); + $this->assertFalse($managedStyle->isUpdated()); } } diff --git a/tests/Spout/Writer/Common/Manager/Style/StyleMergerTest.php b/tests/Spout/Writer/Common/Manager/Style/StyleMergerTest.php index a91d13f..b7c1607 100644 --- a/tests/Spout/Writer/Common/Manager/Style/StyleMergerTest.php +++ b/tests/Spout/Writer/Common/Manager/Style/StyleMergerTest.php @@ -18,7 +18,7 @@ class StyleMergerTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->styleMerger = new StyleMerger(); } diff --git a/tests/Spout/Writer/Common/Manager/Style/StyleRegistryTest.php b/tests/Spout/Writer/Common/Manager/Style/StyleRegistryTest.php index 20a764c..ac78c9e 100644 --- a/tests/Spout/Writer/Common/Manager/Style/StyleRegistryTest.php +++ b/tests/Spout/Writer/Common/Manager/Style/StyleRegistryTest.php @@ -20,7 +20,7 @@ class StyleRegistryTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->defaultStyle = (new StyleBuilder())->build(); $this->styleRegistry = new StyleRegistry($this->defaultStyle); diff --git a/tests/Spout/Writer/ODS/WriterPerfTest.php b/tests/Spout/Writer/ODS/WriterPerfTest.php index 40c4ee0..5917b49 100644 --- a/tests/Spout/Writer/ODS/WriterPerfTest.php +++ b/tests/Spout/Writer/ODS/WriterPerfTest.php @@ -88,7 +88,7 @@ class WriterPerfTest extends TestCase copy($pathToContentXmlFile, $tmpFile); // Get the last 200 characters - $lastCharacters = `tail -c 200 $tmpFile`; + $lastCharacters = shell_exec("tail -c 200 $tmpFile"); // remove the temporary file unlink($tmpFile); diff --git a/tests/Spout/Writer/ODS/WriterWithStyleTest.php b/tests/Spout/Writer/ODS/WriterWithStyleTest.php index 669d438..75177da 100644 --- a/tests/Spout/Writer/ODS/WriterWithStyleTest.php +++ b/tests/Spout/Writer/ODS/WriterWithStyleTest.php @@ -30,7 +30,7 @@ class WriterWithStyleTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->defaultStyle = (new StyleBuilder())->build(); } diff --git a/tests/Spout/Writer/XLSX/WriterPerfTest.php b/tests/Spout/Writer/XLSX/WriterPerfTest.php index f43381f..66ff337 100644 --- a/tests/Spout/Writer/XLSX/WriterPerfTest.php +++ b/tests/Spout/Writer/XLSX/WriterPerfTest.php @@ -135,7 +135,7 @@ class WriterPerfTest extends TestCase copy($filePath, $tmpFile); // Get the last 200 characters - $lastCharacters = `tail -c $numCharacters $tmpFile`; + $lastCharacters = shell_exec("tail -c $numCharacters $tmpFile"); // remove the temporary file unlink($tmpFile); diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index da063e8..2571df4 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -32,7 +32,7 @@ class WriterWithStyleTest extends TestCase /** * @return void */ - public function setUp(): void + public function setUp() : void { $this->defaultStyle = (new StyleBuilder())->build(); }