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

View File

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

View File

@ -2,10 +2,8 @@
/** /**
* This file is part of PHPWord - A pure PHP library for reading and writing * This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents. * word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser * PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation. * General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE * For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of * file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * 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 * This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents. * word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser * PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation. * General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE * For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of * file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -20,15 +18,15 @@ namespace PhpOffice\PhpWordTests\Style;
use InvalidArgumentException; use InvalidArgumentException;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\TextBox; use PhpOffice\PhpWord\Style\TextBox;
use PHPUnit\Framework\TestCase;
/** /**
* Test class for PhpOffice\PhpWord\Style\Image. * Test class for PhpOffice\PhpWord\Style\Image.
* *
* @coversDefaultClass \PhpOffice\PhpWord\Style\Image * @coversDefaultClass \PhpOffice\PhpWord\Style\Image
*
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class TextBoxTest extends \PHPUnit\Framework\TestCase class TextBoxTest extends TestCase
{ {
/** /**
* Test setting style with normal value. * Test setting style with normal value.
@ -55,6 +53,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
'innerMarginLeft' => '5', 'innerMarginLeft' => '5',
'borderSize' => '2', 'borderSize' => '2',
'borderColor' => 'red', 'borderColor' => 'red',
'bgColor' => 'blue',
]; ];
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
$set = "set{$key}"; $set = "set{$key}";
@ -89,6 +88,7 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
'innerMarginLeft' => '5', 'innerMarginLeft' => '5',
'borderSize' => '2', 'borderSize' => '2',
'borderColor' => 'red', 'borderColor' => 'red',
'bgColor' => 'blue',
]; ];
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
$get = "get{$key}"; $get = "get{$key}";
@ -305,4 +305,15 @@ class TextBoxTest extends \PHPUnit\Framework\TestCase
$object->setBorderColor($expected); $object->setBorderColor($expected);
self::assertEquals($expected, $object->getBorderColor()); 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 * This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents. * word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser * PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation. * General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE * For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of * file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
@ -408,7 +406,13 @@ class DocumentTest extends \PHPUnit\Framework\TestCase
// behind // behind
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape'); $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
$style = $element->getAttribute('style'); $style = $element->getAttribute('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); self::assertRegExp('/z\-index:\-[0-9]*/', $style);
} else {
self::assertMatchesRegularExpression('/z\-index:\-[0-9]*/', $style);
}
// square // square
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap'); $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'); $cell->addText('Test');
$doc = TestHelperDOCX::getDocument($phpWord); $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'
)
);
} }
/** /**