Integrated second batch of changes #135

Newline

Fix test, use reflection for protected static members
This commit is contained in:
madflow 2016-06-15 13:44:29 +02:00
parent 38a39edc9e
commit 7026d31631
11 changed files with 76 additions and 48 deletions

View File

@ -7,13 +7,10 @@ use Box\Spout\Writer\Style\BorderPart;
class InvalidNameException extends WriterException class InvalidNameException extends WriterException
{ {
public function __construct($name) public function __construct($name)
{ {
$msg = '%s is not a valid name identifier for a border. Valid identifiers are: %s.'; $msg = '%s is not a valid name identifier for a border. Valid identifiers are: %s.';
parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedNames()))); parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedNames())));
} }
} }

View File

@ -9,10 +9,8 @@ class InvalidStyleException extends WriterException
{ {
public function __construct($name) public function __construct($name)
{ {
$msg = '%s is not a valid style identifier for a border. Valid identifiers are: %s.'; $msg = '%s is not a valid style identifier for a border. Valid identifiers are: %s.';
parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedStyles()))); parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedStyles())));
} }
} }

View File

@ -7,12 +7,10 @@ use Box\Spout\Writer\Style\BorderPart;
class InvalidWidthException extends WriterException class InvalidWidthException extends WriterException
{ {
public function __construct($name) public function __construct($name)
{ {
$msg = '%s is not a valid width identifier for a border. Valid identifiers are: %s.'; $msg = '%s is not a valid width identifier for a border. Valid identifiers are: %s.';
parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedWidths()))); parent::__construct(sprintf($msg, $name, implode(',', BorderPart::getAllowedWidths())));
} }
} }

View File

@ -26,7 +26,7 @@ class BorderHelper
* *
* @var array * @var array
*/ */
public static $widthMap = [ protected static $widthMap = [
Border::WIDTH_THIN => '0.75pt', Border::WIDTH_THIN => '0.75pt',
Border::WIDTH_MEDIUM => '1.75pt', Border::WIDTH_MEDIUM => '1.75pt',
Border::WIDTH_THICK => '2.5pt', Border::WIDTH_THICK => '2.5pt',
@ -37,7 +37,7 @@ class BorderHelper
* *
* @var array * @var array
*/ */
public static $styleMap = [ protected static $styleMap = [
Border::STYLE_SOLID => 'solid', Border::STYLE_SOLID => 'solid',
Border::STYLE_DASHED => 'dashed', Border::STYLE_DASHED => 'dashed',
Border::STYLE_DOTTED => 'dotted', Border::STYLE_DOTTED => 'dotted',

View File

@ -28,7 +28,7 @@ class Border
protected $parts = []; protected $parts = [];
/** /**
* @param array $borderParts * @param array|void $borderParts
*/ */
public function __construct(array $borderParts = []) public function __construct(array $borderParts = [])
{ {

View File

@ -18,9 +18,9 @@ class BorderBuilder
} }
/** /**
* @param string $style Border style @see BorderPart::allowedStyles * @param string|void $style Border style @see BorderPart::allowedStyles
* @param string $color Border A RGB color code * @param string|void $color Border A RGB color code
* @param string $width Border width @see BorderPart::allowedWidths * @param string|void $width Border width @see BorderPart::allowedWidths
* @return self * @return self
*/ */
public function setBorderTop($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK) public function setBorderTop($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
@ -30,9 +30,9 @@ class BorderBuilder
} }
/** /**
* @param string $style Border style @see BorderPart::allowedStyles * @param string|void $style Border style @see BorderPart::allowedStyles
* @param string $color Border A RGB color code * @param string|void $color Border A RGB color code
* @param string $width Border width @see BorderPart::allowedWidths * @param string|void $width Border width @see BorderPart::allowedWidths
* @return self * @return self
*/ */
public function setBorderRight($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK) public function setBorderRight($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
@ -42,9 +42,9 @@ class BorderBuilder
} }
/** /**
* @param string $style Border style @see BorderPart::allowedStyles * @param string|void $style Border style @see BorderPart::allowedStyles
* @param string $color Border A RGB color code * @param string|void $color Border A RGB color code
* @param string $width Border width @see BorderPart::allowedWidths * @param string|void $width Border width @see BorderPart::allowedWidths
* @return self * @return self
*/ */
public function setBorderBottom($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK) public function setBorderBottom($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)
@ -54,9 +54,9 @@ class BorderBuilder
} }
/** /**
* @param string $style Border style @see BorderPart::allowedStyles * @param string|void $style Border style @see BorderPart::allowedStyles
* @param string $color Border A RGB color code * @param string|void $color Border A RGB color code
* @param string $width Border width @see BorderPart::allowedWidths * @param string|void $width Border width @see BorderPart::allowedWidths
* @return self * @return self
*/ */
public function setBorderLeft($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK) public function setBorderLeft($style = Border::STYLE_SOLID, $color = Color::BLACK, $width = Border::WIDTH_THICK)

