Merge ede3f43c2e00acde95eaae1bb0bec028f0806dee into 582da8403d22bd4f284cce8c963513819137ec58

This commit is contained in:
Jan Wolters 2015-11-06 15:27:37 +00:00
commit b938653a02
7 changed files with 84 additions and 38 deletions

View File

@ -209,11 +209,15 @@ abstract class AbstractWriter implements WriterInterface
*/ */
public function addRowWithStyle(array $dataRow, $style) public function addRowWithStyle(array $dataRow, $style)
{ {
$styles = is_array($style) ? $style : [$style];
foreach ($styles as $style) {
if (!$style instanceof Style\Style) { if (!$style instanceof Style\Style) {
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.'); throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
} }
}
$this->setRowStyle($style); $this->setRowStyle($styles);
$this->addRow($dataRow); $this->addRow($dataRow);
$this->resetRowStyleToDefault(); $this->resetRowStyleToDefault();
@ -230,6 +234,7 @@ abstract class AbstractWriter implements WriterInterface
* ['data11', 12, , '', 'data13'], * ['data11', 12, , '', 'data13'],
* ['data21', 'data22', null, false], * ['data21', 'data22', null, false],
* ]; * ];
*
* @return AbstractWriter * @return AbstractWriter
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
@ -264,11 +269,15 @@ abstract class AbstractWriter implements WriterInterface
*/ */
public function addRowsWithStyle(array $dataRows, $style) public function addRowsWithStyle(array $dataRows, $style)
{ {
$styles = is_array($style) ? $style : [$style];
foreach ($styles as $style) {
if (!$style instanceof Style\Style) { if (!$style instanceof Style\Style) {
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.'); throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
} }
}
$this->setRowStyle($style); $this->setRowStyle($styles);
$this->addRows($dataRows); $this->addRows($dataRows);
$this->resetRowStyleToDefault(); $this->resetRowStyleToDefault();
@ -296,7 +305,11 @@ abstract class AbstractWriter implements WriterInterface
private function setRowStyle($style) private function setRowStyle($style)
{ {
// Merge given style with the default one to inherit custom properties // Merge given style with the default one to inherit custom properties
$this->rowStyle = $style->mergeWith($this->defaultRowStyle); $this->rowStyle = [];
$styles = is_array($style) ? $style : [$style];
foreach($styles as $style) {
$this->rowStyle[] = $style->mergeWith($this->defaultRowStyle);
}
} }
/** /**

View File

@ -34,6 +34,10 @@ abstract class AbstractStyleHelper
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
$styles = is_array($style) ? $style : [$style];
foreach ($styles as $style) {
$serializedStyle = $style->serialize(); $serializedStyle = $style->serialize();
if (!$this->hasStyleAlreadyBeenRegistered($style)) { if (!$this->hasStyleAlreadyBeenRegistered($style)) {
@ -43,8 +47,14 @@ abstract class AbstractStyleHelper
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
$this->styleIdToStyleMappingTable[$nextStyleId] = $style; $this->styleIdToStyleMappingTable[$nextStyleId] = $style;
} }
}
return $this->getStyleFromSerializedStyle($serializedStyle); $return = [];
foreach ($this->serializedStyleToStyleIdMappingTable as $serializedStyle => $styleId) {
$return[$styleId] = $this->styleIdToStyleMappingTable[$styleId];
}
return $return;
} }
/** /**
@ -120,14 +130,20 @@ abstract class AbstractStyleHelper
protected function applyWrapTextIfCellContainsNewLine($style, $dataRow) protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
{ {
// if the "wrap text" option is already set, no-op // if the "wrap text" option is already set, no-op
if ($style->shouldWrapText()) { if (!is_array($style) && $style->shouldWrapText()) {
return $style; return $style;
} }
foreach ($dataRow as $cell) { foreach ($dataRow as $index => $cell) {
if (is_string($cell) && strpos($cell, "\n") !== false) { if (is_string($cell) && strpos($cell, "\n") !== false) {
// if the "wrap text" option is already set, no-op
if (!is_array($style)) {
$style->setShouldWrapText(); $style->setShouldWrapText();
break; break;
} else if (!$style[$index]->shouldWrapText()) {
$style[$index]->setShouldWrapText();
}
break;
} }
} }

View File

@ -24,8 +24,11 @@ class StyleHelper extends AbstractStyleHelper
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
$styles = is_array($style) ? $style : [$style];
foreach ($styles as $style ) {
$this->usedFontsSet[$style->getFontName()] = true; $this->usedFontsSet[$style->getFontName()] = true;
return parent::registerStyle($style); }
return parent::registerStyle($styles);
} }
/** /**

View File

@ -134,7 +134,9 @@ class Worksheet implements WorksheetInterface
*/ */
public function addRow($dataRow, $style) public function addRow($dataRow, $style)
{ {
if (!is_array($style)) {
$styleIndex = ($style->getId() + 1); // 1-based $styleIndex = ($style->getId() + 1); // 1-based
}
$cellsCount = count($dataRow); $cellsCount = count($dataRow);
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount); $this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
@ -146,6 +148,10 @@ class Worksheet implements WorksheetInterface
for ($i = 0; $i < $cellsCount; $i++) { for ($i = 0; $i < $cellsCount; $i++) {
$currentCellValue = $dataRow[$currentCellIndex]; $currentCellValue = $dataRow[$currentCellIndex];
if (is_array($style)) {
$styleIndex = 1 + (isset($style[$i]) ? $style[$i]->getId() : $style[0]->getId());
}
if (!array_key_exists($nextCellIndex, $dataRow) || $currentCellValue !== $dataRow[$nextCellIndex]) { if (!array_key_exists($nextCellIndex, $dataRow) || $currentCellValue !== $dataRow[$nextCellIndex]) {
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); $numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated); $data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);

View File

@ -133,10 +133,16 @@ EOD;
$data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">'; $data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
foreach($dataRow as $cellValue) { if (!is_array($style)) {
$style = [$style];
}
foreach($dataRow as $index => $cellValue) {
$styleIndex = isset($style[$index]) ? $style[$index]->getId() : $style[0]->getId();
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$data .= '<c r="' . $columnIndex . $rowIndex . '"'; $data .= '<c r="' . $columnIndex . $rowIndex . '"';
$data .= ' s="' . $style->getId() . '"'; $data .= ' s="' . $styleIndex . '"';
if (CellHelper::isNonEmptyString($cellValue)) { if (CellHelper::isNonEmptyString($cellValue)) {
if ($this->shouldUseInlineStrings) { if ($this->shouldUseInlineStrings) {

View File

@ -35,11 +35,11 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$this->assertNull($style2->getId()); $this->assertNull($style2->getId());
$styleHelper = new StyleHelper($this->defaultStyle); $styleHelper = new StyleHelper($this->defaultStyle);
$registeredStyle1 = $styleHelper->registerStyle($style1); $registeredStyles = $styleHelper->registerStyle($style1);
$registeredStyle2 = $styleHelper->registerStyle($style2); $registeredStyles = $styleHelper->registerStyle($style2);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyles[1]->getId());
$this->assertEquals(2, $registeredStyle2->getId()); $this->assertEquals(2, $registeredStyles[2]->getId());
} }
/** /**
@ -50,11 +50,12 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$styleHelper = new StyleHelper($this->defaultStyle); $styleHelper = new StyleHelper($this->defaultStyle);
$registeredStyle1 = $styleHelper->registerStyle($style); $registeredStyles = $styleHelper->registerStyle($style);
$registeredStyle2 = $styleHelper->registerStyle($style); $registeredStyles = $styleHelper->registerStyle($style);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(0, $registeredStyles[0]->getId());
$this->assertEquals(1, $registeredStyle2->getId()); $this->assertEquals(1, $registeredStyles[1]->getId());
$this->assertEquals(2, count($registeredStyles));
} }
/** /**

View File

@ -35,11 +35,11 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$this->assertNull($style2->getId()); $this->assertNull($style2->getId());
$styleHelper = new StyleHelper($this->defaultStyle); $styleHelper = new StyleHelper($this->defaultStyle);
$registeredStyle1 = $styleHelper->registerStyle($style1); $registeredStyles = $styleHelper->registerStyle($style1);
$registeredStyle2 = $styleHelper->registerStyle($style2); $registeredStyles = $styleHelper->registerStyle($style2);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyles[1]->getId());
$this->assertEquals(2, $registeredStyle2->getId()); $this->assertEquals(2, $registeredStyles[2]->getId());
} }
/** /**
@ -50,11 +50,12 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$styleHelper = new StyleHelper($this->defaultStyle); $styleHelper = new StyleHelper($this->defaultStyle);
$registeredStyle1 = $styleHelper->registerStyle($style); $registeredStyles = $styleHelper->registerStyle($style);
$registeredStyle2 = $styleHelper->registerStyle($style); $registeredStyles = $styleHelper->registerStyle($style);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(0, $registeredStyles[0]->getId());
$this->assertEquals(1, $registeredStyle2->getId()); $this->assertEquals(1, $registeredStyles[1]->getId());
$this->assertEquals(2, count($registeredStyles));
} }
/** /**