Completed
Pull Request — master (#24)
by Lukyanov
04:02 queued 51s
created

Icon::flip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 12
loc 12
rs 9.4285
ccs 9
cts 9
cp 1
cc 1
eloc 8
nc 1
nop 1
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\FA;
11
use yii\helpers\Html;
12
13
/**
14
 * Class Icon
15
 * @package rmrevin\yii\fontawesome\component
16
 */
17
class Icon
18
{
19
20
    /**
21
     * @var string
22
     */
23
    public static $defaultTag = 'i';
24
25
    /**
26
     * @var string
27
     */
28
    private $tag;
29
30
    /**
31
     * @var array
32
     */
33
    private $options = [];
34
35
    /**
36
     * @param string $name
37
     * @param array $options
38
     */
39 10
    public function __construct($name, $options = [])
40
    {
41 10
        Html::addCssClass($options, FA::$cssPrefix);
42 10
        if (!empty($name)) {
43 10
            Html::addCssClass($options, FA::$cssPrefix . '-' . $name);
44 10
        }
45
46 10
        $this->options = $options;
47 10
    }
48
49
    /**
50
     * @return string
51
     */
52 6
    public function __toString()
53
    {
54 6
        return $this->render();
55
    }
56
57
    /**
58
     * @return self
59
     */
60 1
    public function inverse()
61
    {
62 1
        return $this->addCssClass(FA::$cssPrefix . '-inverse');
63
    }
64
65
    /**
66
     * @return self
67
     */
68 3
    public function spin()
69
    {
70 3
        return $this->addCssClass(FA::$cssPrefix . '-spin');
71
    }
72
73
    /**
74
     * @deprecated
75
     * @return self
76
     */
77 1
    public function fixed_width()
78
    {
79 1
        \Yii::warning(sprintf('You are using an deprecated method `%s`.', 'fixed_width'), __METHOD__);
80
81 1
        return $this->fixedWidth();
82
    }
83
84
    /**
85
     * @return self
86
     */
87 1
    public function fixedWidth()
88
    {
89 1
        return $this->addCssClass(FA::$cssPrefix . '-fw');
90
    }
91
92
    /**
93
     * @deprecated
94
     * @return self
95
     */
96 1
    public function ul()
97
    {
98 1
        return $this->addCssClass(FA::$cssPrefix . '-ul');
99
    }
100
101
    /**
102
     * @return self
103
     */
104 3
    public function li()
105
    {
106 3
        return $this->addCssClass(FA::$cssPrefix . '-li');
107
    }
108
109
    /**
110
     * @return self
111
     */
112 1
    public function border()
113
    {
114 1
        return $this->addCssClass(FA::$cssPrefix . '-border');
115
    }
116
117
    /**
118
     * @deprecated
119
     * @return self
120
     */
121
    public function pull_left()
122
    {
123
        \Yii::warning(sprintf('You are using an deprecated method `%s`.', 'pull_left'), __METHOD__);
124
125
        return $this->pullLeft();
126
    }
127
128
    /**
129
     * @return self
130
     */
131 1
    public function pullLeft()
132
    {
133 1
        return $this->addCssClass('pull-left');
134
    }
135
136
    /**
137
     * @deprecated
138
     * @return self
139
     */
140
    public function pull_right()
141
    {
142
        \Yii::warning(sprintf('You are using an deprecated method `%s`.', 'pull_right'), __METHOD__);
143
144
        return $this->pullRight();
145
    }
146
147
    /**
148
     * @return self
149
     */
150
    public function pullRight()
151
    {
152
        return $this->addCssClass('pull-right');
153
    }
154
155
    /**
156
     * @param string $value
157
     * @return self
158
     * @throws \yii\base\InvalidConfigException
159
     */
160 3
    public function size($value)
161
    {
162 3
        return $this->addCssClass(
163 3
            FA::$cssPrefix . '-' . $value,
164 3
            in_array((string)$value, [FA::SIZE_LARGE, FA::SIZE_2X, FA::SIZE_3X, FA::SIZE_4X, FA::SIZE_5X], true),
165 3
            sprintf(
166 3
                '%s - invalid value. Use one of the constants: %s.',
167 3
                'FA::size()',
168
                'FA::SIZE_LARGE, FA::SIZE_2X, FA::SIZE_3X, FA::SIZE_4X, FA::SIZE_5X'
169 3
            )
170 3
        );
171
    }
172
173
    /**
174
     * @param string $value
175
     * @return self
176
     * @throws \yii\base\InvalidConfigException
177
     */
178 1 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...
179
    {
180 1
        return $this->addCssClass(
181 1
            FA::$cssPrefix . '-rotate-' . $value,
182 1
            in_array((string)$value, [FA::ROTATE_90, FA::ROTATE_180, FA::ROTATE_270], true),
183 1
            sprintf(
184 1
                '%s - invalid value. Use one of the constants: %s.',
185 1
                'FA::rotate()',
186
                'FA::ROTATE_90, FA::ROTATE_180, FA::ROTATE_270'
187 1
            )
188 1
        );
189
    }
190
191
    /**
192
     * @param string $value
193
     * @return self
194
     * @throws \yii\base\InvalidConfigException
195
     */
196 1 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...
197
    {
198 1
        return $this->addCssClass(
199 1
            FA::$cssPrefix . '-flip-' . $value,
200 1
            in_array((string)$value, [FA::FLIP_HORIZONTAL, FA::FLIP_VERTICAL], true),
201 1
            sprintf(
202 1
                '%s - invalid value. Use one of the constants: %s.',
203 1
                'FA::flip()',
204
                'FA::FLIP_HORIZONTAL, FA::FLIP_VERTICAL'
205 1
            )
206 1
        );
207
    }
208
209
    /**
210
     * Change html tag.
211
     * @param string $tag
212
     * @return static
213
     * @throws \yii\base\InvalidParamException
214
     */
215 2
    public function tag($tag)
216
    {
217 2
        $this->tag = $tag;
218
219 2
        return $this;
220
    }
221
222
    /**
223
     * @param string $class
224
     * @param bool $condition
225
     * @param string|bool $throw
226
     * @return \rmrevin\yii\fontawesome\component\Icon
227
     * @throws \yii\base\InvalidConfigException
228
     * @codeCoverageIgnore
229
     */
230
    public function addCssClass($class, $condition = true, $throw = false)
231
    {
232
        if ($condition === false) {
233
            if (!empty($throw)) {
234
                $message = !is_string($throw)
235
                    ? 'Condition is false'
236
                    : $throw;
237
238
                throw new \yii\base\InvalidConfigException($message);
239
            }
240
        } else {
241
            Html::addCssClass($this->options, $class);
242
        }
243
244
        return $this;
245
    }
246
247
    /**
248
     * @param string|null $tag
249
     * @param string|null $content
250
     * @param array $options
251
     * @return string
252
     */
253 6 View Code Duplication
    public function render($tag = null, $content = null, $options = [])
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...
254
    {
255 6
        $tag = empty($tag)
256 6
            ? (empty($this->tag) ? static::$defaultTag : $this->tag)
257 6
            : $tag;
258
259 6
        $options = array_merge($this->options, $options);
260
261 6
        return Html::tag($tag, $content, $options);
262
    }
263
}
264