#729 - fix shrink to fit + add test
shrinkToFit was not handled in StyleMerger so it was overwritten by the default cell style StyleManager didn't add the property to the xml if shrinkToFit was used without alignment or text wrap. Unit test
This commit is contained in:
parent
f702127d3a
commit
817e4f1f01
@ -66,8 +66,10 @@ class Style
|
|||||||
/** @var bool Whether the wrap text property was set */
|
/** @var bool Whether the wrap text property was set */
|
||||||
private $hasSetWrapText = false;
|
private $hasSetWrapText = false;
|
||||||
|
|
||||||
private $shrinkToFit = false;
|
/** @var bool Whether the cell should shrink to fit to content */
|
||||||
private $shouldShrinkToFit = false;
|
private $shouldShrinkToFit = false;
|
||||||
|
/** @var bool Whether the shouldShrinkToFit text property was set */
|
||||||
|
private $hasSetShrinkToFit = false;
|
||||||
|
|
||||||
/** @var Border */
|
/** @var Border */
|
||||||
private $border;
|
private $border;
|
||||||
@ -474,7 +476,7 @@ class Style
|
|||||||
*/
|
*/
|
||||||
public function setShouldShrinkToFit($shrinkToFit = true)
|
public function setShouldShrinkToFit($shrinkToFit = true)
|
||||||
{
|
{
|
||||||
$this->shrinkToFit = $shrinkToFit;
|
$this->hasSetShrinkToFit = true;
|
||||||
$this->shouldShrinkToFit = $shrinkToFit;
|
$this->shouldShrinkToFit = $shrinkToFit;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -487,4 +489,12 @@ class Style
|
|||||||
{
|
{
|
||||||
return $this->shouldShrinkToFit;
|
return $this->shouldShrinkToFit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasSetShrinkToFit()
|
||||||
|
{
|
||||||
|
return $this->hasSetShrinkToFit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,8 @@ class StyleBuilder
|
|||||||
/**
|
/**
|
||||||
* Set should shrink to fit
|
* Set should shrink to fit
|
||||||
* @param bool $shrinkToFit
|
* @param bool $shrinkToFit
|
||||||
* @return void
|
* @return StyleBuilder
|
||||||
|
* @api
|
||||||
*/
|
*/
|
||||||
public function setShouldShrinkToFit($shrinkToFit = true)
|
public function setShouldShrinkToFit($shrinkToFit = true)
|
||||||
{
|
{
|
||||||
|
@ -85,6 +85,9 @@ class StyleMerger
|
|||||||
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
|
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
|
||||||
$styleToUpdate->setShouldWrapText();
|
$styleToUpdate->setShouldWrapText();
|
||||||
}
|
}
|
||||||
|
if (!$style->hasSetShrinkToFit() && $baseStyle->shouldShrinkToFit()) {
|
||||||
|
$styleToUpdate->setShouldShrinkToFit();
|
||||||
|
}
|
||||||
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
|
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
|
||||||
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
|
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ EOD;
|
|||||||
|
|
||||||
$content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
|
$content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
|
||||||
|
|
||||||
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
|
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText() || $style->shouldShrinkToFit()) {
|
||||||
$content .= ' applyAlignment="1">';
|
$content .= ' applyAlignment="1">';
|
||||||
$content .= '<alignment';
|
$content .= '<alignment';
|
||||||
if ($style->shouldApplyCellAlignment()) {
|
if ($style->shouldApplyCellAlignment()) {
|
||||||
|
@ -306,6 +306,24 @@ class WriterWithStyleTest extends TestCase
|
|||||||
$this->assertFirstChildHasAttributeEquals(CellAlignment::RIGHT, $xfElement, 'alignment', 'horizontal');
|
$this->assertFirstChildHasAttributeEquals(CellAlignment::RIGHT, $xfElement, 'alignment', 'horizontal');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAddRowShouldApplyShrinkToFit()
|
||||||
|
{
|
||||||
|
$fileName = 'test_add_row_should_apply_shrink_to_fit.xlsx';
|
||||||
|
|
||||||
|
$shrinkToFitStyle = (new StyleBuilder())->setShouldShrinkToFit()->build();
|
||||||
|
$dataRows = $this->createStyledRowsFromValues([['xlsx--11']], $shrinkToFitStyle);
|
||||||
|
|
||||||
|
$this->writeToXLSXFile($dataRows, $fileName);
|
||||||
|
|
||||||
|
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||||
|
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||||
|
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
|
||||||
|
$this->assertFirstChildHasAttributeEquals("true", $xfElement, 'alignment', 'shrinkToFit');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user