Additional unit tests and some code deduplication

This commit is contained in:
Ivan Lanin 2014-04-11 21:16:07 +07:00
parent ae652a6379
commit a3a9af51e5
12 changed files with 96 additions and 74 deletions

View File

@ -66,9 +66,6 @@ class Section extends AbstractElement
if (is_null($value)) { if (is_null($value)) {
continue; continue;
} }
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->settings->setSettingValue($key, $value); $this->settings->setSettingValue($key, $value);
} }
} }

View File

@ -77,6 +77,23 @@ class String
return $value; return $value;
} }
/**
* Return name without underscore for < 0.10.0 variable name compatibility
*
* @param string $value
* @return string
*/
public static function removeUnderscorePrefix($value)
{
if (!is_null($value)) {
if (substr($value, 0, 1) == '_') {
$value = substr($value, 1);
}
}
return $value;
}
/** /**
* Build control characters array * Build control characters array
*/ */

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/** /**
* Abstract style class * Abstract style class
* *
@ -50,7 +52,10 @@ abstract class AbstractStyle
/** /**
* Set style value template method * Set style value template method
* *
* Some child classes have their own specific overrides * Some child classes have their own specific overrides.
* Backward compability check for versions < 0.10.0 which use underscore
* prefix for their private properties.
* Check if the set method is exists. Throws an exception?
* *
* @param string $key * @param string $key
* @param string $value * @param string $value
@ -58,14 +63,7 @@ abstract class AbstractStyle
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
// Backward compability check for versions < 0.10.0 which use underscore $method = 'set' . String::removeUnderscorePrefix($key);
// prefix for their private properties
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
// Check if the set method is exists. Throws an exception?
$method = 'set' . $key;
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->$method($value); $this->$method($value);
} }

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/** /**
* Table cell style * Table cell style
*/ */
@ -145,9 +147,7 @@ class Cell extends AbstractStyle
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
if (substr($key, 0, 1) == '_') { $key = String::removeUnderscorePrefix($key);
$key = substr($key, 1);
}
if ($key == 'borderSize') { if ($key == 'borderSize') {
$this->setBorderSize($value); $this->setBorderSize($value);
} elseif ($key == 'borderColor') { } elseif ($key == 'borderColor') {

View File

@ -193,8 +193,6 @@ class Font extends AbstractStyle
if ($key === 'line-height') { if ($key === 'line-height') {
$this->setLineHeight($value); $this->setLineHeight($value);
null; null;
} elseif (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
} }
$this->setStyleValue($key, $value); $this->setStyleValue($key, $value);
} }

View File

