Completed
Pull Request — master (#211)
by Hura
03:53
created

Style::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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