Use ReflectionHelper, changed ordering of border part definition, updated documentation #135

This commit is contained in:
madflow 2016-06-18 12:09:46 +02:00
parent 7026d31631
commit efca3f8fcd
7 changed files with 43 additions and 44 deletions

View File

@ -154,19 +154,24 @@ Adding borders to a row requires a ```Border``` object.
```php
use Box\Spout\Common\Type;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\Style\Border;
use Box\Spout\Writer\Style\BorderBuilder;
use Box\Spout\Writer\Style\Color;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory;
$border = (new BorderBuilder())
->setBorderBottom(Border::STYLE_DASHED, Color::GREEN, Border::WIDTH_THIN)
->build();
->setBorderBottom(Color::GREEN, Border::WIDTH_THIN, Border::STYLE_DASHED)
->build();
$style = (new StyleBuilder())
->setBorder($border)
->build();
->setBorder($border)
->build();
$writer->addRowWithStyle(['Border Bottom Dashed Green Thin'], $style);
$writer = WriterFactory::create(Type::ODS);
$writer->openToFile(__DIR__ . '/borders.xlsx');
$writer->addRowWithStyle(['Border Bottom Green Thin Dashed'], $style)
$writer->close();

View File

@ -18,50 +18,50 @@ class BorderBuilder
}
/**
* @param string|void $style Border style @see BorderPart::allowedStyles
* @param string|void $color Border A RGB color code
* @param string|void $width Border width @see BorderPart::allowedWidths
* @return self
* @param string|void $style Border style @see BorderPart::allowedStyles
* @return BorderBuilder
*/
public function setBorderTop($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
public function setBorderTop($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
{
$this->border->addPart(new BorderPart(Border::TOP, $style, $color, $width));
$this->border->addPart(new BorderPart(Border::TOP, $color, $width, $style));
return $this;
}
/**
* @param string|void $style Border style @see BorderPart::allowedStyles
* @param string|void $color Border A RGB color code
* @param string|void $width Border width @see BorderPart::allowedWidths
* @return self
* @param string|void $style Border style @see BorderPart::allowedStyles
* @return BorderBuilder
*/
public function setBorderRight($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
public function setBorderRight($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
{
$this->border->addPart(new BorderPart(Border::RIGHT, $style, $color, $width));
$this->border->addPart(new BorderPart(Border::RIGHT, $color, $width, $style));
return $this;
}
/**
* @param string|void $style Border style @see BorderPart::allowedStyles
* @param string|void $color Border A RGB color code
* @param string|void $width Border width @see BorderPart::allowedWidths
* @return self
* @param string|void $style Border style @see BorderPart::allowedStyles
* @return BorderBuilder
*/
public function setBorderBottom($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
public function setBorderBottom($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
{
$this->border->addPart(new BorderPart(Border::BOTTOM, $style, $color, $width));
$this->border->addPart(new BorderPart(Border::BOTTOM, $color, $width, $style));
return $this;
}
/**
* @param string|void $style Border style @see BorderPart::allowedStyles
* @param string|void $color Border A RGB color code
* @param string|void $width Border width @see BorderPart::allowedWidths
* @return self
* @param string|void $style Border style @see BorderPart::allowedStyles
* @return BorderBuilder
*/
public function setBorderLeft($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
public function setBorderLeft($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
{
$this->border->addPart(new BorderPart(Border::LEFT, $style, $color, $width));
$this->border->addPart(new BorderPart(Border::LEFT, $color, $width, $style));
return $this;
}

View File

@ -63,19 +63,19 @@ class BorderPart
/**
* @param string $name @see BorderPart::$allowedNames
* @param string $style @see BorderPart::$allowedStyles
* @param string $width @see BorderPart::$allowedWidths
* @param string $color A RGB color code
* @param string $width @see BorderPart::$allowedWidths
* @param string $style @see BorderPart::$allowedStyles
* @throws InvalidNameException
* @throws InvalidStyleException
* @throws InvalidWidthException
*/
public function __construct($name, $style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_MEDIUM)
public function __construct($name, $color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
{
$this->setName($name);
$this->setStyle($style);
$this->setWidth($width);
$this->setColor($color);
$this->setWidth($width);
$this->setStyle($style);
}
/**

View File

@ -253,11 +253,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
];
$borderBottomGreenThickSolid = (new BorderBuilder())
->setBorderBottom(Border::STYLE_SOLID, Color::GREEN, Border::WIDTH_THICK)->build();
->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build();
$borderTopRedThinDashed = (new BorderBuilder())
->setBorderTop(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build();
->setBorderTop(Color::RED, Border::WIDTH_THIN, Border::STYLE_DASHED)->build();
$styles = [
(new StyleBuilder())->setBorder($borderBottomGreenThickSolid)->build(),
@ -272,13 +272,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(3, count($styleElements), 'There should be 3 styles)');
// Use reflection for protected members here
$widthMapReflection = new \ReflectionProperty('Box\Spout\Writer\ODS\Helper\BorderHelper', 'widthMap');
$widthMapReflection->setAccessible(true);
$widthMap = $widthMapReflection->getValue();
$styleMapReflection = new \ReflectionProperty('Box\Spout\Writer\ODS\Helper\BorderHelper', 'styleMap');
$styleMapReflection->setAccessible(true);
$styleMap = $styleMapReflection->getValue();
$widthMap = \ReflectionHelper::getStaticValue('Box\Spout\Writer\ODS\Helper\BorderHelper', 'widthMap');
$styleMap = \ReflectionHelper::getStaticValue('Box\Spout\Writer\ODS\Helper\BorderHelper', 'styleMap');
$expectedFirst = sprintf(
'%s %s #%s',

View File

@ -28,7 +28,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidBorderPartStyle()
{
$invalidBorderPartStyle = new BorderPart(Border::LEFT, 'invalid');
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Color::BLACK, Border::WIDTH_THIN, 'invalid');
}
/**
@ -36,7 +36,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidBorderPartWidth()
{
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Border::STYLE_DASHED, Color::BLACK, 'invalid');
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Color::BLACK, 'invalid', Border::STYLE_DASHED);
}
/**
@ -92,7 +92,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
foreach (BorderPart::getAllowedNames() as $allowedName) {
foreach (BorderPart::getAllowedStyles() as $allowedStyle) {
foreach (BorderPart::getAllowedWidths() as $allowedWidth) {
$borderPart = new BorderPart($allowedName, $allowedStyle, $color, $allowedWidth);
$borderPart = new BorderPart($allowedName, $color, $allowedWidth, $allowedStyle);
$border = new Border();
$border->addPart($borderPart);
$this->assertEquals(1, count($border->getParts()));
@ -108,4 +108,3 @@ class BorderTest extends \PHPUnit_Framework_TestCase
}
}
}

View File

@ -147,7 +147,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
*/
public function testStyleBuilderShouldMergeBorders()
{
$border = (new BorderBuilder())->setBorderBottom(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build();
$border = (new BorderBuilder())->setBorderBottom(Color::RED, Border::WIDTH_THIN, Border::STYLE_DASHED)->build();
$baseStyle = (new StyleBuilder())->setBorder($border)->build();
$currentStyle = (new StyleBuilder())->build();

View File

@ -248,11 +248,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
];
$borderBottomGreenThickSolid = (new BorderBuilder())
->setBorderBottom(Border::STYLE_SOLID, Color::GREEN, Border::WIDTH_THICK)->build();
->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build();
$borderTopRedThinDashed = (new BorderBuilder())
->setBorderTop(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build();
->setBorderTop(Color::RED, Border::WIDTH_THIN, Border::STYLE_DASHED)->build();
$styles = [
(new StyleBuilder())->setBorder($borderBottomGreenThickSolid)->build(),