Border ordering (#273)

* Fix #271

* Minor fix for quick copy+paste

* Added link to issue #271
This commit is contained in:
madflow 2016-07-13 20:45:37 +02:00 committed by Adrien Loison
parent 54a1a09e29
commit 7a613eed8c
4 changed files with 75 additions and 5 deletions

View File

@ -179,7 +179,7 @@ $style = (new StyleBuilder())
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->addRowWithStyle(['Border Bottom Green Thin Dashed'], $style) $writer->addRowWithStyle(['Border Bottom Green Thin Dashed'], $style);
$writer->close(); $writer->close();
``` ```

View File

@ -35,6 +35,24 @@ class Border
$this->setParts($borderParts); $this->setParts($borderParts);
} }
/**
* @param $name The name of the border part
* @return null|BorderPart
*/
public function getPart($name)
{
return $this->hasPart($name) ? $this->parts[$name] : null;
}
/**
* @param $name The name of the border part
* @return bool
*/
public function hasPart($name)
{
return isset($this->parts[$name]);
}
/** /**
* @return array * @return array
*/ */

View File

@ -110,13 +110,22 @@ EOD;
$border = $style->getBorder(); $border = $style->getBorder();
if ($border) { if ($border) {
$content .= '<border>'; $content .= '<border>';
foreach ($border->getParts() as $part) {
/** @var $part \Box\Spout\Writer\Style\BorderPart */ // @link https://github.com/box/spout/issues/271
$content .= BorderHelper::serializeBorderPart($part); $sortOrder = ['left', 'right', 'top', 'bottom'];
foreach ($sortOrder as $partName) {
if ($border->hasPart($partName)) {
/** @var $part \Box\Spout\Writer\Style\BorderPart */
$part = $border->getPart($partName);
$content .= BorderHelper::serializeBorderPart($part);
}
} }
$content .= '</border>'; $content .= '</border>';
} else { } else {
$content .= '<border><left/><right/><top/><bottom/><diagonal/></border>'; $content .= '<border><left/><right/><top/><bottom/></border>';
} }
} }

View File

@ -268,6 +268,49 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(3, $styleXfsElements->getAttribute('count'), '3 cell xfs present'); $this->assertEquals(3, $styleXfsElements->getAttribute('count'), '3 cell xfs present');
} }
/**
* @return void
*/
public function testBordersCorrectOrder()
{
// Border should be Left, Right, Top, Bottom
$fileName = 'test_borders_correct_order.xlsx';
$dataRows = [
['I am a teapot'],
];
$borders = (new BorderBuilder())
->setBorderRight()
->setBorderTop()
->setBorderLeft()
->setBorderBottom()
->build();
$style = (new StyleBuilder())->setBorder($borders)->build();
$this->writeToXLSXFile($dataRows, $fileName, $style);
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
$correctOrdering = [
'left', 'right', 'top', 'bottom'
];
/** @var $borderNode \DOMElement */
foreach ($borderElements->childNodes as $borderNode) {
$borderParts = $borderNode->childNodes;
$ordering = [];
/** @var $part \DOMText */
foreach ($borderParts as $part) {
if ($part instanceof \DOMElement) {
$ordering[] = $part->nodeName;
}
}
$this->assertEquals($correctOrdering, $ordering, 'The border parts are in correct ordering');
};
}
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName