Completed
Push — master ( 5b5c65...0266b3 )
by Revin
02:48 queued 52s
created

Icon::rotate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 11
cts 11
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
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\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 9
    public function __construct($cssPrefix, $name, $options = [])
32
    {
33 9
        Html::addCssClass($options, $cssPrefix);
34
35 9
        if (!empty($name)) {
36 9
            Html::addCssClass($options, FontAwesome::$basePrefix . '-' . $name);
37 9
        }
38
39 9
        $this->options = $options;
40 9
    }
41
42
    /**
43
     * @return string
44
     */
45 5
    public function __toString()
46
    {
47 5
        $options = $this->options;
48
49 5
        $tag = ArrayHelper::remove($options, 'tag', 'i');
50
51 5
        return Html::tag($tag, null, $options);
52
    }
53
54
    /**
55
     * @return \rmrevin\yii\fontawesome\component\Icon
56
     * @throws \yii\base\InvalidConfigException
57
     */
58 1
    public function inverse()
59
    {
60 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-inverse');
61
    }
62
63
    /**
64
     * @return \rmrevin\yii\fontawesome\component\Icon
65
     * @throws \yii\base\InvalidConfigException
66
     */
67 3
    public function spin()
68
    {
69 3
        return $this->addCssClass(FontAwesome::$basePrefix . '-spin');
70
    }
71
72
    /**
73
     * @return \rmrevin\yii\fontawesome\component\Icon
74
     * @throws \yii\base\InvalidConfigException
75
     */
76 1
    public function pulse()
77
    {
78 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-pulse');
79
    }
80
81
    /**
82
     * @return \rmrevin\yii\fontawesome\component\Icon
83
     * @throws \yii\base\InvalidConfigException
84
     */
85 1
    public function fixedWidth()
86
    {
87 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-fw');
88
    }
89
90
    /**
91
     * @return \rmrevin\yii\fontawesome\component\Icon
92
     * @throws \yii\base\InvalidConfigException
93
     */
94 3
    public function li()
95
    {
96 3
        return $this->addCssClass(FontAwesome::$basePrefix . '-li');
97
    }
98
99
    /**
100
     * @return \rmrevin\yii\fontawesome\component\Icon
101
     * @throws \yii\base\InvalidConfigException
102
     */
103 1
    public function border()
104
    {
105 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-border');
106
    }
107
108
    /**
109
     * @return \rmrevin\yii\fontawesome\component\Icon
110
     * @throws \yii\base\InvalidConfigException
111
     */
112 1
    public function pullLeft()
113
    {
114 1
        return $this->addCssClass(FontAwesome::$basePrefix . '-pull-left');
115
    }
116
117
    /**
118
     * @return \rmrevin\yii\fontawesome\component\Icon
119
     * @throws \yii\base\InvalidConfigException
120
     */
121 1
    public function pullRight()
122
    {
123 1
        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 4
    public function size($value)
132
    {
133
        $values = [
134 4
            FontAwesome::SIZE_LG,
135 4
            FontAwesome::SIZE_SM,
136 4
            FontAwesome::SIZE_XS,
137 4
            FontAwesome::SIZE_2X,
138 4
            FontAwesome::SIZE_3X,
139 4
            FontAwesome::SIZE_4X,
140 4
            FontAwesome::SIZE_5X,
141 4
            FontAwesome::SIZE_6X,
142 4
            FontAwesome::SIZE_7X,
143 4
            FontAwesome::SIZE_8X,
144 4
            FontAwesome::SIZE_9X,
145 4
            FontAwesome::SIZE_10X,
146 4
        ];
147
148 4
        return $this->addCssClass(
149 4
            FontAwesome::$basePrefix . '-' . $value,
150 4
            in_array((string)$value, $values, true),
151 4
            sprintf(
152 4
                '%s - invalid value. Use one of the constants: %s.',
153 4
                'FontAwesome::size()',
154 4
                implode(', ', $values)
155 4
            )
156 4
        );
157
    }
158
159
    /**
160
     * @param string $value
161
     * @return \rmrevin\yii\fontawesome\component\Icon
162
     * @throws \yii\base\InvalidConfigException
163
     */
164 2 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 2
        $values = [FontAwesome::ROTATE_90, FontAwesome::ROTATE_180, FontAwesome::ROTATE_270];
167
168 2
        return $this->addCssClass(
169 2
            FontAwesome::$basePrefix . '-rotate-' . $value,
170 2
            in_array((string)$value, $values, true),
171 2
            sprintf(
172 2
                '%s - invalid value. Use one of the constants: %s.',
173 2
                'FontAwesome::rotate()',
174 2
                implode(', ', $values)
175 2
            )
176 2
        );
177
    }
178
179
    /**
180
     * @param string $value
181
     * @return \rmrevin\yii\fontawesome\component\Icon
182
     * @throws \yii\base\InvalidConfigException
183
     */
184 2 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 2
        $values = [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL];
187
188 2
        return $this->addCssClass(
189 2
            FontAwesome::$basePrefix . '-flip-' . $value,
190 2
            in_array((string)$value, [FontAwesome::FLIP_HORIZONTAL, FontAwesome::FLIP_VERTICAL], true),
191 2
            sprintf(
192 2
                '%s - invalid value. Use one of the constants: %s.',
193 2
                'FontAwesome::flip()',
194 2
                implode(', ', $values)
195 2
            )
196 2
        );
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