Completed
Pull Request — master (#209)
by
unknown
11:22
created

Style::isFontItalic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
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 153
    protected $hasSetNumberFormat = false;
68
    /** @var integer Holds the number format id */
69 153
    protected $numberFormatId = 0;
70
71
    /**
72
     * @return int|null
73
     */
74
    public function getId()
75
    {
76 231
        return $this->id;
77
    }
78 231
79 231
    /**
80
     * @param int $id
81
     * @return Style
82
     */
83
    public function setId($id)
84
    {
85 114
        $this->id = $id;
86
        return $this;
87 114
    }
88
89
    /**
90
     * @return boolean
91
     */
92
    public function isFontBold()
93 36
    {
94
        return $this->fontBold;
95 36
    }
96 36
97 36
    /**
98 36
     * @return Style
99
     */
100
    public function setFontBold()
101
    {
102
        $this->fontBold = true;
103
        $this->hasSetFontBold = true;
104 114
        $this->shouldApplyFont = true;
105
        return $this;
106 114
    }
107
108
    /**
109
     * @return boolean
110
     */
111
    public function isFontItalic()
112 12
    {
113
        return $this->fontItalic;
114 12
    }
115 12
116 12
    /**
117 12
     * @return Style
118
     */
119
    public function setFontItalic()
120
    {
121
        $this->fontItalic = true;
122
        $this->hasSetFontItalic = true;
123 114
        $this->shouldApplyFont = true;
124
        return $this;
125 114
    }
126
127
    /**
128
     * @return boolean
129
     */
130
    public function isFontUnderline()
131 18
    {
132
        return $this->fontUnderline;
133 18
    }
134 18
135 18
    /**
136 18
     * @return Style
137
     */
138
    public function setFontUnderline()
139
    {
140
        $this->fontUnderline = true;
141
        $this->hasSetFontUnderline = true;
142 114
        $this->shouldApplyFont = true;
143
        return $this;
144 114
    }
145
146
    /**
147
     * @return boolean
148
     */
149
    public function isFontStrikethrough()
150 12
    {
151
        return $this->fontStrikethrough;
152 12
    }
153 12
154 12
    /**
155 12
     * @return Style
156
     */
157
    public function setFontStrikethrough()
158
    {
159
        $this->fontStrikethrough = true;
160
        $this->hasSetFontStrikethrough = true;
161 168
        $this->shouldApplyFont = true;
162
        return $this;
163 168
    }
164
165
    /**
166
     * @return int
167
     */
168
    public function getFontSize()
169
    {
170 126
        return $this->fontSize;
171
    }
172 126
173 126
    /**
174 126
     * @param int $fontSize Font size, in pixels
175 126
     * @return Style
176
     */
177
    public function setFontSize($fontSize)
178
    {
179
        $this->fontSize = $fontSize;
180
        $this->hasSetFontSize = true;
181 168
        $this->shouldApplyFont = true;
182
        return $this;
183 168
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getFontColor()
189
    {
190
        return $this->fontColor;
191
    }
192 6
193
    /**
194 6
     * Sets the font color.
195 6
     *
196 6
     * @param string $fontColor ARGB color (@see Color)
197 6
     * @return Style
198
     */
199
    public function setFontColor($fontColor)
200
    {
201
        $this->fontColor = $fontColor;
202
        $this->hasSetFontColor = true;
203 210
        $this->shouldApplyFont = true;
204
        return $this;
205 210
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getFontName()
211
    {
212 120
        return $this->fontName;
213
    }
214 120
215 120
    /**
216 120
     * @param string $fontName Name of the font to use
217 120
     * @return Style
218
     */
219
    public function setFontName($fontName)
220
    {
221
        $this->fontName = $fontName;
222
        $this->hasSetFontName = true;
223 186
        $this->shouldApplyFont = true;
224
        return $this;
225 186
    }
226
227
    /**
228
     * @return boolean
229
     */
230
    public function shouldWrapText()
231 30
    {
232
        return $this->shouldWrapText;
233 30
    }
234 30
235 30
    /**
236
     * @return Style
237
     */
238
    public function setShouldWrapText()
239
    {
240
        $this->shouldWrapText = true;
241 135
        $this->hasSetWrapText = true;
242
        return $this;
243 135
    }
244
245
    /**
246
     * @return bool Whether specific font properties should be applied
247
     */
248
    public function shouldApplyFont()
249
    {
250
        return $this->shouldApplyFont;
251
    }
252
253 231
    public function setNumberFormat($format)
254
    {
255
        $this->numberFormat = $format;
256 231
        $this->hasSetNumberFormat = true;
257 231
        return $this;
258
    }
259 231
260
    public function setNumberFormatId($id)
261 231
    {
262
        $this->numberFormatId = $id;
263 231
    }
264
265
266
    public function shouldApplyNumberFormat()
267
    {
268
        return $this->hasSetNumberFormat;
269
    }
270
271
    public function getNumberFormatId()
272
    {
273
        return $this->numberFormatId;
274
    }
275
276
    public function getNumberFormat()
277 66
    {
278
        return $this->numberFormat;
279 66
    }
280
281 66
282 3
    /**
283 3
     * Serializes the style for future comparison with other styles.
284 66
     * The ID is excluded from the comparison, as we only care about
285 3
     * actual style properties.
286 3
     *
287 66
     * @return string The serialized style
288 3
     */
289 3
    public function serialize()
290 66
    {
291 3
        // In order to be able to properly compare style, set static ID value
292 3
        $currentId = $this->id;
293 66
        $this->setId(0);
294 24
295 24
        $serializedStyle = serialize($this);
296 66
297
        $this->setId($currentId);
298
299 66
        return $serializedStyle;
300 21
    }
301 21
302 66
    /**
303 3
     * Merges the current style with the given style, using the given style as a base. This means that:
304 3
     *   - 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 66
     *   - 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
342
        return $mergedStyle;
343
    }
344
}
345