|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Style; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Style |
|
7
|
|
|
* Represents a style to be applied to a cell |
|
8
|
|
|
* |
|
9
|
|
|
* @package Box\Spout\Writer\Style |
|
10
|
|
|
*/ |
|
11
|
|
|
class Style |
|
12
|
|
|
{ |
|
13
|
|
|
/** Default font values */ |
|
14
|
|
|
const DEFAULT_FONT_SIZE = 11; |
|
15
|
|
|
const DEFAULT_FONT_COLOR = Color::BLACK; |
|
16
|
|
|
const DEFAULT_FONT_NAME = 'Arial'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var int|null Style ID */ |
|
19
|
|
|
protected $id = null; |
|
20
|
|
|
|
|
21
|
|
|
/** @var bool Whether the font should be bold */ |
|
22
|
|
|
protected $fontBold = false; |
|
23
|
|
|
/** @var bool Whether the bold property was set */ |
|
24
|
|
|
protected $hasSetFontBold = false; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool Whether the font should be italic */ |
|
27
|
|
|
protected $fontItalic = false; |
|
28
|
|
|
/** @var bool Whether the italic property was set */ |
|
29
|
|
|
protected $hasSetFontItalic = false; |
|
30
|
|
|
|
|
31
|
|
|
/** @var bool Whether the font should be underlined */ |
|
32
|
|
|
protected $fontUnderline = false; |
|
33
|
|
|
/** @var bool Whether the underline property was set */ |
|
34
|
|
|
protected $hasSetFontUnderline = false; |
|
35
|
|
|
|
|
36
|
|
|
/** @var bool Whether the font should be struck through */ |
|
37
|
|
|
protected $fontStrikethrough = false; |
|
38
|
|
|
/** @var bool Whether the strikethrough property was set */ |
|
39
|
|
|
protected $hasSetFontStrikethrough = false; |
|
40
|
|
|
|
|
41
|
|
|
/** @var int Font size */ |
|
42
|
|
|
protected $fontSize = self::DEFAULT_FONT_SIZE; |
|
43
|
|
|
/** @var bool Whether the font size property was set */ |
|
44
|
|
|
protected $hasSetFontSize = false; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string Font color */ |
|
47
|
|
|
protected $fontColor = self::DEFAULT_FONT_COLOR; |
|
48
|
|
|
/** @var bool Whether the font color property was set */ |
|
49
|
|
|
protected $hasSetFontColor = false; |
|
50
|
|
|
|
|
51
|
|
|
/** @var string Font name */ |
|
52
|
|
|
protected $fontName = self::DEFAULT_FONT_NAME; |
|
53
|
|
|
/** @var bool Whether the font name property was set */ |
|
54
|
|
|
protected $hasSetFontName = false; |
|
55
|
|
|
|
|
56
|
|
|
/** @var bool Whether specific font properties should be applied */ |
|
57
|
|
|
protected $shouldApplyFont = false; |
|
58
|
|
|
|
|
59
|
|
|
/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */ |
|
60
|
|
|
protected $shouldWrapText = false; |
|
61
|
|
|
/** @var bool Whether the wrap text property was set */ |
|
62
|
|
|
protected $hasSetWrapText = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var Border |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $border = null; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool Whether border properties should be applied |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $shouldApplyBorder = false; |
|
73
|
|
|
|
|
74
|
|
|
/** @var string Background color */ |
|
75
|
|
|
protected $backgroundColor = null; |
|
76
|
|
|
|
|
77
|
|
|
/** @var bool */ |
|
78
|
|
|
protected $hasSetBackgroundColor = false; |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return int|null |
|
83
|
|
|
*/ |
|
84
|
237 |
|
public function getId() |
|
85
|
|
|
{ |
|
86
|
237 |
|
return $this->id; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param int $id |
|
91
|
|
|
* @return Style |
|
92
|
|
|
*/ |
|
93
|
279 |
|
public function setId($id) |
|
94
|
|
|
{ |
|
95
|
279 |
|
$this->id = $id; |
|
96
|
279 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return Border |
|
101
|
|
|
*/ |
|
102
|
96 |
|
public function getBorder() |
|
103
|
|
|
{ |
|
104
|
96 |
|
return $this->border; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Border $border |
|
109
|
|
|
* @return Style |
|
110
|
|
|
*/ |
|
111
|
24 |
|
public function setBorder(Border $border) |
|
112
|
|
|
{ |
|
113
|
24 |
|
$this->shouldApplyBorder = true; |
|
114
|
24 |
|
$this->border = $border; |
|
115
|
24 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
270 |
|
public function shouldApplyBorder() |
|
122
|
|
|
{ |
|
123
|
270 |
|
return $this->shouldApplyBorder; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
159 |
|
public function isFontBold() |
|
130
|
|
|
{ |
|
131
|
159 |
|
return $this->fontBold; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return Style |
|
136
|
|
|
*/ |
|
137
|
48 |
|
public function setFontBold() |
|
138
|
|
|
{ |
|
139
|
48 |
|
$this->fontBold = true; |
|
140
|
48 |
|
$this->hasSetFontBold = true; |
|
141
|
48 |
|
$this->shouldApplyFont = true; |
|
142
|
48 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
159 |
|
public function isFontItalic() |
|
149
|
|
|
{ |
|
150
|
159 |
|
return $this->fontItalic; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return Style |
|
155
|
|
|
*/ |
|
156
|
12 |
|
public function setFontItalic() |
|
157
|
|
|
{ |
|
158
|
12 |
|
$this->fontItalic = true; |
|
159
|
12 |
|
$this->hasSetFontItalic = true; |
|
160
|
12 |
|
$this->shouldApplyFont = true; |
|
161
|
12 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
159 |
|
public function isFontUnderline() |
|
168
|
|
|
{ |
|
169
|
159 |
|
return $this->fontUnderline; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return Style |
|
174
|
|
|
*/ |
|
175
|
18 |
|
public function setFontUnderline() |
|
176
|
|
|
{ |
|
177
|
18 |
|
$this->fontUnderline = true; |
|
178
|
18 |
|
$this->hasSetFontUnderline = true; |
|
179
|
18 |
|
$this->shouldApplyFont = true; |
|
180
|
18 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return bool |
|
185
|
|
|
*/ |
|
186
|
159 |
|
public function isFontStrikethrough() |
|
187
|
|
|
{ |
|
188
|
159 |
|
return $this->fontStrikethrough; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return Style |
|
193
|
|
|
*/ |
|
194
|
12 |
|
public function setFontStrikethrough() |
|
195
|
|
|
{ |
|
196
|
12 |
|
$this->fontStrikethrough = true; |
|
197
|
12 |
|
$this->hasSetFontStrikethrough = true; |
|
198
|
12 |
|
$this->shouldApplyFont = true; |
|
199
|
12 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return int |
|
204
|
|
|
*/ |
|
205
|
222 |
|
public function getFontSize() |
|
206
|
|
|
{ |
|
207
|
222 |
|
return $this->fontSize; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param int $fontSize Font size, in pixels |
|
212
|
|
|
* @return Style |
|
213
|
|
|
*/ |
|
214
|
159 |
|
public function setFontSize($fontSize) |
|
215
|
|
|
{ |
|
216
|
159 |
|
$this->fontSize = $fontSize; |
|
217
|
159 |
|
$this->hasSetFontSize = true; |
|
218
|
159 |
|
$this->shouldApplyFont = true; |
|
219
|
159 |
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
222 |
|
public function getFontColor() |
|
226
|
|
|
{ |
|
227
|
222 |
|
return $this->fontColor; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Sets the font color. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $fontColor ARGB color (@see Color) |
|
234
|
|
|
* @return Style |
|
235
|
|
|
*/ |
|
236
|
6 |
|
public function setFontColor($fontColor) |
|
237
|
|
|
{ |
|
238
|
6 |
|
$this->fontColor = $fontColor; |
|
239
|
6 |
|
$this->hasSetFontColor = true; |
|
240
|
6 |
|
$this->shouldApplyFont = true; |
|
241
|
6 |
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
261 |
|
public function getFontName() |
|
248
|
|
|
{ |
|
249
|
261 |
|
return $this->fontName; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param string $fontName Name of the font to use |
|
254
|
|
|
* @return Style |
|
255
|
|
|
*/ |
|
256
|
150 |
|
public function setFontName($fontName) |
|
257
|
|
|
{ |
|
258
|
150 |
|
$this->fontName = $fontName; |
|
259
|
150 |
|
$this->hasSetFontName = true; |
|
260
|
150 |
|
$this->shouldApplyFont = true; |
|
261
|
150 |
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
234 |
|
public function shouldWrapText() |
|
268
|
|
|
{ |
|
269
|
234 |
|
return $this->shouldWrapText; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param bool|void $shouldWrap Should the text be wrapped |
|
274
|
|
|
* @return Style |
|
275
|
|
|
*/ |
|
276
|
30 |
|
public function setShouldWrapText($shouldWrap = true) |
|
277
|
|
|
{ |
|
278
|
30 |
|
$this->shouldWrapText = $shouldWrap; |
|
279
|
30 |
|
$this->hasSetWrapText = true; |
|
280
|
30 |
|
return $this; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @return bool |
|
285
|
|
|
*/ |
|
286
|
186 |
|
public function hasSetWrapText() |
|
287
|
|
|
{ |
|
288
|
186 |
|
return $this->hasSetWrapText; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @return bool Whether specific font properties should be applied |
|
293
|
|
|
*/ |
|
294
|
186 |
|
public function shouldApplyFont() |
|
295
|
|
|
{ |
|
296
|
186 |
|
return $this->shouldApplyFont; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Sets the background color |
|
301
|
|
|
* @param string $color ARGB color (@see Color) |
|
302
|
|
|
* @return Style |
|
303
|
|
|
*/ |
|
304
|
18 |
|
public function setBackgroundColor($color) |
|
305
|
|
|
{ |
|
306
|
18 |
|
$this->hasSetBackgroundColor = true; |
|
307
|
18 |
|
$this->backgroundColor = $color; |
|
308
|
18 |
|
return $this; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return string |
|
313
|
|
|
*/ |
|
314
|
147 |
|
public function getBackgroundColor() |
|
315
|
|
|
{ |
|
316
|
147 |
|
return $this->backgroundColor; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* |
|
321
|
|
|
* @return bool Whether the background color should be applied |
|
322
|
|
|
*/ |
|
323
|
153 |
|
public function shouldApplyBackgroundColor() |
|
324
|
|
|
{ |
|
325
|
153 |
|
return $this->hasSetBackgroundColor; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Serializes the style for future comparison with other styles. |
|
330
|
|
|
* The ID is excluded from the comparison, as we only care about |
|
331
|
|
|
* actual style properties. |
|
332
|
|
|
* |
|
333
|
|
|
* @return string The serialized style |
|
334
|
|
|
*/ |
|
335
|
279 |
|
public function serialize() |
|
336
|
|
|
{ |
|
337
|
|
|
// In order to be able to properly compare style, set static ID value |
|
338
|
279 |
|
$currentId = $this->id; |
|
339
|
279 |
|
$this->setId(0); |
|
340
|
|
|
|
|
341
|
279 |
|
$serializedStyle = serialize($this); |
|
342
|
|
|
|
|
343
|
279 |
|
$this->setId($currentId); |
|
344
|
|
|
|
|
345
|
279 |
|
return $serializedStyle; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Merges the current style with the given style, using the given style as a base. This means that: |
|
350
|
|
|
* - if current style and base style both have property A set, use current style property's value |
|
351
|
|
|
* - if current style has property A set but base style does not, use current style property's value |
|
352
|
|
|
* - if base style has property A set but current style does not, use base style property's value |
|
353
|
|
|
* |
|
354
|
|
|
* @NOTE: This function returns a new style. |
|
355
|
|
|
* |
|
356
|
|
|
* @param Style $baseStyle |
|
357
|
|
|
* @return Style New style corresponding to the merge of the 2 styles |
|
358
|
|
|
*/ |
|
359
|
93 |
|
public function mergeWith($baseStyle) |
|
360
|
|
|
{ |
|
361
|
93 |
|
$mergedStyle = clone $this; |
|
362
|
|
|
|
|
363
|
93 |
|
$this->mergeFontStyles($mergedStyle, $baseStyle); |
|
364
|
93 |
|
$this->mergeOtherFontProperties($mergedStyle, $baseStyle); |
|
365
|
93 |
|
$this->mergeCellProperties($mergedStyle, $baseStyle); |
|
366
|
|
|
|
|
367
|
93 |
|
return $mergedStyle; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param Style $styleToUpdate (passed as reference) |
|
372
|
|
|
* @param Style $baseStyle |
|
373
|
|
|
* @return void |
|
374
|
|
|
*/ |
|
375
|
93 |
|
private function mergeFontStyles($styleToUpdate, $baseStyle) |
|
376
|
|
|
{ |
|
377
|
93 |
|
if (!$this->hasSetFontBold && $baseStyle->isFontBold()) { |
|
378
|
6 |
|
$styleToUpdate->setFontBold(); |
|
379
|
6 |
|
} |
|
380
|
93 |
|
if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) { |
|
381
|
3 |
|
$styleToUpdate->setFontItalic(); |
|
382
|
3 |
|
} |
|
383
|
93 |
|
if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) { |
|
384
|
3 |
|
$styleToUpdate->setFontUnderline(); |
|
385
|
3 |
|
} |
|
386
|
93 |
|
if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) { |
|
387
|
3 |
|
$styleToUpdate->setFontStrikethrough(); |
|
388
|
3 |
|
} |
|
389
|
93 |
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* @param Style $styleToUpdate Style to update (passed as reference) |
|
393
|
|
|
* @param Style $baseStyle |
|
394
|
|
|
* @return void |
|
395
|
|
|
*/ |
|
396
|
93 |
|
private function mergeOtherFontProperties($styleToUpdate, $baseStyle) |
|
397
|
|
|
{ |
|
398
|
93 |
|
if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) { |
|
399
|
42 |
|
$styleToUpdate->setFontSize($baseStyle->getFontSize()); |
|
400
|
42 |
|
} |
|
401
|
93 |
|
if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) { |
|
402
|
|
|
$styleToUpdate->setFontColor($baseStyle->getFontColor()); |
|
403
|
|
|
} |
|
404
|
93 |
|
if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) { |
|
405
|
39 |
|
$styleToUpdate->setFontName($baseStyle->getFontName()); |
|
406
|
39 |
|
} |
|
407
|
93 |
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* @param Style $styleToUpdate Style to update (passed as reference) |
|
411
|
|
|
* @param Style $baseStyle |
|
412
|
|
|
* @return void |
|
413
|
|
|
*/ |
|
414
|
93 |
|
private function mergeCellProperties($styleToUpdate, $baseStyle) |
|
415
|
|
|
{ |
|
416
|
93 |
|
if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) { |
|
417
|
3 |
|
$styleToUpdate->setShouldWrapText(); |
|
418
|
3 |
|
} |
|
419
|
93 |
|
if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) { |
|
420
|
3 |
|
$styleToUpdate->setBorder($baseStyle->getBorder()); |
|
421
|
3 |
|
} |
|
422
|
93 |
|
if (!$this->hasSetBackgroundColor && $baseStyle->shouldApplyBackgroundColor()) { |
|
423
|
|
|
$styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor()); |
|
424
|
|
|
} |
|
425
|
93 |
|
} |
|
426
|
|
|
} |
|
427
|
|
|
|