Completed
Push — develop ( 3560f1...5fce89 )
by Adrien
21:58 queued 15:06
created

Font::getBold()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class Font extends Supervisor implements \PhpOffice\PhpSpreadsheet\IComparable
28
{
29
    /* Underline types */
30
    const UNDERLINE_NONE = 'none';
31
    const UNDERLINE_DOUBLE = 'double';
32
    const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
33
    const UNDERLINE_SINGLE = 'single';
34
    const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
35
36
    /**
37
     * Font Name.
38
     *
39
     * @var string
40
     */
41
    protected $name = 'Calibri';
42
43
    /**
44
     * Font Size.
45
     *
46
     * @var float
47
     */
48
    protected $size = 11;
49
50
    /**
51
     * Bold.
52
     *
53
     * @var bool
54
     */
55
    protected $bold = false;
56
57
    /**
58
     * Italic.
59
     *
60
     * @var bool
61
     */
62
    protected $italic = false;
63
64
    /**
65
     * Superscript.
66
     *
67
     * @var bool
68
     */
69
    protected $superScript = false;
70
71
    /**
72
     * Subscript.
73
     *
74
     * @var bool
75
     */
76
    protected $subScript = false;
77
78
    /**
79
     * Underline.
80
     *
81
     * @var string
82
     */
83
    protected $underline = self::UNDERLINE_NONE;
84
85
    /**
86
     * Strikethrough.
87
     *
88
     * @var bool
89
     */
90
    protected $strikethrough = false;
91
92
    /**
93
     * Foreground color.
94
     *
95
     * @var Color
96
     */
97
    protected $color;
98
99
    /**
100
     * Create a new Font.
101
     *
102
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
103
     *                                    Leave this value at default unless you understand exactly what
104
     *                                        its ramifications are
105
     * @param bool $isConditional Flag indicating if this is a conditional style or not
106
     *                                    Leave this value at default unless you understand exactly what
107
     *                                        its ramifications are
108
     */
109 73
    public function __construct($isSupervisor = false, $isConditional = false)
110
    {
111
        // Supervisor?
112 73
        parent::__construct($isSupervisor);
113
114
        // Initialise values
115 73
        if ($isConditional) {
116 2
            $this->name = null;
117 2
            $this->size = null;
118 2
            $this->bold = null;
119 2
            $this->italic = null;
120 2
            $this->superScript = null;
121 2
            $this->subScript = null;
122 2
            $this->underline = null;
123 2
            $this->strikethrough = null;
124 2
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
125
        } else {
126 73
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
127
        }
128
        // bind parent if we are a supervisor
129 73
        if ($isSupervisor) {
130 73
            $this->color->bindParent($this, 'color');
131
        }
132 73
    }
133
134
    /**
135
     * Get the shared style component for the currently active cell in currently active sheet.
136
     * Only used for style supervisor.
137
     *
138
     * @return Font
139
     */
140
    public function getSharedComponent()
141
    {
142
        return $this->parent->getSharedComponent()->getFont();
143
    }
144
145
    /**
146
     * Build style array from subcomponents.
147
     *
148
     * @param array $array
149
     *
150
     * @return array
151
     */
152 19
    public function getStyleArray($array)
153
    {
154 19
        return ['font' => $array];
155
    }
156
157
    /**
158
     * Apply styles from array.
159
     *
160
     * <code>
161
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
162
     *        array(
163
     *            'name'      => 'Arial',
164
     *            'bold'      => TRUE,
165
     *            'italic'    => FALSE,
166
     *            'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
167
     *            'strike'    => FALSE,
168
     *            'color'     => array(
169
     *                'rgb' => '808080'
170
     *            )
171
     *        )
172
     * );
173
     * </code>
174
     *
175
     * @param array $pStyles Array containing style information
176
     *
177
     * @throws \PhpOffice\PhpSpreadsheet\Exception
178
     *
179
     * @return Font
180
     */
181 22
    public function applyFromArray($pStyles = null)
182
    {
183 22
        if (is_array($pStyles)) {
184 22
            if ($this->isSupervisor) {
185
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
186
            } else {
187 22
                if (isset($pStyles['name'])) {
188 15
                    $this->setName($pStyles['name']);
189
                }
190 22
                if (isset($pStyles['bold'])) {
191 22
                    $this->setBold($pStyles['bold']);
192
                }
193 22
                if (isset($pStyles['italic'])) {
194 4
                    $this->setItalic($pStyles['italic']);
195
                }
196 22
                if (isset($pStyles['superScript'])) {
197 1
                    $this->setSuperScript($pStyles['superScript']);
198
                }
199 22
                if (isset($pStyles['subScript'])) {
200 1
                    $this->setSubScript($pStyles['subScript']);
201
                }
202 22
                if (isset($pStyles['underline'])) {
203 15
                    $this->setUnderline($pStyles['underline']);
204
                }
205 22
                if (isset($pStyles['strike'])) {
206 1
                    $this->setStrikethrough($pStyles['strike']);
207
                }
208 22
                if (isset($pStyles['color'])) {
209 15
                    $this->getColor()->applyFromArray($pStyles['color']);
210
                }
211 22
                if (isset($pStyles['size'])) {
212 22
                    $this->setSize($pStyles['size']);
213
                }
214
            }
215
        } else {
216
            throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid style array passed.');
217
        }
218
219 22
        return $this;
220
    }
221
222
    /**
223
     * Get Name.
224
     *
225
     * @return string
226
     */
227 64
    public function getName()
228
    {
229 64
        if ($this->isSupervisor) {
230
            return $this->getSharedComponent()->getName();
231
        }
232
233 64
        return $this->name;
234
    }
235
236
    /**
237
     * Set Name.
238
     *
239
     * @param string $pValue
240
     *
241
     * @return Font
242
     */
243 26 View Code Duplication
    public function setName($pValue = 'Calibri')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245 26
        if ($pValue == '') {
246
            $pValue = 'Calibri';
247
        }
248 26
        if ($this->isSupervisor) {
249 13
            $styleArray = $this->getStyleArray(['name' => $pValue]);
250 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
251
        } else {
252 26
            $this->name = $pValue;
253
        }
254
255 26
        return $this;
256
    }
