Completed
Push — master ( c42cfb...d07980 )
by ARCANEDEV
14s
created

Label::checkIcon()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 3
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 3
1
<?php namespace Arcanesoft\Core\Helpers\UI;
2
3
use Illuminate\Support\Arr;
4
use Illuminate\Support\HtmlString;
5
6
/**
7
 * Class     Label
8
 *
9
 * @package  Arcanesoft\Core\Helpers\UI
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @TODO: Refactoring
13
 */
14
class Label
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Generate active icon label.
23
     *
24
     * @param  bool   $active
25
     * @param  array  $options
26
     * @param  bool   $withTooltip
27
     *
28
     * @return \Illuminate\Support\HtmlString
29
     */
30 6
    public static function activeIcon($active, array $options = [], $withTooltip = true)
31
    {
32
        // TODO: Refactor the options to a dedicated config file.
33 6
        $classes      = Arr::get($options, 'classes', [
34 6
            'enabled'  => 'label label-success',
35
            'disabled' => 'label label-default',
36
        ]);
37 6
        $translations = Arr::get($options, 'trans', [
38 6
            'enabled'  => 'core::statuses.enabled',
39
            'disabled' => 'core::statuses.disabled',
40
        ]);
41 6
        $icons = Arr::get($options, 'icons', [
42 6
            'enabled'  => 'fa fa-fw fa-check',
43
            'disabled' => 'fa fa-fw fa-ban',
44
        ]);
45
46 6
        $key        = $active ? 'enabled' : 'disabled';
47 6
        $attributes = ['class' => $classes[$key]];
48 6
        $value      = static::generateIcon($icons[$key]);
49
50 6
        return $withTooltip
51 4
            ? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes)
52 6
            : static::generate($value, $attributes);
53
    }
54
55
    /**
56
     * Generate active status label.
57
     *
58
     * @param  bool   $active
59
     * @param  array  $options
60
     * @param  bool   $withIcon
61
     *
62
     * @return \Illuminate\Support\HtmlString
63
     */
64 6
    public static function activeStatus($active, array $options = [], $withIcon = true)
65
    {
66
        // TODO: Refactor the options to a dedicated config file.
67 6
        $classes      = Arr::get($options, 'classes', [
68 6
            'enabled'  => 'label label-success',
69
            'disabled' => 'label label-default',
70
        ]);
71 6
        $translations = Arr::get($options, 'trans', [
72 6
            'enabled'  => 'core::statuses.enabled',
73
            'disabled' => 'core::statuses.disabled',
74
        ]);
75 6
        $icons = Arr::get($options, 'icons', [
76 6
            'enabled'  => 'fa fa-fw fa-check',
77
            'disabled' => 'fa fa-fw fa-ban',
78
        ]);
79
80 6
        $key        = $active ? 'enabled' : 'disabled';
81 6
        $attributes = ['class' => $classes[$key]];
82
83 6
        return $withIcon
84 4
            ? static::generateWithIcon(ucfirst(trans($translations[$key])), $icons[$key], $attributes)
85 6
            : static::generate(ucfirst(trans($translations[$key])), $attributes);
86
    }
87
88
    /**
89
     * Generate check status label.
90
     *
91
     * @param  bool   $checked
92
     * @param  array  $options
93
     * @param  bool   $withTooltip
94
     *
95
     * @return \Illuminate\Support\HtmlString
96
     */
97 4
    public static function checkIcon($checked, array $options = [], $withTooltip = true)
98
    {
99 4
        $classes      = Arr::get($options, 'classes', [
100 4
            'checked'   => 'label label-success',
101
            'unchecked' => 'label label-default',
102
        ]);
103
104 4
        $translations = Arr::get($options, 'trans', [
105 4
            'checked'   => 'core::statuses.checked',
106
            'unchecked' => 'core::statuses.unchecked',
107
        ]);
108
109 4
        $icons = Arr::get($options, 'icons', [
110 4
            'checked'   => 'fa fa-fw fa-check',
111
            'unchecked' => 'fa fa-fw fa-ban',
112
        ]);
113
114 4
        $key        = $checked ? 'checked' : 'unchecked';
115 4
        $attributes = ['class' => $classes[$key]];
116 4
        $value      = static::generateIcon($icons[$key]);
117
118 4
        return $withTooltip
119 4
            ? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes)
