Passed
Push — master ( 6f581c...a35795 )
by Revin
02:43
created

Icon::rotate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 9.8666
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Icon.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\fontawesome\component;
9
10
use rmrevin\yii\fontawesome\FontAwesome;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
use yii\helpers\Html;
14
15
/**
16
 * Class Icon
17
 * @package rmrevin\yii\fontawesome\component
18
 */
19
class Icon
20
{
21
    /**
22
     * @var array
23
     */
24
    private $options = [];
25
26
    /**
27
     * @param string $cssPrefix
28
     * @param string $name
29
     * @param array $options
30
     */
31
    public function __construct($cssPrefix, $name, $options = [])
32
    {
33
        Html::addCssClass($options, $cssPrefix);
34
35
        if (!empty($name)) {
36
            Html::addCssClass($options, FontAwesome::$basePrefix . '-' . $name);
37
        }
38
39
        $this->options = $options;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function __toString()
46
    {
47
        $options = $this->options;
48
49
        $tag = ArrayHelper::remove($options, 'tag', 'i');
50
51
        return Html::tag($tag, null, $options);
52
    }
53
54
    /**
55
     * @return \rmrevin\yii\fontawesome\component\Icon
56
     * @throws \yii\base\InvalidConfigException
57
     */
58
    public function inverse()
59
    {
60
        return $this->addCssClass(FontAwesome::$basePrefix . '-inverse');
61
    }
62
63
    /**
64
     * @return \rmrevin\yii\fontawesome\component\Icon
65
     * @throws \yii\base\InvalidConfigException
66
     */
67
    public function spin()
68
    {
69
        return $this->addCssClass(FontAwesome::$basePrefix . '-spin');
70
    }
71
72
    /**
73
     * @return \rmrevin\yii\fontawesome\component\Icon
74
     * @throws \yii\base\InvalidConfigException
75
     */
76
    public function pulse()
77
    {
78
        return $this->addCssClass(FontAwesome::$basePrefix . '-pulse');
79
    }
80
81
    /**
82
     * @return \rmrevin\yii\fontawesome\component\Icon
83
     * @throws \yii\base\InvalidConfigException
84
     */
85
    public function fixedWidth()
86
    {
87
        return $this->addCssClass(FontAwesome::$basePrefix . '-fw');
88
    }
89
90
    /**
91
     * @return \rmrevin\yii\fontawesome\component\Icon
92
     * @throws \yii\base\InvalidConfigException
93
     */
94
    public function li()
95
    {
96
        return $this->addCssClass(FontAwesome::$basePrefix . '-li');
97
    }
98
99
    /**
100
     * @return \rmrevin\yii\fontawesome\component\Icon
101
     * @throws \yii\base\InvalidConfigException
102
     */
103
    public function border()
104
    {
105
        return $this->addCssClass(FontAwesome::$basePrefix . '-border');
106
    }
107
108
    /**
109
     * @return \rmrevin\yii\fontawesome\component\Icon
110
     * @throws \yii\base\InvalidConfigException
111
     */
112
    public function pullLeft()
113
    {
114
        return $this->addCssClass(FontAwesome::$basePrefix . '-pull-left');
115
    }
116
117
    /**
118
     * @return \rmrevin\yii\fontawesome\component\Icon
119
     * @throws \yii\base\InvalidConfigException
120
     */
121
    public function pullRight()
122
    {
123
        return $this->addCssClass(FontAwesome::$basePrefix . '-pull-right');
124
    }
125
126
    /**
127
     * @param string $value
128
     * @return \rmrevin\yii\fontawesome\component\Icon
129
     * @throws \yii\base\InvalidConfigException
130
     */
131
    public function size($value)
132
    {
133
        return $this->addCssClass(
134
            FontAwesome::$basePrefix . '-' . $value,
135
            in_array((string)$value, [
136
                FontAwesome::SIZE_LARGE,
137
                FontAwesome::SIZE_2X,
138
                FontAwesome::SIZE_3X,
139
                FontAwesome::SIZE_4X,
140
                FontAwesome::SIZE_5X,
141
            ], true),
142
            sprintf(
143
                '%s - invalid value. Use one of the constants: %s.',
144
                'FontAwesome::size()',
145
                'FontAwesome::SIZE_LARGE, FontAwesome::SIZE_2X, FontAwesome::SIZE_3X, FontAwesome::SIZE_4X, FontAwesome::SIZE_5X'
146
            )
147
        );
148
    }
149
150
    /**
151
     * @param string $value
152
     * @return \rmrevin\yii\fontawesome\component\Icon
153
     * @throws \yii\base\InvalidConfigException
154
     */
155 View Code Duplication
    public function rotate($value)
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...
156
    {
157
        return $this->addCssClass(
158
            FontAwesome::$basePrefix . '-rotate-' . $value,
159
            in_array((string)$value, [FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270], true),
160
            sprintf(
161
                '%s - invalid value. Use one of the constants: %s.',
162
                'FontAwesome::rotate()',
163
                'FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270'
164
            )
165
        );
166
    }
167
168
    /**
169
     * @param string $value
170
     * @return \rmrevin\yii\fontawesome\component\Icon
171
     * @throws \yii\base\InvalidConfigException
172
     */
173 View Code Duplication
    public function flip($value)
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...
174
    {
175
        return $this->addCssClass(
176
            FontAwesome::$basePrefix . '-flip-' . $value,
177
            in_array((string)$value, [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL], true),
178
            sprintf(
179
                '%s - invalid value. Use one of the constants: %s.',
180
                'FontAwesome::flip()',
181
                'FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL'
182
            )
183
        );
184
    }
185
186
    /**
187
     * @param string $class
188
     * @param bool $condition
189
     * @param string|bool $throw
190
     * @return \rmrevin\yii\fontawesome\component\Icon
191
     * @throws \yii\base\InvalidConfigException
192
     * @codeCoverageIgnore
193
     */
194
    public function addCssClass($class, $condition = true, $throw = false)
195
    {
196
        if ($condition === false) {
197
            if (!empty($throw)) {
198
                $message = !is_string($throw)
199
                    ? 'Condition is false'
200
                    : $throw;
201
202
                throw new InvalidConfigException($message);
203
            }
204
        } else {
205
            Html::addCssClass($this->options, $class);
206
        }
207
208
        return $this;
209
    }
210
}
211