Alignment::asXML()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 16
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 5
rs 9.6111
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
9
/**
10
 * @property string $horizontal Horizontal alignment
11
 * @property string $vertical Vertical alignment
12
 * @property bool $wraptext Wrap text
13
 */
14
class Alignment extends AbstractStyle
15
{
16
    public const HORIZONTAL_GENERAL = 'general';
17
18
    public const HORIZONTAL_LEFT = 'left';
19
20
    public const HORIZONTAL_CENTER = 'center';
21
22
    public const HORIZONTAL_RIGHT = 'right';
23
24
    public const HORIZONTAL_JUSTIFY = 'justify';
25
26
    private const HORIZONTAL_VALUES = [
27
        self::HORIZONTAL_GENERAL,
28
        self::HORIZONTAL_LEFT,
29
        self::HORIZONTAL_CENTER,
30
        self::HORIZONTAL_RIGHT,
31
        self::HORIZONTAL_JUSTIFY,
32
    ];
33
34
    public const VERTICAL_TOP = 'top';
35
36
    public const VERTICAL_BOTTOM = 'bottom';
37
38
    public const VERTICAL_CENTER = 'center';
39
40
    private const VERTICAL_VALUES = [
41
        self::VERTICAL_TOP,
42
        self::VERTICAL_CENTER,
43
        self::VERTICAL_BOTTOM,
44
    ];
45
46 28
    protected function properties(): array
47
    {
48 28
        return [
49 28
            'horizontal',
50 28
            'vertical',
51 28
            'wraptext',
52 28
        ];
53
    }
54
55 5
    public function asXML(): string
56
    {
57 5
        return '<alignment'
58 5
            . (($this->horizontal) ? ' horizontal="' . $this->horizontal . '"' : '')
59 5
            . (($this->vertical) ? ' vertical="' . $this->vertical . '"' : '')
60 5
            . ((null !== $this->wraptext) ? ' wrapText="' . (($this->wraptext) ? '1' : '0') . '"' : '')
61 5
            . '/>'
62 5
        ;
63
    }
64
65
    /** @param scalar|null $value */
66 25
    protected function castHorizontal($value): string
67
    {
68 25
        $value = (string) $value;
69 25
        if (! in_array($value, self::HORIZONTAL_VALUES, true)) {
70 1
            throw new InvalidPropertyValueException('Invalid alignment horizontal value', 'horizontal', $value);
71
        }
72 25
        return $value;
73
    }
74
75
    /** @param scalar|null $value */
76 6
    protected function castVertical($value): string
77
    {
78 6
        $value = (string) $value;
79 6
        if (! in_array($value, self::VERTICAL_VALUES, true)) {
80 1
            throw new InvalidPropertyValueException('Invalid alignment vertical value', 'vertical', $value);
81
        }
82 6
        return $value;
83
    }
84
85
    /** @param scalar|null $value */
86 2
    protected function castWraptext($value): bool
87
    {
88 2
        return (bool) $value;
89
    }
90
}
91