Skip to content

Commit a379577

Browse files
authored
Merge pull request #2364 from hazington/patch-5
Add background color support for text box element for Word writer
2 parents b453cf0 + 53d34fd commit a379577

File tree

5 files changed

+84
-66
lines changed

5 files changed

+84
-66
lines changed

src/PhpWord/Style/TextBox.php

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/**
33
* This file is part of PHPWord - A pure PHP library for reading and writing
44
* word processing documents.
5-
*
65
* PHPWord is free software distributed under the terms of the GNU Lesser
76
* General Public License version 3 as published by the Free Software Foundation.
8-
*
97
* For the full copyright and license information, please read the LICENSE
108
* file that was distributed with this source code. For the full list of
119
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@@ -27,131 +25,138 @@ class TextBox extends Image
2725
/**
2826
* margin top.
2927
*
30-
* @var int
28+
* @var null|int
3129
*/
3230
private $innerMarginTop;
3331

3432
/**
3533
* margin left.
3634
*
37-
* @var int
35+
* @var null|int
3836
*/
3937
private $innerMarginLeft;
4038

4139
/**
4240
* margin right.
4341
*
44-
* @var int
42+
* @var null|int
4543
*/
4644
private $innerMarginRight;
4745

4846
/**
4947
* Cell margin bottom.
5048
*
51-
* @var int
49+
* @var null|int
5250
*/
5351
private $innerMarginBottom;
5452

5553
/**
5654
* border size.
5755
*
58-
* @var int
56+
* @var null|int
5957
*/
6058
private $borderSize;
6159

6260
/**
6361
* border color.
6462
*
65-
* @var string
63+
* @var null|string
6664
*/
6765
private $borderColor;
6866

6967
/**
70-
* Set margin top.
68+
* background color.
7169
*
72-
* @param int $value
70+
* @var null|string
71+
*/
72+
private $bgColor;
73+
74+
/**
75+
* Set background color.
76+
*/
77+
public function setBgColor(?string $value = null): void
78+
{
79+
$this->bgColor = $value;
80+
}
81+
82+
/**
83+
* Get background color.
84+
*/
85+
public function getBgColor(): ?string
86+
{
87+
return $this->bgColor;
88+
}
89+
90+
/**
91+
* Set margin top.
7392
*/
74-
public function setInnerMarginTop($value = null): void
93+
public function setInnerMarginTop(?int $value = null): void
7594
{
7695
$this->innerMarginTop = $value;
7796
}
7897

7998
/**
8099
* Get margin top.
81-
*
82-
* @return int
83100
*/
84-
public function getInnerMarginTop()
101+
public function getInnerMarginTop(): ?int
85102
{
86103
return $this->innerMarginTop;
87104
}
88105

89106
/**
90107
* Set margin left.
91-
*
92-
* @param int $value
93108
*/
94-
public function setInnerMarginLeft($value = null): void
109+
public function setInnerMarginLeft(?int $value = null): void
95110
{
96111
$this->innerMarginLeft = $value;
97112
}
98113

99114
/**
100115
* Get margin left.
101-
*
102-
* @return int
103116
*/
104-
public function getInnerMarginLeft()
117+
public function getInnerMarginLeft(): ?int
105118
{
106119
return $this->innerMarginLeft;
107120
}
108121

109122
/**
110123
* Set margin right.
111-
*
112-
* @param int $value
113124
*/
114-
public function setInnerMarginRight($value = null): void
125+
public function setInnerMarginRight(?int $value = null): void
115126
{
116127
$this->innerMarginRight = $value;
117128
}
118129

119130
/**
120131
* Get margin right.
121-
*
122-
* @return int
123132
*/
124-
public function getInnerMarginRight()
133+
public function getInnerMarginRight(): ?int
125134
{
126135
return $this->innerMarginRight;
127136
}
128137

129138
/**
130139
* Set margin bottom.
131-
*
132-
* @param int $value
133140
*/
134-
public function setInnerMarginBottom($value = null): void
141+
public function setInnerMarginBottom(?int $value = null): void
135142
{
136143
$this->innerMarginBottom = $value;
137144
}
138145

