Skip to content

Commit a3a9af5

Browse files
committed
Additional unit tests and some code deduplication
1 parent ae652a6 commit a3a9af5

File tree

12 files changed

+96
-74
lines changed

12 files changed

+96
-74
lines changed

src/PhpWord/Element/Section.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ public function setSettings($settings = null)
6666
if (is_null($value)) {
6767
continue;
6868
}
69-
if (substr($key, 0, 1) == '_') {
70-
$key = substr($key, 1);
71-
}
7269
$this->settings->setSettingValue($key, $value);
7370
}
7471
}

src/PhpWord/Shared/String.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ public static function toUTF8($value = '')
7777
return $value;
7878
}
7979

80+
/**
81+
* Return name without underscore for < 0.10.0 variable name compatibility
82+
*
83+
* @param string $value
84+
* @return string
85+
*/
86+
public static function removeUnderscorePrefix($value)
87+
{
88+
if (!is_null($value)) {
89+
if (substr($value, 0, 1) == '_') {
90+
$value = substr($value, 1);
91+
}
92+
}
93+
94+
return $value;
95+
}
96+
8097
/**
8198
* Build control characters array
8299
*/

src/PhpWord/Style/AbstractStyle.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace PhpOffice\PhpWord\Style;
1111

12+
use PhpOffice\PhpWord\Shared\String;
13+
1214
/**
1315
* Abstract style class
1416
*
@@ -50,22 +52,18 @@ public function setIndex($value = null)
5052
/**
5153
* Set style value template method
5254
*
53-
* Some child classes have their own specific overrides
55+
* Some child classes have their own specific overrides.
56+
* Backward compability check for versions < 0.10.0 which use underscore
57+
* prefix for their private properties.
58+
* Check if the set method is exists. Throws an exception?
5459
*
5560
* @param string $key
5661
* @param string $value
5762
* @return self
5863
*/
5964
public function setStyleValue($key, $value)
6065
{
61-
// Backward compability check for versions < 0.10.0 which use underscore
62-
// prefix for their private properties
63-
if (substr($key, 0, 1) == '_') {
64-
$key = substr($key, 1);
65-
}
66-
67-
// Check if the set method is exists. Throws an exception?
68-
$method = 'set' . $key;
66+
$method = 'set' . String::removeUnderscorePrefix($key);
6967
if (method_exists($this, $method)) {
7068
$this->$method($value);
7169
}

src/PhpWord/Style/Cell.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace PhpOffice\PhpWord\Style;
1111

12+
use PhpOffice\PhpWord\Shared\String;
13+
1214
/**
1315
* Table cell style
1416
*/
@@ -145,9 +147,7 @@ public function __construct()
145147
*/
146148
public function setStyleValue($key, $value)
147149
{
148-
if (substr($key, 0, 1) == '_') {
149-
$key = substr($key, 1);
150-
}
150+
$key = String::removeUnderscorePrefix($key);
151151
if ($key == 'borderSize') {
152152
$this->setBorderSize($value);
153153
} elseif ($key == 'borderColor') {

src/PhpWord/Style/Font.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ public function setArrayStyle(array $style = array())
193193
if ($key === 'line-height') {
194194
$this->setLineHeight($value);
195195
null;
196-
} elseif (substr($key, 0, 1) == '_') {
197-
$key = substr($key, 1);
198196
}
199197
$this->setStyleValue($key, $value);
200198
}

src/PhpWord/Style/Paragraph.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PhpOffice\PhpWord\Style;
1111

1212
use PhpOffice\PhpWord\Exception\InvalidStyleException;
13+
use PhpOffice\PhpWord\Shared\String;
1314

1415
/**
1516
* Paragraph style
@@ -127,8 +128,6 @@ public function setArrayStyle(array $style = array())
127128
foreach ($style as $key => $value) {
128129
if ($key === 'line-height') {
129130
null;
130-
} elseif (substr($key, 0, 1) == '_') {
131-
$key = substr($key, 1);
132131
}
133132
$this->setStyleValue($key, $value);
134133
}
@@ -144,9 +143,7 @@ public function setArrayStyle(array $style = array())
144143
*/
145144
public function setStyleValue($key, $value)
146145
{
147-
if (substr($key, 0, 1) == '_') {
148-
$key = substr($key, 1);
149-
}
146+
$key = String::removeUnderscorePrefix($key);
150147
if ($key == 'indent' || $key == 'hanging') {
151148
$value = $value * 720;
152149
} elseif ($key == 'spacing') {

src/PhpWord/Style/Row.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@ public function __construct()
4545
/**
4646
* Set tblHeader
4747
*
48-
* @param boolean $pValue
49-
* @return $this
48+
* @param boolean $value
49+
* @return self
5050
*/
51-
public function setTblHeader($pValue = false)
51+
public function setTblHeader($value = false)
5252
{
53-
if (!is_bool($pValue)) {
54-
$pValue = false;
55-
}
56-
$this->tblHeader = $pValue;
57-
return $this;
53+
$this->tblHeader = $this->setBoolVal($value, $this->tblHeader);
5854
}
5955

6056
/**
@@ -70,16 +66,12 @@ public function getTblHeader()
7066
/**
7167
* Set cantSplit
7268
*
73-
* @param boolean $pValue
74-
* @return $this
69+
* @param boolean $value
70+
* @return self
7571
*/
76-
public function setCantSplit($pValue = false)
72+
public function setCantSplit($value = false)
7773
{
78-
if (!is_bool($pValue)) {
79-
$pValue = false;
80-
}
81-
$this->cantSplit = $pValue;
82-
return $this;
74+
$this->cantSplit = $this->setBoolVal($value, $this->cantSplit);
8375
}
8476

8577
/**
@@ -95,15 +87,12 @@ public function getCantSplit()
9587
/**
9688
* Set exactHeight
9789
*
98-
* @param bool $pValue
99-
* @return $this
90+
* @param bool $value
91+
* @return self
10092
*/
101-
public function setExactHeight($pValue = false)
93+
public function setExactHeight($value = false)
10294
{
103-
if (!is_bool($pValue)) {
104-
$pValue = false;
105-
}
106-
$this->exactHeight = $pValue;
95+
$this->exactHeight = $this->setBoolVal($value, $this->exactHeight);
10796
return $this;
10897
}
10998

src/PhpWord/Style/Section.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace PhpOffice\PhpWord\Style;
1111

12+
use PhpOffice\PhpWord\Shared\String;
13+
1214
/**
1315
* Section settings
1416
*/
@@ -217,9 +219,7 @@ public function __construct()
217219
*/
218220
public function setSettingValue($key, $value)
219221
{
220-
if (substr($key, 0, 1) == '_') {
221-
$key = substr($key, 1);
222-
}
222+
$key = String::removeUnderscorePrefix($key);
223223
if ($key == 'orientation' && $value == 'landscape') {
224224
$this->setLandscape();
225225
} elseif ($key == 'orientation' && is_null($value)) {

src/PhpWord/Style/TOC.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,4 @@ public function setIndent($pValue)
110110
{
111111
$this->indent = $pValue;
112112
}
113-
114-
/**
115-
* Set style value
116-
*
117-
* @param string $key
118-
* @param string $value
119-
*/
120-
public function setStyleValue($key, $value)
121-
{
122-
$this->$key = $value;
123-
}
124113
}

src/PhpWord/Style/Table.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace PhpOffice\PhpWord\Style;
1111

12+
use PhpOffice\PhpWord\Shared\String;
13+
1214
/**
1315
* Table style
1416
*/
@@ -161,18 +163,12 @@ public function __construct($styleTable = null, $styleFirstRow = null)
161163
unset($this->firstRow->borderInsideHColor);
162164
unset($this->firstRow->borderInsideHSize);
163165
foreach ($styleFirstRow as $key => $value) {
164-
if (substr($key, 0, 1) == '_') {
165-
$key = substr($key, 1);
166-
}
167166
$this->firstRow->setStyleValue($key, $value);
168167
}
169168
}
170169

171170
if (!is_null($styleTable) && is_array($styleTable)) {
172171
foreach ($styleTable as $key => $value) {
173-
if (substr($key, 0, 1) == '_') {
174-
$key = substr($key, 1);
175-
}
176172
$this->setStyleValue($key, $value);
177173
}
178174
}
@@ -186,9 +182,7 @@ public function __construct($styleTable = null, $styleFirstRow = null)
186182
*/
187183
public function setStyleValue($key, $value)
188184
{
189-
if (substr($key, 0, 1) == '_') {
190-
$key = substr($key, 1);
191-
}
185+
$key = String::removeUnderscorePrefix($key);
192186
if ($key == 'borderSize') {
193187
$this->setBorderSize($value);
194188
} elseif ($key == 'borderColor') {

0 commit comments

Comments
 (0)