Test Setup Failed
Pull Request — master (#32)
by
unknown
35:47
created

Icon::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * Icon.php
4
 * @author Revin Roman <[email protected]>
5
 * @author Simon Karlen <[email protected]>
6
 * @link https://rmrevin.ru
7
 */
8
9
namespace rmrevin\yii\fontawesome\component;
10
11
use rmrevin\yii\fontawesome\FA;
12
use yii\base\InvalidConfigException;
13
use yii\helpers\ArrayHelper;
14
use yii\helpers\Html;
15
16
/**
17
 * Class Icon
18
 * @package rmrevin\yii\fontawesome\component
19
 */
20
class Icon
21
{
22
    /**
23
     * @var array
24
     */
25
    private $options = [];
26
27
    /**
28
     * @var string
29
     */
30
    private $prefix = 'fas';
31
32
    /**
33
     * @param string $name
34
     * @param array $options
35
     * @param string $prefix
36
     * @throws InvalidConfigException
37
     */
38
    public function __construct($name, $options = [], $prefix = 'fas')
39
    {
40
        if (empty($name)) {
41
            throw new InvalidConfigException('property is mandatory');
42
        }
43
44
        Html::addCssClass($options, $prefix . ' ' . FA::$cssPrefix . '-' . $name);
45
46
        $this->prefix = $prefix;
47
        $this->options = $options;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function __toString()
54
    {
55
        $options = $this->options;
56
57
        $tag = ArrayHelper::remove($options, 'tag', 'i');
58
59
        if (isset($options['data']) && isset($options['data'][FA::$cssPrefix . '-transform']) &&
60
            is_array($options['data'][FA::$cssPrefix . '-transform'])) {
61
            $options['data'][FA::$cssPrefix . '-transform'] = implode(
62
                ' ',
63
                $options['data'][FA::$cssPrefix . '-transform']
64
            );
65
        }
66
67
        return Html::tag($tag, null, $options);
68
    }
69
70
    /**
71
     * @return static
72
     * @throws InvalidConfigException
73
     */
74
    public function inverse()
75
    {
76
        return $this->addCssClass(FA::$cssPrefix . '-inverse');
77
    }
78
79
    /**
80
     * @return static
81
     * @throws InvalidConfigException
82
     */
83
    public function spin()
84
    {
85
        return $this->addCssClass(FA::$cssPrefix . '-spin');
86
    }
87
88
    /**
89
     * @return static
90
     * @throws InvalidConfigException
91
     */
92
    public function pulse()
93
    {
94
        return $this->addCssClass(FA::$cssPrefix . '-pulse');
95
    }
96
97
    /**
98
     * @return static
99
     * @throws InvalidConfigException
100
     */
101
    public function fixedWidth()
102
    {
103
        return $this->addCssClass(FA::$cssPrefix . '-fw');
104
    }
105
106
    /**
107
     * @return static
108
     * @throws InvalidConfigException
109
     */
110
    public function li()
111
    {
112
        return $this->addCssClass(FA::$cssPrefix . '-li');
113
    }
114
115
    /**
116
     * @return static
117
     * @throws InvalidConfigException
118
     */
119
    public function border()
120
    {
121
        return $this->addCssClass(FA::$cssPrefix . '-border');
122
    }
123
124
    /**
125
     * @return static
126
     * @throws InvalidConfigException
127
     */
128
    public function pullLeft()
129
    {
130
        return $this->addCssClass(FA::$cssPrefix . '-pull-left');
131
    }
132
133
    /**
134
     * @return static
135
     * @throws InvalidConfigException
136
     */
137
    public function pullRight()
138
    {
139
        return $this->addCssClass(FA::$cssPrefix . '-pull-right');
140
    }
141
142
    /**
143
     * @param string $value
144
     * @return static
145
     * @throws \yii\base\InvalidConfigException
146
     */
147
    public function size($value)
148
    {
149
        return $this->addCssClass(
150
            FA::$cssPrefix . '-' . $value,
151
            in_array((string)$value, [
152
                FA::SIZE_XS,
153
                FA::SIZE_SM,
154
                FA::SIZE_LG,
155
                FA::SIZE_2X,
156
                FA::SIZE_3X,
157
                FA::SIZE_4X,
158
                FA::SIZE_5X,
159
                FA::SIZE_6X,
160
                FA::SIZE_7X,
161
                FA::SIZE_8X,
162
                FA::SIZE_9X,
163
                FA::SIZE_10X
164
            ], true),
165
            sprintf(
166
                '%s - invalid value. Use one of the constants: %s.',
167
                'FA::size()',
168
                'FA::SIZE_XS, FA::SIZE_SM, FA::SIZE_LG, FA::SIZE_2X, FA::SIZE_3X, FA::SIZE_4X, FA::SIZE_5X, FA::SIZE_6X, FA::SIZE_7X, FA::SIZE_8X, FA::SIZE_9X, FA::SIZE_10X'
169
            )
170
        );
171
    }
172
173
    /**
174
     * @param string $value
175
     * @return static
176
     * @throws \yii\base\InvalidConfigException
177
     */
178
    public function rotate($value)
179
    {
180
        return $this->addTransformation(
181
            'rotate-' . $value,
182
            in_array((int)$value, range(-359, 359), true),
183
            sprintf(
184
                '%s - invalid value. Must be in range: %s.',
185
                'FA::rotate()',
186
                '-359 through 359'
187
            )
188
        );
189
    }
190
191
    /**
192
     * @param string $value
193
     * @return static
194
     * @throws \yii\base\InvalidConfigException
195
     */
196
    public function flip($value)
197
    {
198
        return $this->addTransformation(
199
            'flip-' . $value,
200
            in_array((string)$value, [FA::FLIP_HORIZONTAL, FA::FLIP_VERTICAL], true),
201
            sprintf(
202
                '%s - invalid value. Use one of the constants: %s.',
203
                'FA::flip()',
204
                'FA::FLIP_HORIZONTAL, FA::FLIP_VERTICAL'
205
            )
206
        );
207
    }
208
209
    /**
210
     * @param integer $value
211
     * @return static
212
     * @throws \yii\base\InvalidConfigException
213
     */
214
    public function shrink($value)
215
    {
216
        return $this->addTransformation(
217
            'shrink-' . $value,
218
            is_numeric($value),
219
            sprintf(
220
                '%s - invalid value. Must be numeric.',
221
                'FA::shrink()'
222
            )
223
        );
224
    }
225
226
    /**
227
     * @param integer $value
228
     * @return static
229
     * @throws \yii\base\InvalidConfigException
230
     */
231
    public function grow($value)
232
    {
233
        return $this->addTransformation(
234
            'grow-' . $value,
235
            is_numeric($value),
236
            sprintf(
237
                '%s - invalid value. Must be numeric.',
238
                'FA::shrink()'
239
            )
240
        );
241
    }
242
243
    /**
244
     * @param integer $value
245
     * @return static
246
     * @throws \yii\base\InvalidConfigException
247
     */
248
    public function up($value)
249
    {
250
        return $this->addTransformation(
251
            'up-' . $value,
252
            is_numeric($value),
253
            sprintf(
254
                '%s - invalid value. Must be numeric.',
255
                'FA::shrink()'
256
            )
257
        );
258
    }
259
260
    /**
261
     * @param integer $value
262
     * @return static
263
     * @throws \yii\base\InvalidConfigException
264
     */
265
    public function right($value)
266
    {
267
        return $this->addTransformation(
268
            'right-' . $value,
269
            is_numeric($value),
270
            sprintf(
271
                '%s - invalid value. Must be numeric.',
272
                'FA::shrink()'
273
            )
274
        );
275
    }
276
277
    /**
278
     * @param integer $value
279
     * @return static
280
     * @throws \yii\base\InvalidConfigException
281
     */
282
    public function down($value)
283
    {
284
        return $this->addTransformation(
285
            'down-' . $value,
286
            is_numeric($value),
287
            sprintf(
288
                '%s - invalid value. Must be numeric.',
289
                'FA::shrink()'
290
            )
291
        );
292
    }
293
294
    /**
295
     * @param integer $value
296
     * @return static
297
     * @throws \yii\base\InvalidConfigException
298
     */
299
    public function left($value)
300
    {
301
        return $this->addTransformation(
302
            'left-' . $value,
303
            is_numeric($value),
304
            sprintf(
305
                '%s - invalid value. Must be numeric.',
306
                'FA::shrink()'
307
            )
308
        );
309
    }
310
311
    /**
312
     * @param $value
313
     * @return static
314
     * @throws InvalidConfigException
315
     */
316
    public function mask($value)
317
    {
318
        return $this->addMask($value, !empty($value), false);
319
    }
320
321
    /**
322
     * @param string $class
323
     * @param bool $condition
324
     * @param string|bool $throw
325
     * @return static
326
     * @throws \yii\base\InvalidConfigException
327
     * @codeCoverageIgnore
328
     */
329
    public function addCssClass($class, $condition = true, $throw = false)
330
    {
331
        if ($condition === false) {
332 View Code Duplication
            if (!empty($throw)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
333
                $message = !is_string($throw)
334
                    ? 'Condition is false'
335
                    : $throw;
336
337
                throw new \yii\base\InvalidConfigException($message);
338
            }
339
        } else {
340
            Html::addCssClass($this->options, $class);
341
        }
342
343
        return $this;
344
    }
345
346
    /**
347
     * @param string $transformation
348
     * @param bool $condition
349
     * @param string|bool $throw
350
     * @return static
351
     * @throws \yii\base\InvalidConfigException
352
     * @codeCoverageIgnore
353
     */
354
    public function addTransformation($transformation, $condition = true, $throw = false)
355
    {
356
        if ($condition === false) {
357 View Code Duplication
            if (!empty($throw)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
358
                $message = !is_string($throw)
359
                    ? 'Condition is false'
360
                    : $throw;
361
362
                throw new \yii\base\InvalidConfigException($message);
363
            }
364
        } else {
365
            $transformations = ArrayHelper::getValue($this->options, ['data', FA::$cssPrefix . '-transform'], []);
366
            if (!ArrayHelper::isIn($transformation, $transformations)) {
367
                $transformations[] = $transformation;
368
            }
369
370
            ArrayHelper::setValue($this->options, ['data', FA::$cssPrefix . '-transform'], $transformations);
371
        }
372
373
        return $this;
374
    }
375
376
    /**
377
     * @param string $maskIcon
378
     * @param bool $condition
379
     * @param string|bool $throw
380
     * @return static
381
     * @throws \yii\base\InvalidConfigException
382
     * @codeCoverageIgnore
383
     */
384
    public function addMask($maskIcon, $condition = true, $throw = false)
385
    {
386
        if ($condition === false) {
387 View Code Duplication
            if (!empty($throw)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
388
                $message = !is_string($throw)
389
                    ? 'Condition is false'
390
                    : $throw;
391
392
                throw new \yii\base\InvalidConfigException($message);
393
            }
394
        } else {
395
            ArrayHelper::setValue($this->options, ['data', FA::$cssPrefix . '-mask'],
396
                FA::i($maskIcon)->options['class']);
397
        }
398
399
        return $this;
400
    }
401
}
402