120 4
            : static::generate($value, $attributes);
121
    }
122
123
    /**
124
     * Generate check status label.
125
     *
126
     * @param  bool   $checked
127
     * @param  array  $options
128
     * @param  bool   $withIcon
129
     *
130
     * @return \Illuminate\Support\HtmlString
131
     */
132 6
    public static function checkStatus($checked, array $options = [], $withIcon = true)
133
    {
134
        // TODO: Refactor the options to a dedicated config file.
135 6
        $classes      = Arr::get($options, 'classes', [
136 6
            'checked'   => 'label label-success',
137
            'unchecked' => 'label label-default',
138
        ]);
139 6
        $translations = Arr::get($options, 'trans', [
140 6
            'checked'   => 'core::statuses.checked',
141
            'unchecked' => 'core::statuses.unchecked',
142
        ]);
143 6
        $icons = Arr::get($options, 'icons', [
144 6
            'checked'   => 'fa fa-fw fa-check',
145
            'unchecked' => 'fa fa-fw fa-ban',
146
        ]);
147
148 6
        $key        = $checked ? 'checked' : 'unchecked';
149 6
        $attributes = ['class' => $classes[$key]];
150
151 6
        return $withIcon
152 4
            ? static::generateWithIcon(ucfirst(trans($translations[$key])), $icons[$key], $attributes)
153 6
            : static::generate(ucfirst(trans($translations[$key])), $attributes);
154
    }
155
156
    /**
157
     * Generate count label.
158
     *
159
     * @param  int|float  $count
160
     * @param  array      $options
161
     *
162
     * @return \Illuminate\Support\HtmlString
163
     */
164 6
    public static function count($count, array $options = []) {
165
166
        // TODO: Refactor the options to a dedicated config file.
167 6
        $classes = Arr::get($options, 'classes', [
168 6
            'positive' => 'label label-info',
169
            'zero'     => 'label label-default',
170
            'negative' => 'label label-danger',
171
        ]);
172
173 6
        return static::generate($count, [
174 6
            'class' => $count > 0 ? $classes['positive'] : ($count < 0 ? $classes['negative'] : $classes['zero'])
175
        ]);
176
    }
177
178
    /**
179
     * Generate locked icon label.
180
     *
181
     * @param  bool   $locked
182
     * @param  array  $options
183
     * @param  bool   $withTooltip
184
     *
185
     * @return \Illuminate\Support\HtmlString
186
     */
187 4
    public static function lockedIcon($locked, array $options = [], $withTooltip = true)
188
    {
189
        // TODO: Refactor the options to a dedicated config file.
190 4
        $classes      = Arr::get($options, 'classes', [
191 4
            'locked'   => 'label label-danger',
192
            'unlocked' => 'label label-success',
193
        ]);
194 4
        $translations = Arr::get($options, 'trans', [
195 4
            'locked'   => 'core::statuses.locked',
196
            'unlocked' => 'core::statuses.unlocked',
197
        ]);
198 4
        $icons = Arr::get($options, 'icons', [
199 4
            'locked'   => 'fa fa-fw fa-lock',
200
            'unlocked' => 'fa fa-fw fa-unlock',
201
        ]);
202
203 4
        $key        = $locked ? 'locked' : 'unlocked';
204 4
        $value      = static::generateIcon($icons[$key]);
205 4
        $attributes = ['class' => $classes[$key]];
206
207 4
        return $withTooltip
208 2
            ? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes)
209 4
            : static::generate($value, $attributes);
210
    }
