From d177b4744b6668c7cc7f349bb389d3510b8dadce Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 17:47:04 +0700 Subject: [PATCH] New function PHPWord_Style_Paragraph::setTabs() and unit test for PHPWord_Style_Paragraph --- Classes/PHPWord/Style/Paragraph.php | 37 +++++++---- Tests/PHPWord/Style/ParagraphTest.php | 90 +++++++++++++++++++++++++++ changelog.txt | 1 + 3 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 Tests/PHPWord/Style/ParagraphTest.php diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 70732f86..5942ec34 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -145,24 +145,21 @@ class PHPWord_Style_Paragraph /** * Set Style value * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value */ public function setStyleValue($key, $value) { - if ($key == '_indent') { - $value = $value * 720; // 720 twips per indent - } - if ($key == '_hanging') { + if ($key == '_indent' || $key == '_hanging') { $value = $value * 720; } if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips } - if ($key === '_tabs') { - $value = new PHPWord_Style_Tabs($value); + $method = 'set' . ucwords(substr($key, 1)); + if (method_exists($this, $method)) { + $this->$method($value); } - $this->$key = $value; } /** @@ -311,6 +308,20 @@ class PHPWord_Style_Paragraph return $this->_tabs; } + /* + * Set tabs + * + * @param array $pValue + * @return PHPWord_Style_Paragraph + */ + public function setTabs($pValue = null) + { + if (is_array($pValue)) { + $this->_tabs = new PHPWord_Style_Tabs($pValue); + } + return $this; + } + /** * Get parent style ID * @@ -374,7 +385,7 @@ class PHPWord_Style_Paragraph public function setWidowControl($pValue = true) { if (!is_bool($pValue)) { - $pValue = false; + $pValue = true; } $this->_widowControl = $pValue; return $this; @@ -396,7 +407,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setKeepNext($pValue = true) + public function setKeepNext($pValue = false) { if (!is_bool($pValue)) { $pValue = false; @@ -421,7 +432,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setKeepLines($pValue = true) + public function setKeepLines($pValue = false) { if (!is_bool($pValue)) { $pValue = false; @@ -446,7 +457,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setPageBreakBefore($pValue = true) + public function setPageBreakBefore($pValue = false) { if (!is_bool($pValue)) { $pValue = false; diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php new file mode 100644 index 00000000..c0850cde --- /dev/null +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -0,0 +1,90 @@ + null, + 'widowControl' => true, + 'keepNext' => false, + 'keepLines' => false, + 'pageBreakBefore' => false, + ); + foreach ($attributes as $key => $default) { + $method = 'get' . ucwords($key); + $object->setStyleValue("_$key", null); + $this->assertEquals($default, $object->$method()); + $object->setStyleValue("_$key", ''); + $this->assertEquals($default, $object->$method()); + } + } + + /** + * Test setting style values with normal value + */ + public function testSetStyleValueNormal() + { + $object = new PHPWord_Style_Paragraph(); + + $attributes = array( + 'align' => 'justify', + 'spaceAfter' => 240, + 'spaceBefore' => 240, + 'indent' => 1, + 'hanging' => 1, + 'spacing' => 120, + 'basedOn' => 'Normal', + 'next' => 'Normal', + 'widowControl' => false, + 'keepNext' => true, + 'keepLines' => true, + 'pageBreakBefore' => true, + ); + foreach ($attributes as $key => $value) { + $method = 'get' . ucwords($key); + $object->setStyleValue("_$key", $value); + if ($key == 'align') { + if ($value == 'justify') { + $value = 'both'; + } + } elseif ($key == 'indent' || $key == 'hanging') { + $value = $value * 720; + } elseif ($key == 'spacing') { + $value += 240; + } + $this->assertEquals($value, $object->$method()); + } + } + + /** + * Test tabs + */ + public function testTabs() + { + $object = new PHPWord_Style_Paragraph(); + $object->setTabs(array( + new PHPWord_Style_Tab('left', 1550), + new PHPWord_Style_Tab('right', 5300), + )); + $this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs()); + } + +} diff --git a/changelog.txt b/changelog.txt index 09ffc81f..a6a63214 100755 --- a/changelog.txt +++ b/changelog.txt @@ -48,6 +48,7 @@ Changes in branch for release 0.7.1 : - General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring - General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing. - Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion +- Feature: (ivanlanin) - Paragraph: setTabs() function - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 :