Failed Conditions
Pull Request — master (#209)
by
unknown
02:31
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
    /** @var string Custom number format */
65
    protected $numberFormat = '';
66
    /** @var boolean Whether specific number format has been set */
67
    protected $hasSetNumberFormat = false;
68
    /** @var integer Holds the number format id */
69
    protected $numberFormatId = 0;
70
71
    /**
72
     * @return int|null
73
     */
74 153
    public function getId()
75
    {
76 153
        return $this->id;
77
    }
78
79
    /**
80
     * @param int $id
81
     * @return Style
82
     */
83 231
    public function setId($id)
84
    {
85 231
        $this->id = $id;
86 231
        return $this;
87
    }
88
89
    /**
90
     * @return boolean
91
     */
92 114
    public function isFontBold()
93
    {
94 114
        return $this->fontBold;
95
    }
96
97
    /**
98
     * @return Style
99
     */
100 36
    public function setFontBold()
101
    {
102 36
        $this->fontBold = true;
103 36
        $this->hasSetFontBold = true;
104 36
        $this->shouldApplyFont = true;
105 36
        return $this;
106
    }
107
108
    /**
109
     * @return boolean
110
     */
111 114
    public function isFontItalic()
112
    {
113 114
        return $this->fontItalic;
114
    }
115
116
    /**
117
     * @return Style
118
     */
119 12
    public function setFontItalic()
120
    {
121 12
        $this->fontItalic = true;
122 12
        $this->hasSetFontItalic = true;
123 12
        $this->shouldApplyFont = true;
124 12
        return $this;
125
    }
126
127
    /**
128
     * @return boolean
129
     */
130 114
    public function isFontUnderline()
131
    {
132 114
        return $this->fontUnderline;
133
    }
134
135
    /**
136
     * @return Style
137
     */
138 18
    public function setFontUnderline()
139
    {
140 18
        $this->fontUnderline = true;
141 18
        $this->hasSetFontUnderline = true;
142 18
        $this->shouldApplyFont = true;
143 18
        return $this;
144
    }
145
146
    /**
147
     * @return boolean
148
     */
149 114
    public function isFontStrikethrough()
150
    {
151 114
        return $this->fontStrikethrough;
152
    }
153
154
    /**
155
     * @return Style
156
     */
157 12
    public function setFontStrikethrough()
158
    {
159 12
        $this->fontStrikethrough = true;
160 12
        $this->hasSetFontStrikethrough = true;
161 12
        $this->shouldApplyFont = true;
162 12
        return $this;
163
    }
164
165
    /**
166
     * @return int
167
     */
168 168
    public function getFontSize()
169
    {
170 168
        return $this->fontSize;
171
    }
172
173
    /**
174
     * @param int $fontSize Font size, in pixels
175
     * @return Style
176
     */
177 126
    public function setFontSize($fontSize)
178
    {
179 126
        $this->fontSize = $fontSize;
180 126
        $this->hasSetFontSize = true;
181 126
        $this->shouldApplyFont = true;
182 126
        return $this;
183
    }
184
185
    /**
186
     * @return string
187
     */
188 168
    public function getFontColor()
189
    {
190 168
        return $this->fontColor;
191
    }
192
193
    /**
194
     * Sets the font color.
195
     *
196
     * @param string $fontColor ARGB color (@see Color)
197
     * @return Style
198
     */
199 6
    public function setFontColor($fontColor)
200
    {
201 6
        $this->fontColor = $fontColor;
202 6
        $this->hasSetFontColor = true;
203 6
        $this->shouldApplyFont = true;
204 6
        return $this;
205
    }
206
207
    /**
208
     * @return string
209
     */
210 210
    public function getFontName()
211
    {
212 210
        return $this->fontName;
213
    }
214
215
    /**
216
     * @param string $fontName Name of the font to use
217
     * @return Style
218
     */
219 120
    public function setFontName($fontName)
220
    {
221 120
        $this->fontName = $fontName;
222 120
        $this->hasSetFontName = true;
223 120
        $this->shouldApplyFont = true;
224 120
        return $this;
225
    }
226
227
    /**
228
     * @return boolean
229
     */
230 186
    public function shouldWrapText()
231
    {
232 186
        return $this->shouldWrapText;
233
    }
234
235
    /**
236
     * @return Style
237
     */
238 30
    public function setShouldWrapText()
239
    {
240 30
        $this->shouldWrapText = true;
241 30
        $this->hasSetWrapText = true;
242 30
        return $this;
243
    }
244
245
    /**
246
     * @return bool Whether specific font properties should be applied
247
     */
248 135
    public function shouldApplyFont()
249
    {
250 135
        return $this->shouldApplyFont;
251
    }
252
253
    public function setNumberFormat($format)
254
    {
255
        $this->numberFormat = $format;
256
        $this->hasSetNumberFormat = true;
257
        return $this;
258
    }
259
260
    public function setNumberFormatId($id)
261
    {
262
        $this->numberFormatId = $id;
263
    }
264
265
266 63
    public function shouldApplyNumberFormat()
267
    {
268 63
        return $this->hasSetNumberFormat;
269
    }
270
271 63
    public function getNumberFormatId()
272
    {
273 63
        return $this->numberFormatId;
274
    }
275
276
    public function getNumberFormat()
277
    {
278
        return $this->numberFormat;
279
    }
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 231
    public function serialize()
290
    {
291
        // In order to be able to properly compare style, set static ID value
292 231
        $currentId = $this->id;
293 231
        $this->setId(0);
294
295 231
        $serializedStyle = serialize($this);
296
297 231
        $this->setId($currentId);
298
299 231
        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 66
    public function mergeWith($baseStyle)
314
    {
315 66
        $mergedStyle = clone $this;
316
317 66
        if (!$this->hasSetFontBold && $baseStyle->isFontBold()) {
318 3
            $mergedStyle->setFontBold();
319 3
        }
320 66
        if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) {
321 3
            $mergedStyle->setFontItalic();
322 3
        }
323 66
        if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) {
324 3
            $mergedStyle->setFontUnderline();
325 3
        }
326 66
        if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) {
327 3
            $mergedStyle->setFontStrikethrough();
328 3
        }
329 66
        if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
330 24
            $mergedStyle->setFontSize($baseStyle->getFontSize());
331 24
        }
332 66
        if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
333
            $mergedStyle->setFontColor($baseStyle->getFontColor());
334
        }
335 66
        if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
336 21
            $mergedStyle->setFontName($baseStyle->getFontName());
337 21
        }
338 66
        if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) {
339 3
            $mergedStyle->setShouldWrapText();
340 3
        }
341
342 66
        return $mergedStyle;
343
    }
344
}
345