257
258
    /**
259
     * Get Size.
260
     *
261
     * @return float
262
     */
263 64
    public function getSize()
264
    {
265 64
        if ($this->isSupervisor) {
266
            return $this->getSharedComponent()->getSize();
267
        }
268
269 64
        return $this->size;
270
    }
271
272
    /**
273
     * Set Size.
274
     *
275
     * @param float $pValue
276
     *
277
     * @return Font
278
     */
279 28 View Code Duplication
    public function setSize($pValue = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281 28
        if ($pValue == '') {
282
            $pValue = 10;
283
        }
284 28
        if ($this->isSupervisor) {
285 13
            $styleArray = $this->getStyleArray(['size' => $pValue]);
286 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
287
        } else {
288 28
            $this->size = $pValue;
0 ignored issues
show
Documentation Bug introduced by
The property $size was declared of type double, but $pValue is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
289
        }
290
291 28
        return $this;
292
    }
293
294
    /**
295
     * Get Bold.
296
     *
297
     * @return bool
298
     */
299 64
    public function getBold()
300
    {
301 64
        if ($this->isSupervisor) {
302
            return $this->getSharedComponent()->getBold();
303
        }
304
305 64
        return $this->bold;
306
    }
307
308
    /**
309
     * Set Bold.
310
     *
311
     * @param bool $pValue
312
     *
313
     * @return Font
314
     */
315 32 View Code Duplication
    public function setBold($pValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317 32
        if ($pValue == '') {
318 7
            $pValue = false;
319
        }
320 32
        if ($this->isSupervisor) {
321 19
            $styleArray = $this->getStyleArray(['bold' => $pValue]);
322 19
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
323
        } else {
324 32
            $this->bold = $pValue;
325
        }
326
327 32
        return $this;
328
    }
329
330
    /**
331
     * Get Italic.
332
     *
333
     * @return bool
334
     */
335 64
    public function getItalic()
336
    {
337 64
        if ($this->isSupervisor) {
338
            return $this->getSharedComponent()->getItalic();
339
        }
340
341 64
        return $this->italic;
342
    }
343
344
    /**
345
     * Set Italic.
346
     *
347
     * @param bool $pValue
348
     *
349
     * @return Font
350
     */
