Skip to content

Commit 999a9c5

Browse files
committed
Style: Remove bgColor from Font, Table, and Cell and put it into the new Shading style
1 parent 9afa461 commit 999a9c5

File tree

14 files changed

+411
-204
lines changed

14 files changed

+411
-204
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This release marked heavy refactorings on internal code structure with the creat
4242
- Section: Ability to define gutter and line numbering - @ivanlanin
4343
- Font: Small caps, all caps, and double strikethrough - @ivanlanin GH-151
4444
- Settings: Ability to use measurement unit other than twips with `setMeasurementUnit` - @ivanlanin GH-199
45+
- Style: Remove `bgColor` from `Font`, `Table`, and `Cell` and put it into the new `Shading` style - @ivanlanin
4546

4647
### Bugfixes
4748

samples/Sample_04_Textrun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Ads styles
99
$phpWord->addParagraphStyle('pStyle', array('spacing'=>100));
1010
$phpWord->addFontStyle('BoldText', array('bold'=>true));
11-
$phpWord->addFontStyle('ColoredText', array('color'=>'FF8080'));
11+
$phpWord->addFontStyle('ColoredText', array('color'=>'FF8080', 'bgColor' => 'FFFFCC'));
1212
$phpWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
1313

1414
// New portrait section

samples/Sample_09_Tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
$section->addText("Table with colspan and rowspan", $header);
5656

5757
$styleTable = array('borderSize' => 6, 'borderColor' => '999999');
58-
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center');
58+
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center', 'bgColor' => 'FFFF00');
5959
$cellRowContinue = array('vMerge' => 'continue');
6060
$cellColSpan = array('gridSpan' => 2, 'valign' => 'center');
6161
$cellHCentered = array('align' => 'center');

src/PhpWord/Style/Cell.php

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

1010
namespace PhpOffice\PhpWord\Style;
1111

12-
use PhpOffice\PhpWord\Shared\String;
12+
use PhpOffice\PhpWord\Style\Shading;
1313

1414
/**
1515
* Table cell style
@@ -33,13 +33,6 @@ class Cell extends Border
3333
*/
3434
private $textDirection;
3535

36-
/**
37-
* Background-Color
38-
*
39-
* @var string
40-
*/
41-
private $bgColor;
42-
4336
/**
4437
* Border Default Color
4538
*
@@ -64,14 +57,20 @@ class Cell extends Border
6457
*/
6558
private $vMerge = null;
6659

60+
/**
61+
* Shading
62+
*
63+
* @var \PhpOffice\PhpWord\Style\Shading
64+
*/
65+
private $shading;
66+
6767
/**
6868
* Create a new Cell Style
6969
*/
7070
public function __construct()
7171
{
7272
$this->valign = null;
7373
$this->textDirection = null;
74-
$this->bgColor = null;
7574
$this->borderTopSize = null;
7675
$this->borderTopColor = null;
7776
$this->borderLeftSize = null;
@@ -83,24 +82,6 @@ public function __construct()
8382
$this->defaultBorderColor = '000000';
8483
}
8584

86-
/**
87-
* Set style value
88-
*
89-
* @param string $key
90-
* @param mixed $value
91-
*/
92-
public function setStyleValue($key, $value)
93-
{
94-
$key = String::removeUnderscorePrefix($key);
95-
if ($key == 'borderSize') {
96-
$this->setBorderSize($value);
97-
} elseif ($key == 'borderColor') {
98-
$this->setBorderColor($value);
99-
} else {
100-
$this->$key = $value;
101-
}
102-
}
103-
10485
/**
10586
* Get vertical align
10687
*/
@@ -138,21 +119,26 @@ public function setTextDirection($pValue = null)
138119
}
139120

140121
/**
141-
* Get background color
122+
* Get background
123+
*
124+
* @return string
142125
*/
143126
public function getBgColor()
144127
{
145-
return $this->bgColor;
128+
if (!is_null($this->shading)) {
129+
return $this->shading->getFill();
130+
}
146131
}
147132

148133
/**
149-
* Set background color
134+
* Set background
150135
*
151-
* @param string $pValue
136+
* @param string $value
137+
* @return \PhpOffice\PhpWord\Style\Table
152138
*/
153-
public function setBgColor($pValue = null)
139+
public function setBgColor($value = null)
154140
{
155-
$this->bgColor = $pValue;
141+
$this->setShading(array('fill' => $value));
156142
}
157143

158144
/**
@@ -198,4 +184,34 @@ public function getVMerge()
198184
{
199185
return $this->vMerge;
200186
}
187+
188+
/**
189+
* Get shading
190+
*
191+
* @return \PhpOffice\PhpWord\Style\Shading
192+
*/
193+
public function getShading()
194+
{
195+
return $this->shading;
196+
}
197+
198+
/**
199+
* Set shading
200+
*
201+
* @param array $value
202+
* @return self
203+
*/
204+
public function setShading($value = null)
205+
{
206+
if (is_array($value)) {
207+
if (!$this->shading instanceof Shading) {
208+
$this->shading = new Shading();
209+
}
210+
$this->shading->setStyleByArray($value);
211+
} else {
212+
$this->shading = null;
213+
}
214+
215+
return $this;
216+
}
201217
}

