Completed
Push — master ( 759093...8330b4 )
by Revin
01:29
created

Icon::pulse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
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
        $values = [
134
            FontAwesome::SIZE_LG,
135
            FontAwesome::SIZE_SM,
136
            FontAwesome::SIZE_XS,
137
            FontAwesome::SIZE_2X,
138
            FontAwesome::SIZE_3X,
139
            FontAwesome::SIZE_4X,
140
            FontAwesome::SIZE_5X,
141
            FontAwesome::SIZE_6X,
142
            FontAwesome::SIZE_7X,
143
            FontAwesome::SIZE_8X,
144
            FontAwesome::SIZE_9X,
145
            FontAwesome::SIZE_10X,
146
        ];
147
148
        return $this->addCssClass(
149
            FontAwesome::$basePrefix . '-' . $value,
150
            in_array((string)$value, $values, true),
151
            sprintf(
152
                '%s - invalid value. Use one of the constants: %s.',
153
                'FontAwesome::size()',
154
                implode(', ', $values)
155
            )
156
        );
157
    }
158
159
    /**
160
     * @param string $value
161
     * @return \rmrevin\yii\fontawesome\component\Icon
162
     * @throws \yii\base\InvalidConfigException
163
     */
164 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...
165
    {
166
        $values = [FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270];
167
168
        return $this->addCssClass(
169
            FontAwesome::$basePrefix . '-rotate-' . $value,
170
            in_array((string)$value, $values, true),
171
            sprintf(
172
                '%s - invalid value. Use one of the constants: %s.',
173
                'FontAwesome::rotate()',
174
                implode(', ', $values)
175
            )
176
        );
177
    }
178
179
    /**
180
     * @param string $value
181
     * @return \rmrevin\yii\fontawesome\component\Icon
182
     * @throws \yii\base\InvalidConfigException
183
     */
184 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...
185
    {
186
        $values = [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL];
187
188
        return $this->addCssClass(
189
            FontAwesome::$basePrefix . '-flip-' . $value,
190
            in_array((string)$value, [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL], true),
191
            sprintf(
192
                '%s - invalid value. Use one of the constants: %s.',
193
                'FontAwesome::flip()',
194
                implode(', ', $values)
195
            )
196
        );
197
    }
198
199
    /**
200
     * @param string $class
201
     * @param bool $condition
202
     * @param string|bool $throw
203
     * @return \rmrevin\yii\fontawesome\component\Icon
204
     * @throws \yii\base\InvalidConfigException
205
     * @codeCoverageIgnore
206
     */
207
    public function addCssClass($class, $condition = true, $throw = false)
208
    {
209
        if ($condition === false) {
210
            if (!empty($throw)) {
211
                $message = !is_string($throw)
212
                    ? 'Condition is false'
213
                    : $throw;
214
215
                throw new InvalidConfigException($message);
216
            }
217
        } else {
218
            Html::addCssClass($this->options, $class);
219
        }
220
221
        return $this;
222
    }
223
}
224