Merge pull request #2364 from hazington/patch-5

Add background color support for text box element for Word writer
This commit is contained in:
Progi1984 2023-01-04 09:23:26 +01:00 committed by GitHub
commit a379577ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 66 deletions

View File

@ -2,10 +2,8 @@
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -27,121 +25,128 @@ class TextBox extends Image
/**
* margin top.
*
* @var int
* @var null|int
*/
private $innerMarginTop;
/**
* margin left.
*
* @var int
* @var null|int
*/
private $innerMarginLeft;
/**
* margin right.
*
* @var int
* @var null|int
*/
private $innerMarginRight;
/**
* Cell margin bottom.
*
* @var int
* @var null|int
*/
private $innerMarginBottom;
/**
* border size.
*
* @var int
* @var null|int
*/
private $borderSize;
/**
* border color.
*
* @var string
* @var null|string
*/
private $borderColor;
/**
* Set margin top.
* background color.
*
* @param int $value
* @var null|string
*/
public function setInnerMarginTop($value = null): void
private $bgColor;
/**
* Set background color.
*/
public function setBgColor(?string $value = null): void
{
$this->bgColor = $value;
}
/**
* Get background color.
*/
public function getBgColor(): ?string
{
return $this->bgColor;
}
/**
* Set margin top.
*/
public function setInnerMarginTop(?int $value = null): void
{
$this->innerMarginTop = $value;
}
/**
* Get margin top.
*
* @return int
*/
public function getInnerMarginTop()
public function getInnerMarginTop(): ?int
{
return $this->innerMarginTop;
}
/**
* Set margin left.
*
* @param int $value
*/
public function setInnerMarginLeft($value = null): void
public function setInnerMarginLeft(?int $value = null): void
{
$this->innerMarginLeft = $value;
}
/**
* Get margin left.
*
* @return int
*/
public function getInnerMarginLeft()
public function getInnerMarginLeft(): ?int
{
return $this->innerMarginLeft;
}
/**
* Set margin right.
*
* @param int $value
*/
public function setInnerMarginRight($value = null): void
public function setInnerMarginRight(?int $value = null): void
{
$this->innerMarginRight = $value;
}
/**
* Get margin right.
*
* @return int
*/
public function getInnerMarginRight()
public function getInnerMarginRight(): ?int
{
return $this->innerMarginRight;
}
/**
* Set margin bottom.
*
* @param int $value
*/
public function setInnerMarginBottom($value = null): void
public function setInnerMarginBottom(?int $value = null): void
{
$this->innerMarginBottom = $value;
}
/**
* Get margin bottom.
*
* @return int
*/
public function getInnerMarginBottom()
public function getInnerMarginBottom(): ?int
{
return $this->innerMarginBottom;
}
@ -149,9 +154,9 @@ class TextBox extends Image
/**
* Set TLRB cell margin.
*
* @param int $value Margin in twips
* @param null|int $value Margin in twips
*/
public function setInnerMargin($value = null): void
public function setInnerMargin(?int $value = null): void
{
$this->setInnerMarginTop($value);
$this->setInnerMarginLeft($value);
@ -164,17 +169,15 @@ class TextBox extends Image
*
* @return int[]
*/
public function getInnerMargin()
public function getInnerMargin(): array
{
return [$this->innerMarginLeft, $this->innerMarginTop, $this->innerMarginRight, $this->innerMarginBottom];
}
/**
* Has inner margin?
*
* @return bool
*/
public function hasInnerMargins()
public function hasInnerMargins(): bool
{
$hasInnerMargins = false;
$margins = $this->getInnerMargin();
@ -191,39 +194,33 @@ class TextBox extends Image
/**
* Set border size.
*
* @param int $value Size in points
* @param null|int $value Size in points
*/
public function setBorderSize($value = null): void
public function setBorderSize(?int $value = null): void
{
$this->borderSize = $value;
}
/**
* Get border size.
*
* @return int
*/
public function getBorderSize()
public function getBorderSize(): ?int
{
return $this->borderSize;
}
/**
* Set border color.
*
* @param string $value
*/
public function setBorderColor($value = null): void
public function setBorderColor(?string $value = null): void
{
$this->borderColor = $value;
}
/**
* Get border color.
*
* @return string
*/
public function getBorderColor()
public function getBorderColor(): ?string
{
return $this->borderColor;
}

View File

@ -2,10 +2,8 @@
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -50,6 +48,10 @@ class TextBox extends Image
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t0202');
if ($style->getBgColor()) {
$xmlWriter->writeAttribute('fillcolor', $style->getBgColor());
}
$styleWriter->write();
$styleWriter->writeBorder();

View File

@ -2,10 +2,8 @@
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.

View File

@ -2,10 +2,8 @@
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -20,15 +18,15 @@ namespace PhpOffice\PhpWordTests\Style;
use InvalidArgumentException;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\TextBox;
use PHPUnit\Framework\TestCase;
/**
* Test class for PhpOffice\PhpWord\Style\Image.
*
* @coversDefaultClass \PhpOffice\PhpWord\Style\Image
*
* @runTestsInSeparateProcesses
*/
class TextBoxTest extends \PHPUnit\Framework\TestCase
class TextBoxTest extends TestCase
{
/**
* Test setting style with normal value.
@ -55,6 +53,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
'innerMarginLeft' => '5',
'borderSize' => '2',
'borderColor' => 'red',
'bgColor' => 'blue',
];
foreach ($properties as $key => $value) {
$set = "set{$key}";
@ -89,6 +88,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
'innerMarginLeft' => '5',
'borderSize' => '2',
'borderColor' => 'red',
'bgColor' => 'blue',
];
foreach ($properties as $key => $value) {
$get = "get{$key}";
@ -305,4 +305,15 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
$object->setBorderColor($expected);
self::assertEquals($expected, $object->getBorderColor());
}
/**
* Test set/get bgColor.
*/
public function testSetGetBgColor(): void
{
$expected = 'blue';
$object = new TextBox();
$object->setBgColor($expected);
self::assertEquals($expected, $object->getBgColor());
}
}

View File

@ -2,10 +2,8 @@
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -61,11 +59,11 @@ class DocumentTest extends \PHPUnit\Framework\TestCase
$doc = TestHelperDOCX::getDocument($phpWord);
self::assertNotNull($doc);
// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr'));
}
/**
@ -408,7 +406,13 @@ class DocumentTest extends \PHPUnit\Framework\TestCase
// behind
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
$style = $element->getAttribute('style');
self::assertRegExp('/z\-index:\-[0-9]*/', $style);
// Try to address CI coverage issue for PHP 7.1 and 7.2 when using regex match assertions
if (method_exists(static::class, 'assertRegExp')) {
self::assertRegExp('/z\-index:\-[0-9]*/', $style);
} else {
self::assertMatchesRegularExpression('/z\-index:\-[0-9]*/', $style);
}
// square
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap');
@ -551,7 +555,13 @@ class DocumentTest extends \PHPUnit\Framework\TestCase
$cell->addText('Test');
$doc = TestHelperDOCX::getDocument($phpWord);
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'));
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'
)
);
}
/**