Passed
Push — master ( 689ec5...45f08c )
by Sebastian
05:26
created

StyleOptions::isTrailingSemicolonEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\StyleCollection;
6
7
use AppUtils\Interface_Optionable;
8
use AppUtils\StyleCollection;
9
use AppUtils\Traits_Optionable;
10
11
class StyleOptions implements Interface_Optionable
12
{
13
    use Traits_Optionable;
14
15
    public const OPTION_INDENT_LEVEL = 'indent';
16
    public const OPTION_NEWLINES = 'newlines';
17
    public const OPTION_INDENT_CHAR = 'indent-char';
18
    public const OPTION_SPACE_BEFORE_VALUE = 'space-before-value';
19
    public const OPTION_TRAILING_SEMICOLON = 'trailing-semicolon';
20
    public const OPTION_SORTING = 'sorting';
21
22
    public function getDefaultOptions() : array
23
    {
24
        return array(
25
            self::OPTION_INDENT_LEVEL => 0,
26
            self::OPTION_INDENT_CHAR => '    ',
27
            self::OPTION_NEWLINES => false,
28
            self::OPTION_SPACE_BEFORE_VALUE => false,
29
            self::OPTION_TRAILING_SEMICOLON => false,
30
            self::OPTION_SORTING => true
31
        );
32
    }
33
34
    public function enableSorting(bool $enabled=true) : StyleOptions
35
    {
36
        return $this->setOption(self::OPTION_SORTING, $enabled);
37
    }
38
39
    public function enableSpaceBeforeValue(bool $enabled=true) : StyleOptions
40
    {
41
        return $this->setOption(self::OPTION_SPACE_BEFORE_VALUE, $enabled);
42
    }
43
44
    /**
45
     * @param bool $enabled
46
     * @return $this
47
     */
48
    public function enableNewline(bool $enabled=true) : StyleOptions
49
    {
50
        return $this->setOption(self::OPTION_NEWLINES, $enabled);
51
    }
52
53
    /**
54
     * Sets the indentation level to use to indent
55
     * the styles.
56
     *
57
     * NOTE: Enabling this automatically enables newlines.
58
     *
59
     * @param int $level `0` = Off. For each level above `0`, prepends the indent character to each style.
60
     * @return StyleOptions
61
     */
62
    public function setIndentLevel(int $level) : StyleOptions
63
    {
64
        return $this->setOption(self::OPTION_INDENT_LEVEL, $level);
65
    }
66
67
    /**
68
     * Sets the character/string to use for indentation.
69
     *
70
     * Default is to use 4 spaces. To use tabs, there is
71
     * the method {@see StyleCollection::setIndentCharTab()}
72
     *
73
     * @param string $char
74
     * @return StyleOptions
75
     */
76
    public function setIndentChar(string $char) : StyleOptions
77
    {
78
        return $this->setOption(self::OPTION_INDENT_CHAR, $char);
79
    }
80
81
    public function setIndentCharTab() : StyleOptions
82
    {
83
        return $this->setIndentChar("\t");
84
    }
85
86
    public function isSpaceBeforeValueEnabled() : bool
87
    {
88
        return $this->getBoolOption(self::OPTION_SPACE_BEFORE_VALUE);
89
    }
90
91
    public function isSortingEnabled() : bool
92
    {
93
        return $this->getBoolOption(self::OPTION_SORTING);
94
    }
95
96
    public function isIndentEnabled() : bool
97
    {
98
        return $this->getIndentLevel() > 0;
99
    }
100
101
    public function isNewlineEnabled() : bool
102
    {
103
        return $this->isIndentEnabled() || $this->getBoolOption(self::OPTION_NEWLINES);
104
    }
105
106
    public function isTrailingSemicolonEnabled() : bool
107
    {
108
        return $this->getBoolOption(self::OPTION_TRAILING_SEMICOLON);
109
    }
110
111
    public function getIndentLevel() : int
112
    {
113
        return $this->getIntOption(self::OPTION_INDENT_LEVEL);
114
    }
115
116
    public function getIndentChar() : string
117
    {
118
        return $this->getStringOption(self::OPTION_INDENT_CHAR);
119
    }
120
121
    public function enableTrailingSemicolon(bool $enabled=true) : StyleOptions
122
    {
123
        return $this->setOption(self::OPTION_TRAILING_SEMICOLON, $enabled);
124
    }
125
126
    public function configureForStylesheet() : StyleOptions
127
    {
128
        return $this
129
            ->enableSpaceBeforeValue()
130
            ->enableNewline()
131
            ->enableTrailingSemicolon()
132
            ->setIndentLevel(1);
133
    }
134
135
    public function configureForInline() : StyleOptions
136
    {
137
        return $this
138
            ->enableSpaceBeforeValue(false)
139
            ->enableNewline(false)
140
            ->enableTrailingSemicolon(false)
141
            ->setIndentLevel(0);
142
    }
143
}
144