From a3a9af51e5ed991e75825de38a77404a0559f58e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 11 Apr 2014 21:16:07 +0700 Subject: [PATCH] Additional unit tests and some code deduplication --- src/PhpWord/Element/Section.php | 3 -- src/PhpWord/Shared/String.php | 17 +++++++ src/PhpWord/Style/AbstractStyle.php | 16 +++--- src/PhpWord/Style/Cell.php | 6 +-- src/PhpWord/Style/Font.php | 2 - src/PhpWord/Style/Paragraph.php | 7 +-- src/PhpWord/Style/Row.php | 35 +++++-------- src/PhpWord/Style/Section.php | 6 +-- src/PhpWord/Style/TOC.php | 11 ----- src/PhpWord/Style/Table.php | 12 ++--- src/PhpWord/TOC.php | 6 --- .../Tests/Style/NumberingLevelTest.php | 49 +++++++++++++++++++ 12 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 tests/PhpWord/Tests/Style/NumberingLevelTest.php diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 82060e85..9e5dbd0a 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -66,9 +66,6 @@ class Section extends AbstractElement if (is_null($value)) { continue; } - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } $this->settings->setSettingValue($key, $value); } } diff --git a/src/PhpWord/Shared/String.php b/src/PhpWord/Shared/String.php index e603f034..95f75f13 100644 --- a/src/PhpWord/Shared/String.php +++ b/src/PhpWord/Shared/String.php @@ -77,6 +77,23 @@ class String 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 */ diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index 2c88bdb2..45ba17a7 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -9,6 +9,8 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Shared\String; + /** * Abstract style class * @@ -50,7 +52,10 @@ abstract class AbstractStyle /** * 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 $value @@ -58,14 +63,7 @@ abstract class AbstractStyle */ public function setStyleValue($key, $value) { - // Backward compability check for versions < 0.10.0 which use underscore - // 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; + $method = 'set' . String::removeUnderscorePrefix($key); if (method_exists($this, $method)) { $this->$method($value); } diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 2e1f9d79..64a7f35a 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -9,6 +9,8 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Shared\String; + /** * Table cell style */ @@ -145,9 +147,7 @@ class Cell extends AbstractStyle */ public function setStyleValue($key, $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } + $key = String::removeUnderscorePrefix($key); if ($key == 'borderSize') { $this->setBorderSize($value); } elseif ($key == 'borderColor') { diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 2fef3a66..dd8ff02a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -193,8 +193,6 @@ class Font extends AbstractStyle if ($key === 'line-height') { $this->setLineHeight($value); null; - } elseif (substr($key, 0, 1) == '_') { - $key = substr($key, 1); } $this->setStyleValue($key, $value); } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 8feb7924..57ad4405 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -10,6 +10,7 @@ namespace PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Exception\InvalidStyleException; +use PhpOffice\PhpWord\Shared\String; /** * Paragraph style @@ -127,8 +128,6 @@ class Paragraph extends AbstractStyle foreach ($style as $key => $value) { if ($key === 'line-height') { null; - } elseif (substr($key, 0, 1) == '_') { - $key = substr($key, 1); } $this->setStyleValue($key, $value); } @@ -144,9 +143,7 @@ class Paragraph extends AbstractStyle */ public function setStyleValue($key, $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } + $key = String::removeUnderscorePrefix($key); if ($key == 'indent' || $key == 'hanging') { $value = $value * 720; } elseif ($key == 'spacing') { diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index d4dc642f..698adea4 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -45,16 +45,12 @@ class Row extends AbstractStyle /** * Set tblHeader * - * @param boolean $pValue - * @return $this + * @param boolean $value + * @return self */ - public function setTblHeader($pValue = false) + public function setTblHeader($value = false) { - if (!is_bool($pValue)) { - $pValue = false; - } - $this->tblHeader = $pValue; - return $this; + $this->tblHeader = $this->setBoolVal($value, $this->tblHeader); } /** @@ -70,16 +66,12 @@ class Row extends AbstractStyle /** * Set cantSplit * - * @param boolean $pValue - * @return $this + * @param boolean $value + * @return self */ - public function setCantSplit($pValue = false) + public function setCantSplit($value = false) { - if (!is_bool($pValue)) { - $pValue = false; - } - $this->cantSplit = $pValue; - return $this; + $this->cantSplit = $this->setBoolVal($value, $this->cantSplit); } /** @@ -95,15 +87,12 @@ class Row extends AbstractStyle /** * Set exactHeight * - * @param bool $pValue - * @return $this + * @param bool $value + * @return self */ - public function setExactHeight($pValue = false) + public function setExactHeight($value = false) { - if (!is_bool($pValue)) { - $pValue = false; - } - $this->exactHeight = $pValue; + $this->exactHeight = $this->setBoolVal($value, $this->exactHeight); return $this; } diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 6005e3ca..717fdaf7 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -9,6 +9,8 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Shared\String; + /** * Section settings */ @@ -217,9 +219,7 @@ class Section extends AbstractStyle */ public function setSettingValue($key, $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } + $key = String::removeUnderscorePrefix($key); if ($key == 'orientation' && $value == 'landscape') { $this->setLandscape(); } elseif ($key == 'orientation' && is_null($value)) { diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index f7a752a9..e8a781b0 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -110,15 +110,4 @@ class TOC extends AbstractStyle { $this->indent = $pValue; } - - /** - * Set style value - * - * @param string $key - * @param string $value - */ - public function setStyleValue($key, $value) - { - $this->$key = $value; - } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index f7c98c36..000ecff7 100755 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -9,6 +9,8 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Shared\String; + /** * Table style */ @@ -161,18 +163,12 @@ class Table extends AbstractStyle unset($this->firstRow->borderInsideHColor); unset($this->firstRow->borderInsideHSize); foreach ($styleFirstRow as $key => $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } $this->firstRow->setStyleValue($key, $value); } } if (!is_null($styleTable) && is_array($styleTable)) { foreach ($styleTable as $key => $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } $this->setStyleValue($key, $value); } } @@ -186,9 +182,7 @@ class Table extends AbstractStyle */ public function setStyleValue($key, $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } + $key = String::removeUnderscorePrefix($key); if ($key == 'borderSize') { $this->setBorderSize($value); } elseif ($key == 'borderColor') { diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index d84bf770..22c79634 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -82,9 +82,6 @@ class TOC if (!is_null($styleTOC) && is_array($styleTOC)) { foreach ($styleTOC as $key => $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } self::$TOCStyle->setStyleValue($key, $value); } } @@ -93,9 +90,6 @@ class TOC if (is_array($styleFont)) { self::$fontStyle = new Font(); foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } self::$fontStyle->setStyleValue($key, $value); } } else { diff --git a/tests/PhpWord/Tests/Style/NumberingLevelTest.php b/tests/PhpWord/Tests/Style/NumberingLevelTest.php new file mode 100644 index 00000000..f3e28a0e --- /dev/null +++ b/tests/PhpWord/Tests/Style/NumberingLevelTest.php @@ -0,0 +1,49 @@ + 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()); + } + } +}