351 26 View Code Duplication
    public function setItalic($pValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
352
    {
353 26
        if ($pValue == '') {
354 7
            $pValue = false;
355
        }
356 26
        if ($this->isSupervisor) {
357 1
            $styleArray = $this->getStyleArray(['italic' => $pValue]);
358 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
359
        } else {
360 26
            $this->italic = $pValue;
361
        }
362
363 26
        return $this;
364
    }
365
366
    /**
367
     * Get SuperScript.
368
     *
369
     * @return bool
370
     */
371 62
    public function getSuperScript()
372
    {
373 62
        if ($this->isSupervisor) {
374
            return $this->getSharedComponent()->getSuperScript();
375
        }
376
377 62
        return $this->superScript;
378
    }
379
380
    /**
381
     * Set SuperScript.
382
     *
383
     * @param bool $pValue
384
     *
385
     * @return Font
386
     */
387 3 View Code Duplication
    public function setSuperScript($pValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
388
    {
389 3
        if ($pValue == '') {
390
            $pValue = false;
391
        }
392 3
        if ($this->isSupervisor) {
393
            $styleArray = $this->getStyleArray(['superScript' => $pValue]);
394
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
395
        } else {
396 3
            $this->superScript = $pValue;
397 3
            $this->subScript = !$pValue;
398
        }
399
400 3
        return $this;
401
    }
402
403
    /**
404
     * Get SubScript.
405
     *
406
     * @return bool
407
     */
408 62
    public function getSubScript()
409
    {
410 62
        if ($this->isSupervisor) {
411
            return $this->getSharedComponent()->getSubScript();
412
        }
413
414 62
        return $this->subScript;
415
    }
416
417
    /**
418
     * Set SubScript.
419
     *
420
     * @param bool $pValue
421
     *
422
     * @return Font
423
     */
424 3 View Code Duplication
    public function setSubScript($pValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
425
    {
426 3
        if ($pValue == '') {
427
            $pValue = false;
428
        }
429 3
        if ($this->isSupervisor) {
430
            $styleArray = $this->getStyleArray(['subScript' => $pValue]);
431
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
432
        } else {
433 3
            $this->subScript = $pValue;
434 3
            $this->superScript = !$pValue;
435
        }
436
437 3
        return $this;
438
    }
439
440
    /**
441
     * Get Underline.
442
     *
443
     * @return string
444
     */
445 64
    public function getUnderline()
446
    {
447 64
        if ($this->isSupervisor) {
448
            return $this->getSharedComponent()->getUnderline();
449
        }
450
451 64
        return $this->underline;
452
    }
453
454
    /**
455
     * Set Underline.
456
     *
457
     * @param string|bool $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type
458
     *                                    If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
459
     *                                        false equates to UNDERLINE_NONE
460
     *
461
     * @return Font
462
     */
463 21 View Code Duplication
    public function setUnderline($pValue = self::UNDERLINE_NONE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
464
    {
465 21
        if (is_bool($pValue)) {
466
            $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
467 21
        } elseif ($pValue == '') {
468
            $pValue = self::UNDERLINE_NONE;
469
        }
470 21
        if ($this->isSupervisor) {
471 13
            $styleArray = $this->getStyleArray(['underline' => $pValue]);
472 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
473
        } else {
474 21
            $this->underline = $pValue;
475
        }
476
477 21
        return $this;
478
    }
479
480
    /**
481
     * Get Strikethrough.
482
     *
483
     * @return bool
484
     */
485 62
    public function getStrikethrough()
486
    {
487 62
        if ($this->isSupervisor) {
488
            return $this->getSharedComponent()->getStrikethrough();
489
        }
490
491 62
        return $this->strikethrough;
492
    }
493
494
    /**
495
     * Set Strikethrough.
496
     *
497
     * @param bool $pValue
498
     *
499
     * @return Font
500
     */
501 7 View Code Duplication
    public function setStrikethrough($pValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
502
    {
503 7
        if ($pValue == '') {
504 6
            $pValue = false;
505
        }
506 7
        if ($this->isSupervisor) {
507
            $styleArray = $this->getStyleArray(['strike' => $pValue]);
508
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
509
        } else {
510 7
            $this->strikethrough = $pValue;
511
        }
512
513 7
        return $this;
514
    }
515
516
    /**
517
     * Get Color.
518
     *
519
     * @return Color
520
     */
521 64
    public function getColor()
522
    {
523 64
        return $this->color;
524
    }
525
526
    /**
527
     * Set Color.
528
     *
529
     * @param Color $pValue
530
     *
531
     * @throws \PhpOffice\PhpSpreadsheet\Exception
532
     *
533
     * @return Font
534
     */
535 16 View Code Duplication
    public function setColor(Color $pValue = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
536
    {
537
        // make sure parameter is a real color and not a supervisor
538 16
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
0 ignored issues
show
Bug introduced by
It seems like $pValue is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
539
540 16
        if ($this->isSupervisor) {
541 1
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
542 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
543
        } else {
544 15
            $this->color = $color;
545
        }
546
547 16
        return $this;
548
    }
549
550
    /**
551
     * Get hash code.
552
     *
553
     * @return string Hash code
554
     */
555 70
    public function getHashCode()
556
    {
557 70
        if ($this->isSupervisor) {
558
            return $this->getSharedComponent()->getHashCode();
559
        }
560
561 70
        return md5(
562 70
            $this->name .
563 70
            $this->size .
564 70
            ($this->bold ? 't' : 'f') .
565 70
            ($this->italic ? 't' : 'f') .
566 70
            ($this->superScript ? 't' : 'f') .
567 70
            ($this->subScript ? 't' : 'f') .
568 70
            $this->underline .
569 70
            ($this->strikethrough ? 't' : 'f') .
570 70
            $this->color->getHashCode() .
571 70
            __CLASS__
572
        );
573
    }
574
}
575