Skip to content

Commit f0ec497

Browse files
authored
fix: Allow vAlign and vMerge on Style\Cell to be set to null (#2676)
* fix: Allow vAlign and vMerge on Style\Cell to be set to null (#2673) vAlign and vMerge are initialized as `null` in every style until it is explicitly set. Right now, it is not possible to unset them, after it has been set once. I've added a null-check to skip the validation, based on the default parameter value of `null`, which indicates to me that it once was intended to work like this. I've also fixed the type-hints, which were wrong from the start. * docs(changelog): Add note about supporting vAlign and vMerge to be unset * test: Extend existing table test case to check if unsetting vAlign works * test: Add test case to check if unsetting vMerge works This should fix the reduction in test coverage because of the new null checking code in the Setter
1 parent fa0cea0 commit f0ec497

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Bug fixes
1111

1212
- Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668)
13+
- Allow vAlign and vMerge on Style\Cell to be set to null by [@SpraxDev](https://github.com/SpraxDev) fixing [#2673](https://github.com/PHPOffice/PHPWord/issues/2673) in [#2676](https://github.com/PHPOffice/PHPWord/pull/2676)
1314

1415
### Miscellaneous
1516

src/PhpWord/Style/Cell.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Cell extends Border
6969
/**
7070
* Vertical align (top, center, both, bottom).
7171
*
72-
* @var string
72+
* @var null|string
7373
*/
7474
private $vAlign;
7575

@@ -93,7 +93,7 @@ class Cell extends Border
9393
* - restart: Start/restart merged region
9494
* - continue: Continue merged region
9595
*
96-
* @var string
96+
* @var null|string
9797
*/
9898
private $vMerge;
9999

@@ -128,7 +128,7 @@ class Cell extends Border
128128
/**
129129
* Get vertical align.
130130
*
131-
* @return string
131+
* @return null|string
132132
*/
133133
public function getVAlign()
134134
{
@@ -138,12 +138,18 @@ public function getVAlign()
138138
/**
139139
* Set vertical align.
140140
*
141-
* @param string $value
141+
* @param null|string $value
142142
*
143143
* @return self
144144
*/
145145
public function setVAlign($value = null)
146146
{
147+
if ($value === null) {
148+
$this->vAlign = null;
149+
150+
return $this;
151+
}
152+
147153
VerticalJc::validate($value);
148154
$this->vAlign = $this->setEnumVal($value, VerticalJc::values(), $this->vAlign);
149155

@@ -235,7 +241,7 @@ public function setGridSpan($value = null)
235241
/**
236242
* Get vertical merge (rowspan).
237243
*
238-
* @return string
244+
* @return null|string
239245
*/
240246
public function getVMerge()
241247
{
@@ -245,12 +251,18 @@ public function getVMerge()
245251
/**
246252
* Set vertical merge (rowspan).
247253
*
248-
* @param string $value
254+
* @param null|string $value
249255
*
250256
* @return self
251257
*/
252258
public function setVMerge($value = null)
253259
{
260+
if ($value === null) {
261+
$this->vMerge = null;
262+
263+
return $this;
264+
}
265+
254266
$enum = [self::VMERGE_RESTART, self::VMERGE_CONTINUE];
255267
$this->vMerge = $this->setEnumVal($value, $enum, $this->vMerge);
256268

tests/PhpWordTests/Writer/HTML/Element/TableTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use DOMXPath;
2121
use PhpOffice\PhpWord\PhpWord;
2222
use PhpOffice\PhpWord\SimpleType\VerticalJc;
23+
use PhpOffice\PhpWord\Style;
2324
use PhpOffice\PhpWordTests\Writer\HTML\Helper;
2425
use PHPUnit\Framework\TestCase;
2526

@@ -179,12 +180,55 @@ public function testWriteTableCellVAlign(): void
179180
$cell->addText('bottom text');
180181
$cell->getStyle()->setVAlign(VerticalJc::BOTTOM);
181182

183+
$cell = $row->addCell();
184+
$cell->addText('no vAlign');
185+
$cell->getStyle()->setVAlign(VerticalJc::BOTTOM);
186+
$cell->getStyle()->setVAlign();
187+
182188
$dom = Helper::getAsHTML($phpWord);
183189
$xpath = new DOMXPath($dom);
184190

185191
$cell1Style = Helper::getTextContent($xpath, '//table/tr/td[1]', 'style');
186192
$cell2Style = Helper::getTextContent($xpath, '//table/tr/td[2]', 'style');
187193
self::assertSame('vertical-align: top;', $cell1Style);
188194
self::assertSame('vertical-align: bottom;', $cell2Style);
195+
196+
$cell3Query = $xpath->query('//table/tr/td[3]');
197+
self::assertNotFalse($cell3Query);
198+
self::assertCount(1, $cell3Query);
199+
200+
$cell3Style = $cell3Query->item(0)->attributes->getNamedItem('style');
201+
self::assertNull($cell3Style);
202+
}
203+
204+
public function testWriteTableCellVMerge(): void
205+
{
206+
$phpWord = new PhpWord();
207+
$section = $phpWord->addSection();
208+
209+
$table = $section->addTable();
210+
211+
$cell = $table->addRow()->addCell();
212+
$cell->addText('text');
213+
$cell->getStyle()->setVMerge(Style\Cell::VMERGE_RESTART);
214+
215+
$cell = $table->addRow()->addCell();
216+
$cell->getStyle()->setVMerge(Style\Cell::VMERGE_CONTINUE);
217+
218+
$cell = $table->addRow()->addCell();
219+
$cell->addText('no vMerge');
220+
$cell->getStyle()->setVMerge(Style\Cell::VMERGE_CONTINUE);
221+
$cell->getStyle()->setVMerge();
222+
223+
$dom = Helper::getAsHTML($phpWord);
224+
$xpath = new DOMXPath($dom);
225+
226+
$cell1Style = Helper::getTextContent($xpath, '//table/tr[1]/td[1]', 'rowspan');
227+
self::assertSame('2', $cell1Style);
228+
229+
$cell3Query = $xpath->query('//table/tr[3]/td[1]');
230+
self::assertNotFalse($cell3Query);
231+
self::assertCount(1, $cell3Query);
232+
self::assertNull($cell3Query->item(0)->attributes->getNamedItem('rowspan'));
189233
}
190234
}

0 commit comments

Comments
 (0)