Passed
Branch master (7e2aaa)
by Revin
02:14
created

Icon::ul()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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