Completed
Push — develop ( cdbf33...653adf )
by Adrien
26:38
created

Font::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 2
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 3
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
7
class Font extends Supervisor
8
{
9
    // Underline types
10
    const UNDERLINE_NONE = 'none';
11
    const UNDERLINE_DOUBLE = 'double';
12
    const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
13
    const UNDERLINE_SINGLE = 'single';
14
    const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
15
16
    /**
17
     * Font Name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'Calibri';
22
23
    /**
24
     * Font Size.
25
     *
26
     * @var float
27
     */
28
    protected $size = 11;
29
30
    /**
31
     * Bold.
32
     *
33
     * @var bool
34
     */
35
    protected $bold = false;
36
37
    /**
38
     * Italic.
39
     *
40
     * @var bool
41
     */
42
    protected $italic = false;
43
44
    /**
45
     * Superscript.
46
     *
47
     * @var bool
48
     */
49
    protected $superscript = false;
50
51
    /**
52
     * Subscript.
53
     *
54
     * @var bool
55
     */
56
    protected $subscript = false;
57
58
    /**
59
     * Underline.
60
     *
61
     * @var string
62
     */
63
    protected $underline = self::UNDERLINE_NONE;
64
65
    /**
66
     * Strikethrough.
67
     *
68
     * @var bool
69
     */
70
    protected $strikethrough = false;
71
72
    /**
73
     * Foreground color.
74
     *
75
     * @var Color
76
     */
77
    protected $color;
78
79
    /**
80
     * @var int
81
     */
82
    public $colorIndex;
83
84
    /**
85
     * Create a new Font.
86
     *
87
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
88
     *                                    Leave this value at default unless you understand exactly what
89
     *                                        its ramifications are
90
     * @param bool $isConditional Flag indicating if this is a conditional style or not
91
     *                                    Leave this value at default unless you understand exactly what
92
     *                                        its ramifications are
93
     */
94 157
    public function __construct($isSupervisor = false, $isConditional = false)
95
    {
96
        // Supervisor?
97 157
        parent::__construct($isSupervisor);
98
99
        // Initialise values
100 157
        if ($isConditional) {
101 3
            $this->name = null;
102 3
            $this->size = null;
103 3
            $this->bold = null;
104 3
            $this->italic = null;
105 3
            $this->superscript = null;
106 3
            $this->subscript = null;
107 3
            $this->underline = null;
108 3
            $this->strikethrough = null;
109 3
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
110
        } else {
111 157
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
112
        }
113
        // bind parent if we are a supervisor
114 157
        if ($isSupervisor) {
115 157
            $this->color->bindParent($this, 'color');
0 ignored issues
show
Bug introduced by
$this of type PhpOffice\PhpSpreadsheet\Style\Font is incompatible with the type PhpOffice\PhpSpreadsheet\Style\Style expected by parameter $parent of PhpOffice\PhpSpreadsheet...upervisor::bindParent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
            $this->color->bindParent(/** @scrutinizer ignore-type */ $this, 'color');
Loading history...
116
        }
117 157
    }
118
119
    /**
120
     * Get the shared style component for the currently active cell in currently active sheet.
121
     * Only used for style supervisor.
122
     *
123
     * @return Font
124
     */
125
    public function getSharedComponent()
126
    {
127
        return $this->parent->getSharedComponent()->getFont();
128
    }
129
130
    /**
131
     * Build style array from subcomponents.
132
     *
133
     * @param array $array
134
     *
135
     * @return array
136
     */
137 19
    public function getStyleArray($array)
138
    {
139 19
        return ['font' => $array];
140
    }
141
142
    /**
143
     * Apply styles from array.
144
     * <code>
145
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
146
     *        array(
147
     *            'name'      => 'Arial',
148
     *            'bold'      => TRUE,
149
     *            'italic'    => FALSE,
150
     *            'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
151
     *            'strikethrough'    => FALSE,
152
     *            'color'     => array(
153
     *                'rgb' => '808080'
154
     *            )
155
     *        )
156
     * );
157
     * </code>.
158
     *
159
     * @param array $pStyles Array containing style information
160
     *
161
     * @throws PhpSpreadsheetException
162
     *
163
     * @return Font
164
     */
165 24
    public function applyFromArray(array $pStyles)
166
    {
167 24
        if ($this->isSupervisor) {
168
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
169
        } else {
170 24
            if (isset($pStyles['name'])) {
171 16
                $this->setName($pStyles['name']);
172
            }
173 24
            if (isset($pStyles['bold'])) {
174 23
                $this->setBold($pStyles['bold']);
175
            }
176 24
            if (isset($pStyles['italic'])) {
177 5
                $this->setItalic($pStyles['italic']);
178
            }
179 24
            if (isset($pStyles['superscript'])) {
180 1
                $this->setSuperscript($pStyles['superscript']);
181
            }
182 24
            if (isset($pStyles['subscript'])) {
183 1
                $this->setSubscript($pStyles['subscript']);
184
            }
185 24
            if (isset($pStyles['underline'])) {
186 16
                $this->setUnderline($pStyles['underline']);
187
            }
188 24
            if (isset($pStyles['strikethrough'])) {
189 1
                $this->setStrikethrough($pStyles['strikethrough']);
190
            }
191 24
            if (isset($pStyles['color'])) {
192 17
                $this->getColor()->applyFromArray($pStyles['color']);
193
            }
194 24
            if (isset($pStyles['size'])) {
195 16
                $this->setSize($pStyles['size']);
196
            }
197
        }
198
199 24
        return $this;
200
    }
201
202
    /**
203
     * Get Name.
204
     *
205
     * @return string
206
     */
207 80
    public function getName()
208
    {
209 80
        if ($this->isSupervisor) {
210
            return $this->getSharedComponent()->getName();
211
        }
212
213 80
        return $this->name;
214
    }
215
216
    /**
217
     * Set Name.
218
     *
219
     * @param string $pValue
220
     *
221
     * @return Font
222
     */
223 51
    public function setName($pValue)
224
    {
225 51
        if ($pValue == '') {
226 1
            $pValue = 'Calibri';
227
        }
228 51
        if ($this->isSupervisor) {
229 13
            $styleArray = $this->getStyleArray(['name' => $pValue]);
230 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
231
        } else {
232 51
            $this->name = $pValue;
233
        }
234
235 51
        return $this;
236
    }
237
238
    /**
239
     * Get Size.
240
     *
241
     * @return float
242
     */
243 80
    public function getSize()
244
    {
245 80
        if ($this->isSupervisor) {
246
            return $this->getSharedComponent()->getSize();
247
        }
248
249 80
        return $this->size;
250
    }
251
252
    /**
253
     * Set Size.
254
     *
255
     * @param float $pValue
256
     *
257
     * @return Font
258
     */
259 53
    public function setSize($pValue)
260
    {
261 53
        if ($pValue == '') {
262 1
            $pValue = 10;
263
        }
264 53
        if ($this->isSupervisor) {
265 13
            $styleArray = $this->getStyleArray(['size' => $pValue]);
266 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
267
        } else {
268 53
            $this->size = $pValue;
269
        }
270
271 53
        return $this;
272
    }
273
274
    /**
275
     * Get Bold.
276
     *
277
     * @return bool
278
     */
279 80
    public function getBold()
280
    {
281 80
        if ($this->isSupervisor) {
282
            return $this->getSharedComponent()->getBold();
283
        }
284
285 80
        return $this->bold;
286
    }
287
288
    /**
289
     * Set Bold.
290
     *
291
     * @param bool $pValue
292
     *
293
     * @return Font
294
     */
295 52
    public function setBold($pValue)
296
    {
297 52
        if ($pValue == '') {
298 12
            $pValue = false;
299
        }
300 52
        if ($this->isSupervisor) {
301 19
            $styleArray = $this->getStyleArray(['bold' => $pValue]);
302 19
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
303
        } else {
304 52
            $this->bold = $pValue;
305
        }
306
307 52
        return $this;
308
    }
309
310
    /**
311
     * Get Italic.
312
     *
313
     * @return bool
314
     */
315 80
    public function getItalic()
316
    {
317 80
        if ($this->isSupervisor) {
318
            return $this->getSharedComponent()->getItalic();
319
        }
320
321 80
        return $this->italic;
322
    }
323
324
    /**
325
     * Set Italic.
326
     *
327
     * @param bool $pValue
328
     *
329
     * @return Font
330
     */
331 34
    public function setItalic($pValue)
332
    {
333 34
        if ($pValue == '') {
334 12
            $pValue = false;
335
        }
336 34
        if ($this->isSupervisor) {
337 1
            $styleArray = $this->getStyleArray(['italic' => $pValue]);
338 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
339
        } else {
340 34
            $this->italic = $pValue;
341
        }
342
343 34
        return $this;
344
    }
345
346
    /**
347
     * Get Superscript.
348
     *
349
     * @return bool
350
     */
351 76
    public function getSuperscript()
352
    {
353 76
        if ($this->isSupervisor) {
354
            return $this->getSharedComponent()->getSuperscript();
355
        }
356
357 76
        return $this->superscript;
358
    }
359
360
    /**
361
     * Set Superscript.
362
     *
363
     * @param bool $pValue
364
     *
365
     * @return Font
366
     */
367 4
    public function setSuperscript($pValue)
368
    {
369 4
        if ($pValue == '') {
370
            $pValue = false;
371
        }
372 4
        if ($this->isSupervisor) {
373
            $styleArray = $this->getStyleArray(['superscript' => $pValue]);
374
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
375
        } else {
376 4
            $this->superscript = $pValue;
377 4
            $this->subscript = !$pValue;
378
        }
379
380 4
        return $this;
381
    }
382
383
    /**
384
     * Get Subscript.
385
     *
386
     * @return bool
387
     */
388 76
    public function getSubscript()
389
    {
390 76
        if ($this->isSupervisor) {
391
            return $this->getSharedComponent()->getSubscript();
392
        }
393
394 76
        return $this->subscript;
395
    }
396
397
    /**
398
     * Set Subscript.
399
     *
400
     * @param bool $pValue
401
     *
402
     * @return Font
403
     */
404 4
    public function setSubscript($pValue)
405
    {
406 4
        if ($pValue == '') {
407
            $pValue = false;
408
        }
409 4
        if ($this->isSupervisor) {
410
            $styleArray = $this->getStyleArray(['subscript' => $pValue]);
411
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
412
        } else {
413 4
            $this->subscript = $pValue;
414 4
            $this->superscript = !$pValue;
415
        }
416
417 4
        return $this;
418
    }
419
420
    /**
421
     * Get Underline.
422
     *
423
     * @return string
424
     */
425 80
    public function getUnderline()
426
    {
427 80
        if ($this->isSupervisor) {
428
            return $this->getSharedComponent()->getUnderline();
429
        }
430
431 80
        return $this->underline;
432
    }
433
434
    /**
435
     * Set Underline.
436
     *
437
     * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type
438
     *                                    If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
439
     *                                        false equates to UNDERLINE_NONE
440
     *
441
     * @return Font
442
     */
443 27
    public function setUnderline($pValue)
444
    {
445 27
        if (is_bool($pValue)) {
446
            $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
447 27
        } elseif ($pValue == '') {
448
            $pValue = self::UNDERLINE_NONE;
449
        }
450 27
        if ($this->isSupervisor) {
451 13
            $styleArray = $this->getStyleArray(['underline' => $pValue]);
452 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
453
        } else {
454 27
            $this->underline = $pValue;
455
        }
456
457 27
        return $this;
458
    }
459
460
    /**
461
     * Get Strikethrough.
462
     *
463
     * @return bool
464
     */
465 76
    public function getStrikethrough()
466
    {
467 76
        if ($this->isSupervisor) {
468
            return $this->getSharedComponent()->getStrikethrough();
469
        }
470
471 76
        return $this->strikethrough;
472
    }
473
474
    /**
475
     * Set Strikethrough.
476
     *
477
     * @param bool $pValue
478
     *
479
     * @return Font
480
     */
481 11
    public function setStrikethrough($pValue)
482
    {
483 11
        if ($pValue == '') {
484 10
            $pValue = false;
485
        }
486 11
        if ($this->isSupervisor) {
487
            $styleArray = $this->getStyleArray(['strike' => $pValue]);
488
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
489
        } else {
490 11
            $this->strikethrough = $pValue;
491
        }
492
493 11
        return $this;
494
    }
495
496
    /**
497
     * Get Color.
498
     *
499
     * @return Color
500
     */
501 98
    public function getColor()
502
    {
503 98
        return $this->color;
504
    }
505
506
    /**
507
     * Set Color.
508
     *
509
     * @param Color $pValue
510
     *
511
     * @throws PhpSpreadsheetException
512
     *
513
     * @return Font
514
     */
515 16
    public function setColor(Color $pValue)
516
    {
517
        // make sure parameter is a real color and not a supervisor
518 16
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
519
520 16
        if ($this->isSupervisor) {
521 1
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
522 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
523
        } else {
524 15
            $this->color = $color;
525
        }
526
527 16
        return $this;
528
    }
529
530
    /**
531
     * Get hash code.
532
     *
533
     * @return string Hash code
534
     */
535 86
    public function getHashCode()
536
    {
537 86
        if ($this->isSupervisor) {
538
            return $this->getSharedComponent()->getHashCode();
539
        }
540
541 86
        return md5(
542 86
            $this->name .
543 86
            $this->size .
544 86
            ($this->bold ? 't' : 'f') .
545 86
            ($this->italic ? 't' : 'f') .
546 86
            ($this->superscript ? 't' : 'f') .
547 86
            ($this->subscript ? 't' : 'f') .
548 86
            $this->underline .
549 86
            ($this->strikethrough ? 't' : 'f') .
550 86
            $this->color->getHashCode() .
551 86
            __CLASS__
552
        );
553
    }
554
}
555