|
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
|
|
|
/** |
|
75
|
|
|
* @return int|null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getId() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->id; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $id |
|
84
|
|
|
* @return Style |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setId($id) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->id = $id; |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return Border |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getBorder() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->border; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param Border $border |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setBorder(Border $border) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->shouldApplyBorder = true; |
|
106
|
|
|
$this->border = $border; |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return boolean |
|
112
|
|
|
*/ |
|
113
|
|
|
public function shouldApplyBorder() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->shouldApplyBorder; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return boolean |
|
120
|
|
|
*/ |
|
121
|
|
|
public function isFontBold() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->fontBold; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return Style |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setFontBold() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->fontBold = true; |
|
132
|
|
|
$this->hasSetFontBold = true; |
|
133
|
|
|
$this->shouldApplyFont = true; |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public function isFontItalic() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->fontItalic; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return Style |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setFontItalic() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->fontItalic = true; |
|
151
|
|
|
$this->hasSetFontItalic = true; |
|
152
|
|
|
$this->shouldApplyFont = true; |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return boolean |
|
158
|
|
|
*/ |
|
159
|
|
|
public function isFontUnderline() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->fontUnderline; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return Style |
|
166
|
|
|
*/ |
|
167
|
|
|
public function setFontUnderline() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->fontUnderline = true; |
|
170
|
|
|
$this->hasSetFontUnderline = true; |
|
171
|
|
|
$this->shouldApplyFont = true; |
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return boolean |
|
177
|
|
|
*/ |
|
178
|
|
|
public function isFontStrikethrough() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->fontStrikethrough; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return Style |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setFontStrikethrough() |
|
187
|
|
|
{ |
|
188
|
|
|
$this->fontStrikethrough = true; |
|
189
|
|
|
$this->hasSetFontStrikethrough = true; |
|
190
|
|
|
$this->shouldApplyFont = true; |
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return int |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getFontSize() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->fontSize; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param int $fontSize Font size, in pixels |
|
204
|
|
|
* @return Style |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setFontSize($fontSize) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->fontSize = $fontSize; |
|
209
|
|
|
$this->hasSetFontSize = true; |
|
210
|
|
|
$this->shouldApplyFont = true; |
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getFontColor() |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->fontColor; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Sets the font color. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $fontColor ARGB color (@see Color) |
|
226
|
|
|
* @return Style |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setFontColor($fontColor) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->fontColor = $fontColor; |
|
231
|
|
|
$this->hasSetFontColor = true; |
|
232
|
|
|
$this->shouldApplyFont = true; |
|
233
|
|
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return string |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getFontName() |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->fontName; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param string $fontName Name of the font to use |
|
246
|
|
|
* @return Style |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setFontName($fontName) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->fontName = $fontName; |
|
251
|
|
|
$this->hasSetFontName = true; |
|
252
|
|
|
$this->shouldApplyFont = true; |
|
253
|
|
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return boolean |
|
258
|
|
|
*/ |
|
259
|
|
|
public function shouldWrapText() |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->shouldWrapText; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return Style |
|
266
|
|
|
*/ |
|
267
|
|
|
public function setShouldWrapText() |
|
268
|
|
|
{ |
|
269
|
|
|
$this->shouldWrapText = true; |
|
270
|
|
|
$this->hasSetWrapText = true; |
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @return bool Whether specific font properties should be applied |
|
276
|
|
|
*/ |
|
277
|
|
|
public function shouldApplyFont() |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->shouldApplyFont; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Serializes the style for future comparison with other styles. |
|
284
|
|
|
* The ID is excluded from the comparison, as we only care about |
|
285
|
|
|
* actual style properties. |
|
286
|
|
|
* |
|
287
|
|
|
* @return string The serialized style |
|
288
|
|
|
*/ |
|
289
|
|
|
public function serialize() |
|
290
|
|
|
{ |
|
291
|
|
|
// In order to be able to properly compare style, set static ID value |
|
292
|
|
|
$currentId = $this->id; |
|
293
|
|
|
$this->setId(0); |
|
294
|
|
|
|
|
295
|
|
|
$serializedStyle = serialize($this); |
|
296
|
|
|
|
|
297
|
|
|
$this->setId($currentId); |
|
298
|
|
|
|
|
299
|
|
|
return $serializedStyle; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Merges the current style with the given style, using the given style as a base. This means that: |
|
304
|
|
|
* - if current style and base style both have property A set, use current style property's value |
|
305
|
|
|
* - if current style has property A set but base style does not, use current style property's value |
|
306
|
|
|
* - if base style has property A set but current style does not, use base style property's value |
|
307
|
|
|
* |
|
308
|
|
|
* @NOTE: This function returns a new style. |
|
309
|
|
|
* |
|
310
|
|
|
* @param Style $baseStyle |
|
311
|
|
|
* @return Style New style corresponding to the merge of the 2 styles |
|
312
|
|
|
*/ |
|
313
|
|
|
public function mergeWith($baseStyle) |
|
314
|
|
|
{ |
|
315
|
|
|
$mergedStyle = clone $this; |
|
316
|
|
|
|
|
317
|
|
|
if (!$this->hasSetFontBold && $baseStyle->isFontBold()) { |
|
318
|
|
|
$mergedStyle->setFontBold(); |
|
319
|
|
|
} |
|
320
|
|
|
if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) { |
|
321
|
|
|
$mergedStyle->setFontItalic(); |
|
322
|
|
|
} |
|
323
|
|
|
if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) { |
|
324
|
|
|
$mergedStyle->setFontUnderline(); |
|
325
|
|
|
} |
|
326
|
|
|
if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) { |
|
327
|
|
|
$mergedStyle->setFontStrikethrough(); |
|
328
|
|
|
} |
|
329
|
|
|
if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) { |
|
330
|
|
|
$mergedStyle->setFontSize($baseStyle->getFontSize()); |
|
331
|
|
|
} |
|
332
|
|
|
if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) { |
|
333
|
|
|
$mergedStyle->setFontColor($baseStyle->getFontColor()); |
|
334
|
|
|
} |
|
335
|
|
|
if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) { |
|
336
|
|
|
$mergedStyle->setFontName($baseStyle->getFontName()); |
|
337
|
|
|
} |
|
338
|
|
|
if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) { |
|
339
|
|
|
$mergedStyle->setShouldWrapText(); |
|
340
|
|
|
} |
|
341
|
|
|
if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) { |
|
342
|
|
|
$mergedStyle->setBorder($baseStyle->getBorder()); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
return $mergedStyle; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|