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