139146
/**
140147
* Get margin bottom.
141-
*
142-
* @return int
143148
*/
144-
public function getInnerMarginBottom()
149+
public function getInnerMarginBottom(): ?int
145150
{
146151
return $this->innerMarginBottom;
147152
}
148153

149154
/**
150155
* Set TLRB cell margin.
151156
*
152-
* @param int $value Margin in twips
157+
* @param null|int $value Margin in twips
153158
*/
154-
public function setInnerMargin($value = null): void
159+
public function setInnerMargin(?int $value = null): void
155160
{
156161
$this->setInnerMarginTop($value);
157162
$this->setInnerMarginLeft($value);
@@ -164,17 +169,15 @@ public function setInnerMargin($value = null): void
164169
*
165170
* @return int[]
166171
*/
167-
public function getInnerMargin()
172+
public function getInnerMargin(): array
168173
{
169174
return [$this->innerMarginLeft, $this->innerMarginTop, $this->innerMarginRight, $this->innerMarginBottom];
170175
}
171176

172177
/**
173178
* Has inner margin?
174-
*
175-
* @return bool
176179
*/
177-
public function hasInnerMargins()
180+
public function hasInnerMargins(): bool
178181
{
179182
$hasInnerMargins = false;
180183
$margins = $this->getInnerMargin();
@@ -191,39 +194,33 @@ public function hasInnerMargins()
191194
/**
192195
* Set border size.
193196
*
194-
* @param int $value Size in points
197+
* @param null|int $value Size in points
195198
*/
196-
public function setBorderSize($value = null): void
199+
public function setBorderSize(?int $value = null): void
197200
{
198201
$this->borderSize = $value;
199202
}
200203

201204
/**
202205
* Get border size.
203-
*
204-
* @return int
205206
*/
206-
public function getBorderSize()
207+
public function getBorderSize(): ?int
207208
{
208209
return $this->borderSize;
209210
}
210211

211212
/**
212213
* Set border color.
213-
*
214-
* @param string $value
215214
*/
216-
public function setBorderColor($value = null): void
215+
public function setBorderColor(?string $value = null): void
217216
{
218217
$this->borderColor = $value;
219218
}
220219

221220
/**
222221
* Get border color.
223-
*
224-
* @return string
225222
*/
226-
public function getBorderColor()
223+
public function getBorderColor(): ?string
227224
{
228225
return $this->borderColor;
229226
}

src/PhpWord/Writer/Word2007/Element/TextBox.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/**
33
* This file is part of PHPWord - A pure PHP library for reading and writing
44
* word processing documents.
5-
*
65
* PHPWord is free software distributed under the terms of the GNU Lesser
76
* General Public License version 3 as published by the Free Software Foundation.
8-
*
97
* For the full copyright and license information, please read the LICENSE
108
* file that was distributed with this source code. For the full list of
119
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@@ -50,6 +48,10 @@ public function write(): void
5048
$xmlWriter->startElement('v:shape');
5149
$xmlWriter->writeAttribute('type', '#_x0000_t0202');
5250

51+
if ($style->getBgColor()) {
52+
$xmlWriter->writeAttribute('fillcolor', $style->getBgColor());
53+
}
54+
5355
$styleWriter->write();
5456
$styleWriter->writeBorder();
5557

src/PhpWord/Writer/Word2007/Style/TextBox.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/**
33
* This file is part of PHPWord - A pure PHP library for reading and writing
44
* word processing documents.
5-
*
65
* PHPWord is free software distributed under the terms of the GNU Lesser
76
* General Public License version 3 as published by the Free Software Foundation.
8-
*
97
* For the full copyright and license information, please read the LICENSE
108
* file that was distributed with this source code. For the full list of
119
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.

tests/PhpWordTests/Style/TextBoxTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/**
33
* This file is part of PHPWord - A pure PHP library for reading and writing
44
* word processing documents.
5-
*
65
* PHPWord is free software distributed under the terms of the GNU Lesser
76
* General Public License version 3 as published by the Free Software Foundation.
8-
*
97
* For the full copyright and license information, please read the LICENSE
108
* file that was distributed with this source code. For the full list of
119
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@@ -20,15 +18,15 @@
2018
use InvalidArgumentException;
2119
use PhpOffice\PhpWord\SimpleType\Jc;
2220
use PhpOffice\PhpWord\Style\TextBox;
21+
use PHPUnit\Framework\TestCase;
2322

2423
/**
2524
* Test class for PhpOffice\PhpWord\Style\Image.
2625
*
2726
* @coversDefaultClass \PhpOffice\PhpWord\Style\Image
28-
*
2927
* @runTestsInSeparateProcesses
3028
*/
31-
class TextBoxTest extends \PHPUnit\Framework\TestCase
29+
class TextBoxTest extends TestCase
3230
{
3331
/**
3432
* Test setting style with normal value.
@@ -55,6 +53,7 @@ public function testSetGetNormal(): void
5553
'innerMarginLeft' => '5',
5654
'borderSize' => '2',
5755
'borderColor' => 'red',
56+
'bgColor' => 'blue',
5857
];
5958
foreach ($properties as $key => $value) {
6059
$set = "set{$key}";
@@ -89,6 +88,7 @@ public function testSetStyleValue(): void
8988
'innerMarginLeft' => '5',
9089
'borderSize' => '2',
9190
'borderColor' => 'red',
91+
'bgColor' => 'blue',
9292
];
9393
foreach ($properties as $key => $value) {
9494
$get = "get{$key}";
@@ -305,4 +305,15 @@ public function testSetGetBorderColor(): void
305305
$object->setBorderColor($expected);
306306
self::assertEquals($expected, $object->getBorderColor());
307307
}
308+
309+
/**
310+
* Test set/get bgColor.
311+
*/
312+
public function testSetGetBgColor(): void
313+
{
314+
$expected = 'blue';
315+
$object = new TextBox();
316+
$object->setBgColor($expected);
317+
self::assertEquals($expected, $object->getBgColor());
318+
}
308319
}

tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/**
33
* This file is part of PHPWord - A pure PHP library for reading and writing
44
* word processing documents.
5-
*
65
* PHPWord is free software distributed under the terms of the GNU Lesser
76
* General Public License version 3 as published by the Free Software Foundation.
8-
*
97
* For the full copyright and license information, please read the LICENSE
108
* file that was distributed with this source code. For the full list of
119
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@@ -61,11 +59,11 @@ public function testWriteCustomProps(): void
6159
$doc = TestHelperDOCX::getDocument($phpWord);
6260
self::assertNotNull($doc);
6361

64-
// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr'));
65-
// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));
66-
// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4'));
67-
// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8'));
68-
// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr'));
62+
// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr'));
63+
// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));
64+
// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4'));
65+
// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8'));
66+
// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr'));
6967
}
7068

7169
/**
@@ -408,7 +406,13 @@ public function testWriteImage(): void
408406
// behind
409407
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
410408
$style = $element->getAttribute('style');
411-
self::assertRegExp('/z\-index:\-[0-9]*/', $style);
409+
410+
// Try to address CI coverage issue for PHP 7.1 and 7.2 when using regex match assertions
411+
if (method_exists(static::class, 'assertRegExp')) {
412+
self::assertRegExp('/z\-index:\-[0-9]*/', $style);
413+
} else {
414+
self::assertMatchesRegularExpression('/z\-index:\-[0-9]*/', $style);
415+
}
412416

413417
// square
414418
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap');
@@ -551,7 +555,13 @@ public function testWriteDefaultColor(): void
551555
$cell->addText('Test');
552556

553557
$doc = TestHelperDOCX::getDocument($phpWord);
554-
self::assertEquals(Cell::DEFAULT_BORDER_COLOR, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcBorders/w:top', 'w:color'));
558+
self::assertEquals(
559+
Cell::DEFAULT_BORDER_COLOR,
560+
$doc->getElementAttribute(
561+
'/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcBorders/w:top',
562+
'w:color'
563+
)
564+
);
555565
}
556566

557567
/**

0 commit comments

Comments
 (0)