211
212
    /**
213
     * Generate locked status label.
214
     *
215
     * @param  bool   $locked
216
     * @param  array  $options
217
     * @param  bool   $withIcon
218
     *
219
     * @return \Illuminate\Support\HtmlString
220
     */
221 4
    public static function lockedStatus($locked, array $options = [], $withIcon = true)
222
    {
223
        // TODO: Refactor the options to a dedicated config file.
224 4
        $classes      = Arr::get($options, 'classes', [
225 4
            'locked'   => 'label label-danger',
226
            'unlocked' => 'label label-success',
227
        ]);
228 4
        $translations = Arr::get($options, 'trans', [
229 4
            'locked'   => 'core::statuses.locked',
230
            'unlocked' => 'core::statuses.unlocked',
231
        ]);
232 4
        $icons = Arr::get($options, 'icons', [
233 4
            'locked'   => 'fa fa-fw fa-lock',
234
            'unlocked' => 'fa fa-fw fa-unlock',
235
        ]);
236
237 4
        $key        = $locked ? 'locked' : 'unlocked';
238 4
        $value      = ucfirst(trans($translations[$key]));
239 4
        $attributes = ['class' => $classes[$key]];
240
241 4
        return $withIcon
242 2
            ? static::generateWithIcon($value, $icons[$key], $attributes)
243 4
            : static::generate($value, $attributes);
244
    }
245
246
    /**
247
     * Generate trashed icon label.
248
     *
249
     * @param  array  $options
250
     * @return \Illuminate\Support\HtmlString
251
     */
252 4
    public static function trashedStatus(array $options = [], $withIcon = true)
253
    {
254 4
        $trans      = Arr::get($options, 'trans', 'core::statuses.trashed');
255 4
        $icon       = Arr::get($options, 'icon', 'fa fa-fw fa-trash-o');
256
        $attributes = [
257 4
            'class' => Arr::get($options, 'class', 'label label-danger')
258
        ];
259
260 4
        return $withIcon
261 2
            ? static::generateWithIcon(ucfirst(trans($trans)), $icon, $attributes)
262 4
            : static::generate(ucfirst(trans($trans)), $attributes);
263
    }
264
265
    /* -----------------------------------------------------------------
266
     |  Other Methods
267
     | -----------------------------------------------------------------
268
     */
269
270
    /**
271
     * Generate the label.
272
     *
273
     * @param  mixed  $value
274
     * @param  array  $attributes
275
     *
276
     * @return \Illuminate\Support\HtmlString
277
     */
278 40
    protected static function generate($value, array $attributes = [])
279
    {
280 40
        return new HtmlString(
281 40
            '<span'.html()->attributes($attributes).'>'.$value.'</span>'
282
        );
283
    }
284
285
    /**
286
     * Generate the label with tooltip.
287
     *
288
     * @param  string  $value
289
     * @param  string  $title
290
     * @param  array   $attributes
291
     *
292
     * @return \Illuminate\Support\HtmlString
293
     */
294 10
    protected static function generateWithTooltip($value, $title, array $attributes = [])
295
    {
296 10
        $attributes['data-toggle']         = 'tooltip';
297 10
        $attributes['data-original-title'] = $title;
298
299 10
        return static::generate($value, $attributes);
300
    }
301
302
    /**
303
     * Generate the label with icon & text as value.
304
     *
305
     * @param  string  $value
306
     * @param  string  $icon
307
     * @param  array   $attributes
308
     *
309
     * @return \Illuminate\Support\HtmlString
310
     */
311 12
    protected static function generateWithIcon($value, $icon, array $attributes = [])
312
    {
313 12
        return static::generate(static::generateIcon($icon). ' '.$value, $attributes);
314
    }
315
316
    /**
317
     * Generate icon tag.
318
     *
319
     * @param  string  $class
320
     *
321
     * @return string
322
     */
323 26
    protected static function generateIcon($class)
324
    {
325 26
        return '<i class="'.$class.'"></i>';
326
    }
327
}
328