Skip to content

Commit d177b47

Browse files
committed
New function PHPWord_Style_Paragraph::setTabs() and unit test for PHPWord_Style_Paragraph
1 parent 537f49e commit d177b47

File tree

3 files changed

+115
-13
lines changed

3 files changed

+115
-13
lines changed

Classes/PHPWord/Style/Paragraph.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,21 @@ public function __construct()
145145
/**
146146
* Set Style value
147147
*
148-
* @param string $key
149-
* @param mixed $value
148+
* @param string $key
149+
* @param mixed $value
150150
*/
151151
public function setStyleValue($key, $value)
152152
{
153-
if ($key == '_indent') {
154-
$value = $value * 720; // 720 twips per indent
155-
}
156-
if ($key == '_hanging') {
153+
if ($key == '_indent' || $key == '_hanging') {
157154
$value = $value * 720;
158155
}
159156
if ($key == '_spacing') {
160157
$value += 240; // because line height of 1 matches 240 twips
161158
}
162-
if ($key === '_tabs') {
163-
$value = new PHPWord_Style_Tabs($value);
159+
$method = 'set' . ucwords(substr($key, 1));
160+
if (method_exists($this, $method)) {
161+
$this->$method($value);
164162
}
165-
$this->$key = $value;
166163
}
167164

168165
/**
@@ -311,6 +308,20 @@ public function getTabs()
311308
return $this->_tabs;
312309
}
313310

311+
/*
312+
* Set tabs
313+
*
314+
* @param array $pValue
315+
* @return PHPWord_Style_Paragraph
316+
*/
317+
public function setTabs($pValue = null)
318+
{
319+
if (is_array($pValue)) {
320+
$this->_tabs = new PHPWord_Style_Tabs($pValue);
321+
}
322+
return $this;
323+
}
324+
314325
/**
315326
* Get parent style ID
316327
*
@@ -374,7 +385,7 @@ public function getWidowControl()
374385
public function setWidowControl($pValue = true)
375386
{
376387
if (!is_bool($pValue)) {
377-
$pValue = false;
388+
$pValue = true;
378389
}
379390
$this->_widowControl = $pValue;
380391
return $this;
@@ -396,7 +407,7 @@ public function getKeepNext()
396407
* @param bool $pValue
397408
* @return PHPWord_Style_Paragraph
398409
*/
399-
public function setKeepNext($pValue = true)
410+
public function setKeepNext($pValue = false)
400411
{
401412
if (!is_bool($pValue)) {
402413
$pValue = false;
@@ -421,7 +432,7 @@ public function getKeepLines()
421432
* @param bool $pValue
422433
* @return PHPWord_Style_Paragraph
423434
*/
424-
public function setKeepLines($pValue = true)
435+
public function setKeepLines($pValue = false)
425436
{
426437
if (!is_bool($pValue)) {
427438
$pValue = false;
@@ -446,7 +457,7 @@ public function getPageBreakBefore()
446457
* @param bool $pValue
447458
* @return PHPWord_Style_Paragraph
448459
*/
449-
public function setPageBreakBefore($pValue = true)
460+
public function setPageBreakBefore($pValue = false)
450461
{
451462
if (!is_bool($pValue)) {
452463
$pValue = false;

Tests/PHPWord/Style/ParagraphTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Style_Paragraph;
6+
use PHPWord_Style_Tab;
7+
8+
/**
9+
* Class PHPWord_Style_ParagraphTest
10+
*
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
/**
18+
* Test setting style values with null or empty value
19+
*/
20+
public function testSetStyleValueWithNullOrEmpty()
21+
{
22+
$object = new PHPWord_Style_Paragraph();
23+
24+
$attributes = array(
25+
'tabs' => null,
26+
'widowControl' => true,
27+
'keepNext' => false,
28+
'keepLines' => false,
29+
'pageBreakBefore' => false,
30+
);
31+
foreach ($attributes as $key => $default) {
32+
$method = 'get' . ucwords($key);
33+
$object->setStyleValue("_$key", null);
34+
$this->assertEquals($default, $object->$method());
35+
$object->setStyleValue("_$key", '');
36+
$this->assertEquals($default, $object->$method());
37+
}
38+
}
39+
40+
/**
41+
* Test setting style values with normal value
42+
*/
43+
public function testSetStyleValueNormal()
44+
{
45+
$object = new PHPWord_Style_Paragraph();
46+
47+
$attributes = array(
48+
'align' => 'justify',
49+
'spaceAfter' => 240,
50+
'spaceBefore' => 240,
51+
'indent' => 1,
52+
'hanging' => 1,
53+
'spacing' => 120,
54+
'basedOn' => 'Normal',
55+
'next' => 'Normal',
56+
'widowControl' => false,
57+
'keepNext' => true,
58+
'keepLines' => true,
59+
'pageBreakBefore' => true,
60+
);
61+
foreach ($attributes as $key => $value) {
62+
$method = 'get' . ucwords($key);
63+
$object->setStyleValue("_$key", $value);
64+
if ($key == 'align') {
65+
if ($value == 'justify') {
66+
$value = 'both';
67+
}
68+
} elseif ($key == 'indent' || $key == 'hanging') {
69+
$value = $value * 720;
70+
} elseif ($key == 'spacing') {
71+
$value += 240;
72+
}
73+
$this->assertEquals($value, $object->$method());
74+
}
75+
}
76+
77+
/**
78+
* Test tabs
79+
*/
80+
public function testTabs()
81+
{
82+
$object = new PHPWord_Style_Paragraph();
83+
$object->setTabs(array(
84+
new PHPWord_Style_Tab('left', 1550),
85+
new PHPWord_Style_Tab('right', 5300),
86+
));
87+
$this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs());
88+
}
89+
90+
}

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Changes in branch for release 0.7.1 :
4848
- General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring
4949
- General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing.
5050
- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
51+
- Feature: (ivanlanin) - Paragraph: setTabs() function
5152
- QA: (Progi1984) - UnitTests
5253

5354
Changes in branch for release 0.7.0 :

0 commit comments

Comments
 (0)