@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Exception\InvalidStyleException; use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\Shared\String;
/** /**
* Paragraph style * Paragraph style
@ -127,8 +128,6 @@ class Paragraph extends AbstractStyle
foreach ($style as $key => $value) { foreach ($style as $key => $value) {
if ($key === 'line-height') { if ($key === 'line-height') {
null; null;
} elseif (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
} }
$this->setStyleValue($key, $value); $this->setStyleValue($key, $value);
} }
@ -144,9 +143,7 @@ class Paragraph extends AbstractStyle
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
if (substr($key, 0, 1) == '_') { $key = String::removeUnderscorePrefix($key);
$key = substr($key, 1);
}
if ($key == 'indent' || $key == 'hanging') { if ($key == 'indent' || $key == 'hanging') {
$value = $value * 720; $value = $value * 720;
} elseif ($key == 'spacing') { } elseif ($key == 'spacing') {

View File

@ -45,16 +45,12 @@ class Row extends AbstractStyle
/** /**
* Set tblHeader * Set tblHeader
* *
* @param boolean $pValue * @param boolean $value
* @return $this * @return self
*/ */
public function setTblHeader($pValue = false) public function setTblHeader($value = false)
{ {
if (!is_bool($pValue)) { $this->tblHeader = $this->setBoolVal($value, $this->tblHeader);
$pValue = false;
}
$this->tblHeader = $pValue;
return $this;
} }
/** /**
@ -70,16 +66,12 @@ class Row extends AbstractStyle
/** /**
* Set cantSplit * Set cantSplit
* *
* @param boolean $pValue * @param boolean $value
* @return $this * @return self
*/ */
public function setCantSplit($pValue = false) public function setCantSplit($value = false)
{ {
if (!is_bool($pValue)) { $this->cantSplit = $this->setBoolVal($value, $this->cantSplit);
$pValue = false;
}
$this->cantSplit = $pValue;
return $this;
} }
/** /**
@ -95,15 +87,12 @@ class Row extends AbstractStyle
/** /**
* Set exactHeight * Set exactHeight
* *
* @param bool $pValue * @param bool $value
* @return $this * @return self
*/ */
public function setExactHeight($pValue = false) public function setExactHeight($value = false)
{ {
if (!is_bool($pValue)) { $this->exactHeight = $this->setBoolVal($value, $this->exactHeight);
$pValue = false;
}
$this->exactHeight = $pValue;
return $this; return $this;
} }

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/** /**
* Section settings * Section settings
*/ */
@ -217,9 +219,7 @@ class Section extends AbstractStyle
*/ */
public function setSettingValue($key, $value) public function setSettingValue($key, $value)
{ {
if (substr($key, 0, 1) == '_') { $key = String::removeUnderscorePrefix($key);
$key = substr($key, 1);
}
if ($key == 'orientation' && $value == 'landscape') { if ($key == 'orientation' && $value == 'landscape') {
$this->setLandscape(); $this->setLandscape();
} elseif ($key == 'orientation' && is_null($value)) { } elseif ($key == 'orientation' && is_null($value)) {

View File

@ -110,15 +110,4 @@ class TOC extends AbstractStyle
{ {
$this->indent = $pValue; $this->indent = $pValue;
} }
/**
* Set style value
*
* @param string $key
* @param string $value
*/
public function setStyleValue($key, $value)
{
$this->$key = $value;
}
} }

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
/** /**
* Table style * Table style
*/ */
@ -161,18 +163,12 @@ class Table extends AbstractStyle
unset($this->firstRow->borderInsideHColor); unset($this->firstRow->borderInsideHColor);
unset($this->firstRow->borderInsideHSize); unset($this->firstRow->borderInsideHSize);
foreach ($styleFirstRow as $key => $value) { foreach ($styleFirstRow as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->firstRow->setStyleValue($key, $value); $this->firstRow->setStyleValue($key, $value);
} }
} }
if (!is_null($styleTable) && is_array($styleTable)) { if (!is_null($styleTable) && is_array($styleTable)) {
foreach ($styleTable as $key => $value) { foreach ($styleTable as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$this->setStyleValue($key, $value); $this->setStyleValue($key, $value);
} }
} }
@ -186,9 +182,7 @@ class Table extends AbstractStyle
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
if (substr($key, 0, 1) == '_') { $key = String::removeUnderscorePrefix($key);
$key = substr($key, 1);
}
if ($key == 'borderSize') { if ($key == 'borderSize') {
$this->setBorderSize($value); $this->setBorderSize($value);
} elseif ($key == 'borderColor') { } elseif ($key == 'borderColor') {

View File

@ -82,9 +82,6 @@ class TOC
if (!is_null($styleTOC) && is_array($styleTOC)) { if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) { foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
self::$TOCStyle->setStyleValue($key, $value); self::$TOCStyle->setStyleValue($key, $value);
} }
} }
@ -93,9 +90,6 @@ class TOC
if (is_array($styleFont)) { if (is_array($styleFont)) {
self::$fontStyle = new Font(); self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) { foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
self::$fontStyle->setStyleValue($key, $value); self::$fontStyle->setStyleValue($key, $value);
} }
} else { } else {

View File

@ -0,0 +1,49 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Tests\Style;
use PhpOffice\PhpWord\Style\NumberingLevel;
/**
* Test class for PhpOffice\PhpWord\Style\NumberingLevel
*
* @runTestsInSeparateProcesses
*/
class NumberingLevelTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new NumberingLevel();
$attributes = array(
'level' => 1,
'start' => 1,
'format' => 'decimal',
'restart' => 1,
'suffix' => 'space',
'text' => '%1.',
'align' => 'left',
'left' => 360,
'hanging' => 360,
'tabPos' => 360,
'font' => 'Arial',
'hint' => 'default',
);
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
}