View File

@ -62,9 +62,9 @@ class BorderPart
]; ];
/** /**
* @param string $name @see BorderPart::allowedNames * @param string $name @see BorderPart::$allowedNames
* @param string $style @see BorderPart::allowedStyles * @param string $style @see BorderPart::$allowedStyles
* @param string $width @see BorderPart::allowedWidths * @param string $width @see BorderPart::$allowedWidths
* @param string $color A RGB color code * @param string $color A RGB color code
* @throws InvalidNameException * @throws InvalidNameException
* @throws InvalidStyleException * @throws InvalidStyleException
@ -87,7 +87,9 @@ class BorderPart
} }
/** /**
* @param string $name * @param string $name The name of the border part @see BorderPart::$allowedNames
* @throws InvalidNameException
* @return void
*/ */
public function setName($name) public function setName($name)
{ {
@ -106,7 +108,9 @@ class BorderPart
} }
/** /**
* @param string $style * @param string $style The style of the border part @see BorderPart::$allowedStyles
* @throws InvalidStyleException
* @return void
*/ */
public function setStyle($style) public function setStyle($style)
{ {
@ -125,7 +129,8 @@ class BorderPart
} }
/** /**
* @param string $color * @param string $color The color of the border part @see Color::rgb()
* @return void
*/ */
public function setColor($color) public function setColor($color)
{ {
@ -141,7 +146,9 @@ class BorderPart
} }
/** /**
* @param string $width * @param string $width The width of the border part @see BorderPart::$allowedWidths
* @throws InvalidWidthException
* @return void
*/ */
public function setWidth($width) public function setWidth($width)
{ {

View File

@ -41,7 +41,7 @@ class BorderHelper
*/ */
public static function serializeBorderPart(BorderPart $borderPart) public static function serializeBorderPart(BorderPart $borderPart)
{ {
$borderStyle = self::$xlsxStyleMap[$borderPart->getStyle()][$borderPart->getWidth()]; $borderStyle = self::getBorderStyle($borderPart);
$colorEl = $borderPart->getColor() ? sprintf('<color rgb="%s"/>', $borderPart->getColor()) : ''; $colorEl = $borderPart->getColor() ? sprintf('<color rgb="%s"/>', $borderPart->getColor()) : '';
$partEl = sprintf( $partEl = sprintf(
@ -54,4 +54,15 @@ class BorderHelper
return $partEl . PHP_EOL; return $partEl . PHP_EOL;
} }
/**
* Get the style definition from the style map
*
* @param BorderPart $borderPart
* @return string
*/
protected static function getBorderStyle(BorderPart $borderPart)
{
return self::$xlsxStyleMap[$borderPart->getStyle()][$borderPart->getWidth()];
}
} }

View File

@ -100,10 +100,13 @@ EOD;
*/ */
protected function getBordersSectionContent() protected function getBordersSectionContent()
{ {
$content = '<borders count="' . count($this->styleIdToStyleMappingTable) . '">'; $registeredStyles = $this->getRegisteredStyles();
$registeredStylesCount = count($registeredStyles);
$content = '<borders count="' . $registeredStylesCount . '">';
/** @var \Box\Spout\Writer\Style\Style $style */ /** @var \Box\Spout\Writer\Style\Style $style */
foreach ($this->getRegisteredStyles() as $style) { foreach ($registeredStyles as $style) {
$border = $style->getBorder(); $border = $style->getBorder();
if ($border) { if ($border) {
$content .= '<border>'; $content .= '<border>';

View File

@ -268,30 +268,45 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->writeToODSFileWithMultipleStyles($dataRows, $fileName, $styles); $this->writeToODSFileWithMultipleStyles($dataRows, $fileName, $styles);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertEquals(3, count($styleElements), 'There should be 3 styles)'); $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();
$expectedFirst = sprintf( $expectedFirst = sprintf(
'%s %s #%s', '%s %s #%s',
BorderHelper::$widthMap[Border::WIDTH_THICK], $widthMap[Border::WIDTH_THICK],
BorderHelper::$styleMap[Border::STYLE_SOLID], $styleMap[Border::STYLE_SOLID],
Color::GREEN Color::GREEN
); );
$this->assertEquals($expectedFirst, $styleElements[1] $actualFirst = $styleElements[1]
->getElementsByTagName('table-cell-properties') ->getElementsByTagName('table-cell-properties')
->item(0) ->item(0)
->getAttribute('fo:border-bottom')); ->getAttribute('fo:border-bottom');
$this->assertEquals($expectedFirst, $actualFirst);
$expectedThird = sprintf( $expectedThird = sprintf(
'%s %s #%s', '%s %s #%s',
BorderHelper::$widthMap[Border::WIDTH_THIN], $widthMap[Border::WIDTH_THIN],
BorderHelper::$styleMap[Border::STYLE_DASHED], $styleMap[Border::STYLE_DASHED],
Color::RED Color::RED
); );
$this->assertEquals($expectedThird, $styleElements[2]
$actualThird = $styleElements[2]
->getElementsByTagName('table-cell-properties') ->getElementsByTagName('table-cell-properties')
->item(0) ->item(0)
->getAttribute('fo:border-top')); ->getAttribute('fo:border-top');
$this->assertEquals($expectedThird, $actualThird);
} }

View File

@ -89,19 +89,17 @@ class BorderTest extends \PHPUnit_Framework_TestCase
public function testAnyCombinationOfAllowedBorderPartsParams() public function testAnyCombinationOfAllowedBorderPartsParams()
{ {
$color = Color::BLACK; $color = Color::BLACK;
foreach(BorderPart::getAllowedNames() as $allowedName) foreach (BorderPart::getAllowedNames() as $allowedName) {
{ foreach (BorderPart::getAllowedStyles() as $allowedStyle) {
foreach(BorderPart::getAllowedStyles() as $allowedStyle) foreach (BorderPart::getAllowedWidths() as $allowedWidth) {
{
foreach(BorderPart::getAllowedWidths() as $allowedWidth)
{
$borderPart = new BorderPart($allowedName, $allowedStyle, $color, $allowedWidth); $borderPart = new BorderPart($allowedName, $allowedStyle, $color, $allowedWidth);
$border = new Border(); $border = new Border();
$border->addPart($borderPart); $border->addPart($borderPart);
$this->assertEquals(1, count($border->getParts())); $this->assertEquals(1, count($border->getParts()));
$part = $border->getParts()[$allowedName];
/** @var $part BorderPart */ /** @var $part BorderPart */
$part = $border->getParts()[$allowedName];
$this->assertEquals($allowedStyle, $part->getStyle()); $this->assertEquals($allowedStyle, $part->getStyle());
$this->assertEquals($allowedWidth, $part->getWidth()); $this->assertEquals($allowedWidth, $part->getWidth());
$this->assertEquals($color, $part->getColor()); $this->assertEquals($color, $part->getColor());
@ -109,4 +107,5 @@ class BorderTest extends \PHPUnit_Framework_TestCase
} }
} }
} }
} }