src/PhpWord/Style/Font.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PhpOffice\PhpWord\PhpWord;
1313
use PhpOffice\PhpWord\Exception\InvalidStyleException;
14+
use PhpOffice\PhpWord\Style\Shading;
1415

1516
/**
1617
* Font style
@@ -153,12 +154,6 @@ class Font extends AbstractStyle
153154
*/
154155
private $fgColor = null;
155156

156-
/**
157-
* Background color
158-
*
159-
* @var string
160-
*/
161-
private $bgColor = null;
162157
/**
163158
* Text line height
164159
*
@@ -195,6 +190,13 @@ class Font extends AbstractStyle
195190
*/
196191
private $allCaps = false;
197192

193+
/**
194+
* Shading
195+
*
196+
* @var \PhpOffice\PhpWord\Style\Shading
197+
*/
198+
private $shading;
199+
198200
/**
199201
* Create new font style
200202
*
@@ -501,26 +503,26 @@ public function setFgColor($value = null)
501503
}
502504

503505
/**
504-
* Get background color
506+
* Get background
505507
*
506-
* @return string
508+
* @return string
507509
*/
508510
public function getBgColor()
509511
{
510-
return $this->bgColor;
512+
if (!is_null($this->shading)) {
513+
return $this->shading->getFill();
514+
}
511515
}
512516

513517
/**
514-
* Set background color
518+
* Set background
515519
*
516520
* @param string $value
517-
* @return $this
521+
* @return \PhpOffice\PhpWord\Style\Table
518522
*/
519523
public function setBgColor($value = null)
520524
{
521-
$this->bgColor = $value;
522-
523-
return $this;
525+
$this->setShading(array('fill' => $value));
524526
}
525527

526528
/**
@@ -649,4 +651,34 @@ public function setAllCaps($value = false)
649651

650652
return $this;
651653
}
654+
655+
/**
656+
* Get shading
657+
*
658+
* @return \PhpOffice\PhpWord\Style\Shading
659+
*/
660+
public function getShading()
661+
{
662+
return $this->shading;
663+
}
664+
665+
/**
666+
* Set shading
667+
*
668+
* @param array $value
669+
* @return self
670+
*/
671+
public function setShading($value = null)
672+
{
673+
if (is_array($value)) {
674+
if (!$this->shading instanceof Shading) {
675+
$this->shading = new Shading();
676+
}
677+
$this->shading->setStyleByArray($value);
678+
} else {
679+
$this->shading = null;
680+
}
681+
682+
return $this;
683+
}
652684
}

src/PhpWord/Style/LineNumbering.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function getRestart()
142142
/**
143143
* Set distance
144144
*
145-
* @param int|float $value
145+
* @param string $value
146146
* @return self
147147
*/
148148
public function setRestart($value = null)

src/PhpWord/Style/Section.php

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

1010
namespace PhpOffice\PhpWord\Style;
1111

12+
use PhpOffice\PhpWord\Style\LineNumbering;
13+
1214
/**
1315
* Section settings
1416
*/
@@ -146,7 +148,7 @@ class Section extends Border
146148
/**
147149
* Line numbering
148150
*
149-
* @var array
151+
* @var \PhpOffice\PhpWord\Style\LineNumbering
150152
* @link http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html
151153
*/
152154
private $lineNumbering;
@@ -491,7 +493,7 @@ public function setBreakType($value = null)
491493
/**
492494
* Get line numbering
493495
*
494-
* @return self
496+
* @return \PhpOffice\PhpWord\Style\LineNumbering
495497
*/
496498
public function getLineNumbering()
497499
{
@@ -506,20 +508,15 @@ public function getLineNumbering()
506508
*/
507509
public function setLineNumbering($value = null)
508510
{
509-
if ($this->lineNumbering instanceof LineNumbering) {
511+
if (is_array($value)) {
512+
if (!$this->lineNumbering instanceof LineNumbering) {
513+
$this->lineNumbering = new LineNumbering($value);
514+
}
510515
$this->lineNumbering->setStyleByArray($value);
511516
} else {
512-
$this->lineNumbering = new LineNumbering($value);
517+
$this->lineNumbering = null;
513518
}
514519

515520
return $this;
516521
}
517-
518-
/**
519-
* Remove line numbering
520-
*/
521-
public function removeLineNumbering()
522-
{
523-
$this->lineNumbering = null;
524-
}
525522
}

0 commit comments

Comments
 (0)