Font::asXML()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 9
nc 64
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eclipxe\XlsxExporter\Styles;
6
7
use Eclipxe\XlsxExporter\Exceptions\InvalidPropertyValueException;
8
use Eclipxe\XlsxExporter\Utils\OpenXmlColor;
0 ignored issues
show
Bug introduced by
The type Eclipxe\XlsxExporter\Utils\OpenXmlColor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * @property string $name Font name
12
 * @property int $size Font size (pt)
13
 * @property bool $bold Font is bold
14
 * @property bool $italic Font is italic
15
 * @property bool $strike Font is stricken
16
 * @property string $underline Underline value (use constants)
17
 * @property bool $wordwrap Font is word wrapped
18
 * @property string $color Font color
19
 */
20
class Font extends AbstractStyle
21
{
22
    public const UNDERLINE_NONE = 'none';
23
24
    public const UNDERLINE_DOUBLE = 'double';
25
26
    public const UNDERLINE_SINGLE = 'single';
27
28
    private const UNDERLINE_VALUES = [
29
        self::UNDERLINE_NONE,
30
        self::UNDERLINE_SINGLE,
31
        self::UNDERLINE_DOUBLE,
32
    ];
33
34 27
    protected function properties(): array
35
    {
36 27
        return [
37 27
            'name',
38 27
            'size',
39 27
            'bold',
40 27
            'italic',
41 27
            'strike',
42 27
            'underline',
43 27
            'wordwrap',
44 27
            'color',
45 27
        ];
46
    }
47
48 2
    public function asXML(): string
49
    {
50
        // According to http://msdn.microsoft.com/en-us/library/ff531499%28v=office.12%29.aspx
51
        // Excel requires the child elements to be in the following sequence:
52
        // b, i, strike, condense, extend, outline, shadow, u, vertAlign, sz, color, name, family, charset, scheme
53 2
        return /** @lang text */ '<font>'
54 2
            . '<b val="' . (($this->bold) ? '1' : '0') . '" />'
55 2
            . '<i val="' . (($this->italic) ? '1' : '0') . '" />'
56 2
            . '<strike val="' . (($this->strike) ? '1' : '0') . '" />'
57 2
            . '<u val="' . ($this->underline ?: static::UNDERLINE_NONE) . '" />'
58 2
            . (($this->size) ? '<sz val="' . $this->size . '"/>' : '')
59 2
            . (($this->color) ? '<color rgb="' . $this->color . '"/>' : '')
60 2
            . (($this->name) ? '<name val="' . $this->name . '"/>' : '')
61 2
            . '</font>'
62 2
        ;
63
    }
64
65
    /** @param scalar|null $value */
66 21
    protected function castName($value): ?string
67
    {
68 21
        $value = trim((string) $value);
69 21
        return ('' !== $value) ? $value : null;
70
    }
71
72
    /** @param scalar|null $value */
73 20
    protected function castSize($value): int
74
    {
75 20
        return max(6, (int) $value);
76
    }
77
78
    /** @param scalar|null $value */
79 20
    protected function castBold($value): bool
80
    {
81 20
        return (bool) $value;
82
    }
83
84
    /** @param scalar|null $value */
85
    protected function castItalic($value): bool
86
    {
87
        return (bool) $value;
88
    }
89
90
    /** @param scalar|null $value */
91
    protected function castStrike($value): bool
92
    {
93
        return (bool) $value;
94
    }
95
96
    /** @param scalar|null $value */
97
    protected function castWordwrap($value): bool
98
    {
99
        return (bool) $value;
100
    }
101
102
    /** @param scalar|null $value */
103 3
    protected function castColor($value): string
104
    {
105 3
        if (! is_string($value) && ! is_int($value)) {
106
            $value = (string) $value;
107
        }
108 3
        $color = OpenXmlColor::cast($value);
109 3
        if (false === $color) {
110 1
            throw new InvalidPropertyValueException('Invalid font color value', 'color', $value);
111
        }
112 3
        return $color;
113
    }
114
115
    /** @param scalar|null $value */
116 4
    protected function castUnderline($value): string
117
    {
118 4
        $value = (string) $value;
119 4
        if (! in_array($value, self::UNDERLINE_VALUES, true)) {
120
            throw new InvalidPropertyValueException('Invalid font underline value', 'underline', $value);
121
        }
122 4
        return $value;
123
    }
124
}
125