Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 8a17d6c

Browse files
alamiraultadrilo
authored andcommitted
Remove rowStyle reference and replace it by new RegisteredStyle class
1 parent c6f596c commit 8a17d6c

3 files changed

Lines changed: 66 additions & 18 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Box\Spout\Writer\Common\Manager;
4+
5+
use Box\Spout\Common\Entity\Style\Style;
6+
7+
class RegisteredStyle
8+
{
9+
/**
10+
* @var Style
11+
*/
12+
private $style;
13+
14+
/**
15+
* @var bool
16+
*/
17+
private $isRowStyle;
18+
19+
public function __construct(Style $style, bool $isRowStyle)
20+
{
21+
$this->style = $style;
22+
$this->isRowStyle = $isRowStyle;
23+
}
24+
25+
public function getStyle() : Style
26+
{
27+
return $this->style;
28+
}
29+
30+
public function isRowStyle() : bool
31+
{
32+
return $this->isRowStyle;
33+
}
34+
}

src/Spout/Writer/ODS/Manager/WorksheetManager.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
1111
use Box\Spout\Common\Helper\StringHelper;
1212
use Box\Spout\Writer\Common\Entity\Worksheet;
13+
use Box\Spout\Writer\Common\Manager\RegisteredStyle;
1314
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
1415
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
1516
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
@@ -93,7 +94,7 @@ public function getTableElementStartAsString(Worksheet $worksheet)
9394
$escapedSheetName = $this->stringsEscaper->escape($externalSheet->getName());
9495
$tableStyleName = 'ta' . ($externalSheet->getIndex() + 1);
9596

96-
$tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">';
97+
$tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">';
9798
$tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="co1" table:number-columns-repeated="' . $worksheet->getMaxNumColumns() . '"/>';
9899

99100
return $tableElement;
@@ -104,8 +105,8 @@ public function getTableElementStartAsString(Worksheet $worksheet)
104105
*
105106
* @param Worksheet $worksheet The worksheet to add the row to
106107
* @param Row $row The row to be added
107-
* @throws IOException If the data cannot be written
108108
* @throws InvalidArgumentException If a cell value's type is not supported
109+
* @throws IOException If the data cannot be written
109110
* @return void
110111
*/
111112
public function addRow(Worksheet $worksheet, Row $row)
@@ -125,7 +126,13 @@ public function addRow(Worksheet $worksheet, Row $row)
125126
$nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null;
126127

127128
if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) {
128-
$data .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $currentCellIndex, $nextCellIndex);
129+
$registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle);
130+
$cellStyle = $registeredStyle->getStyle();
131+
if ($registeredStyle->isRowStyle()) {
132+
$rowStyle = $cellStyle;
133+
}
134+
135+
$data .= $this->getCellXMLWithStyle($cell, $cellStyle, $currentCellIndex, $nextCellIndex);
129136
$currentCellIndex = $nextCellIndex;
130137
}
131138

@@ -146,17 +153,15 @@ public function addRow(Worksheet $worksheet, Row $row)
146153

147154
/**
148155
* Applies styles to the given style, merging the cell's style with its row's style
149-
* Then builds and returns xml for the cell.
150156
*
151157
* @param Cell $cell
152158
* @param Style $rowStyle
153-
* @param int $currentCellIndex
154-
* @param int $nextCellIndex
155159
* @throws InvalidArgumentException If a cell value's type is not supported
156-
* @return string
160+
* @return RegisteredStyle
157161
*/
158-
private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentCellIndex, $nextCellIndex)
162+
private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle
159163
{
164+
$isRowStyle = false;
160165
if ($cell->getStyle()->isEmpty()) {
161166
$cell->setStyle($rowStyle);
162167

@@ -166,7 +171,7 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentC
166171
$registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle());
167172
} else {
168173
$registeredStyle = $this->styleManager->registerStyle($rowStyle);
169-
$rowStyle = $registeredStyle;
174+
$isRowStyle = true;
170175
}
171176
} else {
172177
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
@@ -182,7 +187,12 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentC
182187
$registeredStyle = $this->styleManager->registerStyle($newCellStyle);
183188
}
184189

185-
$styleIndex = $registeredStyle->getId() + 1; // 1-based
190+
return new RegisteredStyle($registeredStyle, $isRowStyle);
191+
}
192+
193+
private function getCellXMLWithStyle(Cell $cell, Style $style, int $currentCellIndex, int $nextCellIndex) : string
194+
{
195+
$styleIndex = $style->getId() + 1; // 1-based
186196

187197
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
188198

src/Spout/Writer/XLSX/Manager/WorksheetManager.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Box\Spout\Writer\Common\Entity\Options;
1515
use Box\Spout\Writer\Common\Entity\Worksheet;
1616
use Box\Spout\Writer\Common\Helper\CellHelper;
17+
use Box\Spout\Writer\Common\Manager\RegisteredStyle;
1718
use Box\Spout\Writer\Common\Manager\RowManager;
1819
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
1920
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
@@ -160,7 +161,12 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row)
160161
$rowXML = '<row r="' . $rowIndexOneBased . '" spans="1:' . $numCells . '">';
161162

162163
foreach ($row->getCells() as $columnIndexZeroBased => $cell) {
163-
$rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndexOneBased, $columnIndexZeroBased);
164+
$registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle);
165+
$cellStyle = $registeredStyle->getStyle();
166+
if ($registeredStyle->isRowStyle()) {
167+
$rowStyle = $cellStyle;
168+
}
169+
$rowXML .= $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $cellStyle->getId());
164170
}
165171

166172
$rowXML .= '</row>';
@@ -173,18 +179,16 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row)
173179

174180
/**
175181
* Applies styles to the given style, merging the cell's style with its row's style
176-
* Then builds and returns xml for the cell.
177182
*
178183
* @param Cell $cell
179184
* @param Style $rowStyle
180-
* @param int $rowIndexOneBased
181-
* @param int $columnIndexZeroBased
182185
*
183186
* @throws InvalidArgumentException If the given value cannot be processed
184-
* @return string
187+
* @return RegisteredStyle
185188
*/
186-
private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $rowIndexOneBased, $columnIndexZeroBased)
189+
private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle
187190
{
191+
$isRowStyle = false;
188192
if ($cell->getStyle()->isEmpty()) {
189193
$cell->setStyle($rowStyle);
190194

@@ -194,7 +198,7 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $rowIndex
194198
$registeredStyle = $this->styleManager->registerStyle($managedStyle->getStyle());
195199
} else {
196200
$registeredStyle = $this->styleManager->registerStyle($rowStyle);
197-
$rowStyle = $registeredStyle;
201+
$isRowStyle = true;
198202
}
199203
} else {
200204
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
@@ -211,7 +215,7 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $rowIndex
211215
$registeredStyle = $this->styleManager->registerStyle($newCellStyle);
212216
}
213217

214-
return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId());
218+
return new RegisteredStyle($registeredStyle, $isRowStyle);
215219
}
216220

217221
/**

0 commit comments

Comments
 (0)