Passed
Push — master ( ec8de8...0b05ae )
by Revin
04:00
created

Icon::spin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 9
    public function __construct($cssPrefix, $name, $options = [])
32
    {
33 9
        Html::addCssClass($options, $cssPrefix);
34
35 9
        if (!empty($name)) {
36 9
            Html::addCssClass($options, FontAwesome::$basePrefix . '-' . $name);
37 9
        }
38
39 9
        $this->options = $options;
40 9
    }
41
42
    /**
43
     * @return string
44
     */
45 5
    public function __toString()
46
    {
47 5
        $options = $this->options;
48
49 5
        $tag = ArrayHelper::remove($options, 'tag', 'i');
50
51 5
        return Html::tag($tag, null, $options);
52
    }
53
54
    /**
55
     * @return \rmrevin\yii\fontawesome\component\Icon
56
     * @throws \yii\base\InvalidConfigException
57
     */
58 1
    public function inverse()
59
    {
60 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-inverse');
61
    }
62
63
    /**
64
     * @return \rmrevin\yii\fontawesome\component\Icon
65
     * @throws \yii\base\InvalidConfigException
66
     */
67 3
    public function spin()
68
    {
69 3
        return $this->addCssClass(FontAwesome::$basePrefix . '-spin');
70
    }
71
72
    /**
73
     * @return \rmrevin\yii\fontawesome\component\Icon
74
     * @throws \yii\base\InvalidConfigException
75
     */
76 1
    public function pulse()
77
    {
78 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-pulse');
79
    }
80
81
    /**
82
     * @return \rmrevin\yii\fontawesome\component\Icon
83
     * @throws \yii\base\InvalidConfigException
84
     */
85 1
    public function fixedWidth()
86
    {
87 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-fw');
88
    }
89
90
    /**
91
     * @return \rmrevin\yii\fontawesome\component\Icon
92
     * @throws \yii\base\InvalidConfigException
93
     */
94 3
    public function li()
95
    {
96 3
        return $this->addCssClass(FontAwesome::$basePrefix . '-li');
97
    }
98
99
    /**
100
     * @return \rmrevin\yii\fontawesome\component\Icon
101
     * @throws \yii\base\InvalidConfigException
102
     */
103 1
    public function border()
104
    {
105 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-border');
106
    }
107
108
    /**
109
     * @return \rmrevin\yii\fontawesome\component\Icon
110
     * @throws \yii\base\InvalidConfigException
111
     */
112 1
    public function pullLeft()
113
    {
114 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-pull-left');
115
    }
116
117
    /**
118
     * @return \rmrevin\yii\fontawesome\component\Icon
119
     * @throws \yii\base\InvalidConfigException
120
     */
121 1
    public function pullRight()
122
    {
123 1
        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 4
    public function size($value)
132
    {
133 4
        return $this->addCssClass(
134 4
            FontAwesome::$basePrefix . '-' . $value,
135 4
            in_array((string)$value, [
136 4
                FontAwesome::SIZE_LARGE,
137 4
                FontAwesome::SIZE_2X,
138 4
                FontAwesome::SIZE_3X,
139 4
                FontAwesome::SIZE_4X,
140 4
                FontAwesome::SIZE_5X,
141 4
            ], true),
142 4
            sprintf(
143 4
                '%s - invalid value. Use one of the constants: %s.',
144 4
                'FontAwesome::size()',
145
                'FontAwesome::SIZE_LARGE, FontAwesome::SIZE_2X, FontAwesome::SIZE_3X, FontAwesome::SIZE_4X, FontAwesome::SIZE_5X'
146 4
            )
147 4
        );
148
    }
149
150
    /**
151
     * @param string $value
152
     * @return \rmrevin\yii\fontawesome\component\Icon
153
     * @throws \yii\base\InvalidConfigException
154
     */
155 2 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 2
        return $this->addCssClass(
158 2
            FontAwesome::$basePrefix . '-rotate-' . $value,
159 2
            in_array((string)$value, [FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270], true),
160 2
            sprintf(
161 2
                '%s - invalid value. Use one of the constants: %s.',
162 2
                'FontAwesome::rotate()',
163
                'FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270'
164 2
            )
165 2
        );
166
    }
167
168
    /**
169
     * @param string $value
170
     * @return \rmrevin\yii\fontawesome\component\Icon
171
     * @throws \yii\base\InvalidConfigException
172
     */
173 2 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 2
        return $this->addCssClass(
176 2
            FontAwesome::$basePrefix . '-flip-' . $value,
177 2
            in_array((string)$value, [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL], true),
178 2
            sprintf(
179 2
                '%s - invalid value. Use one of the constants: %s.',
180 2
                'FontAwesome::flip()',
181
                'FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL'
182 2
            )
183 2
        );
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