Completed
Push — master ( 0d3318...8136d6 )
by ARCANEDEV
02:52
created

Label::activeIcon()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 3
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 19
cts 19
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
     * Generate active icon label.
22
     *
23
     * @param  bool   $active
24
     * @param  array  $options
25
     * @param  bool   $withTooltip
26
     *
27
     * @return \Illuminate\Support\HtmlString
28
     */
29 6
    public static function activeIcon($active, array $options = [], $withTooltip = true)
30
    {
31
        // TODO: Refactor the options to a dedicated config file.
32 6
        $classes      = Arr::get($options, 'classes', [
33 6
            'enabled'  => 'label label-success',
34 2
            'disabled' => 'label label-default',
35 2
        ]);
36 6
        $translations = Arr::get($options, 'trans', [
37 6
            'enabled'  => 'core::statuses.enabled',
38 2
            'disabled' => 'core::statuses.disabled',
39 2
        ]);
40 6
        $icons = Arr::get($options, 'icons', [
41 6
            'enabled'  => 'fa fa-fw fa-check',
42 2
            'disabled' => 'fa fa-fw fa-ban',
43 2
        ]);
44
45 6
        $key        = $active ? 'enabled' : 'disabled';
46 6
        $attributes = ['class' => $classes[$key]];
47 6
        $value      = static::generateIcon($icons[$key]);
48
49 4
        return $withTooltip
50 4
            ? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes)
51 6
            : static::generate($value, $attributes);
52
    }
53
54
    /**
55
     * Generate active status label.
56
     *
57
     * @param  bool   $active
58
     * @param  array  $options
59
     *
60
     * @return \Illuminate\Support\HtmlString
61
     */
62
    public static function activeStatus($active, array $options = [])
63
    {
64
        // TODO: Refactor the options to a dedicated config file.
65
        $classes      = Arr::get($options, 'classes', [
66
            'enabled'  => 'label label-success',
67
            'disabled' => 'label label-default',
68
        ]);
69
        $translations = Arr::get($options, 'trans', [
70
            'enabled'  => 'core::statuses.enabled',
71
            'disabled' => 'core::statuses.disabled',
72
        ]);
73
74
        $key = $active ? 'enabled' : 'disabled';
75
76
        return static::generate(ucfirst(trans($translations[$key])), [
77
            'class' => $classes[$key]
78
        ]);
79
    }
80
81
    /**
82
     * Generate count label.
83
     *
84
     * @param  int|float  $count
85
     * @param  array      $options
86
     *
87
     * @return \Illuminate\Support\HtmlString
88
     */
89 9
    public static function count($count, array $options = []) {
90
91
        // TODO: Refactor the options to a dedicated config file.
92 9
        $classes = Arr::get($options, 'classes', [
93 9
            'positive' => 'label label-info',
94 3
            'zero'     => 'label label-default',
95 3
            'negative' => 'label label-danger',
96 3
        ]);
97
98 9
        return static::generate($count, [
99 9
            'class' => $count > 0 ? $classes['positive'] : ($count < 0 ? $classes['negative'] : $classes['zero'])
100 3
        ]);
101
    }
102
103
    /**
104
     * Generate locked icon label.
105
     *
106
     * @param  bool   $locked
107
     * @param  array  $options
108
     * @param  bool   $withTooltip
109
     *
110
     * @return \Illuminate\Support\HtmlString
111
     */
112 6
    public static function lockedIcon($locked, array $options = [], $withTooltip = true)
113
    {
114
        // TODO: Refactor the options to a dedicated config file.
115 6
        $classes      = Arr::get($options, 'classes', [
116 6
            'locked'   => 'label label-danger',
117 2
            'unlocked' => 'label label-success',
118 2
        ]);
119 6
        $translations = Arr::get($options, 'trans', [
120 6
            'locked'   => 'core::statuses.locked',
121 2
            'unlocked' => 'core::statuses.unlocked',
122 2
        ]);
123 6
        $icons = Arr::get($options, 'icons', [
124 6
            'locked'   => 'fa fa-fw fa-lock',
125 2
            'unlocked' => 'fa fa-fw fa-unlock',
126 2
        ]);
127
128 6
        $key        = $locked ? 'locked' : 'unlocked';
129 6
        $value      = static::generateIcon($icons[$key]);
130 6
        $attributes = ['class' => $classes[$key]];
131
132 4
        return $withTooltip
133 4
            ? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes)
134 6
            : static::generate($value, $attributes);
135
    }
136
137
    /**
138
     * Generate locked status label.
139
     *
140
     * @param  bool   $locked
141
     * @param  array  $options
142
     * @param  bool   $withIcon
143
     *
144
     * @return \Illuminate\Support\HtmlString
145
     */
146 6
    public static function lockedStatus($locked, array $options = [], $withIcon = true)
147
    {
148
        // TODO: Refactor the options to a dedicated config file.
149 6
        $classes      = Arr::get($options, 'classes', [
150 6
            'locked'   => 'label label-danger',
151 2
            'unlocked' => 'label label-success',
152 2
        ]);
153 6
        $translations = Arr::get($options, 'trans', [
154 6
            'locked'   => 'core::statuses.locked',
155 2
            'unlocked' => 'core::statuses.unlocked',
156 2
        ]);
157 6
        $icons = Arr::get($options, 'icons', [
158 6
            'locked'   => 'fa fa-fw fa-lock',
159 2
            'unlocked' => 'fa fa-fw fa-unlock',
160 2
        ]);
161
162 6
        $key        = $locked ? 'locked' : 'unlocked';
163 6
        $value      = ucfirst(trans($translations[$key]));
164 6
        $attributes = ['class' => $classes[$key]];
165
166 4
        return $withIcon
167 4
            ? static::generateWithIcon($value, $icons[$key], $attributes)
168 6
            : static::generate($value, $attributes);
169
    }
170
171
    /**
172
     * Generate trashed icon label.
173
     *
174
     * @param  array  $options
175
     * @return \Illuminate\Support\HtmlString
176
     */
177 6
    public static function trashedStatus(array $options = [], $withIcon = true)
178
    {
179 6
        $trans      = Arr::get($options, 'trans', 'core::statuses.trashed');
180 6
        $icon       = Arr::get($options, 'icon', 'fa fa-fw fa-trash-o');
181
        $attributes = [
182 6
            'class' => Arr::get($options, 'class', 'label label-danger')
183 2
        ];
184
185 4
        return $withIcon
186 4
            ? static::generateWithIcon(ucfirst(trans($trans)), $icon, $attributes)
187 6
            : static::generate(ucfirst(trans($trans)), $attributes);
188
    }
189
190
    /* -----------------------------------------------------------------
191
     |  Other Methods
192
     | -----------------------------------------------------------------
193
     */
194
    /**
195
     * Generate the label.
196
     *
197
     * @param  mixed  $value
198
     * @param  array  $attributes
199
     *
200
     * @return \Illuminate\Support\HtmlString
201
     */
202 33
    protected static function generate($value, array $attributes = [])
203
    {
204 33
        return new HtmlString(
205 33
            '<span'.html()->attributes($attributes).'>'.$value.'</span>'
206 11
        );
207
    }
208
209
    /**
210
     * Generate the label with tooltip.
211
     *
212
     * @param  string  $value
213
     * @param  string  $title
214
     * @param  array   $attributes
215
     *
216
     * @return \Illuminate\Support\HtmlString
217
     */
218 6
    protected static function generateWithTooltip($value, $title, array $attributes = [])
219
    {
220 6
        $attributes['data-toggle']         = 'tooltip';
221 6
        $attributes['data-original-title'] = $title;
222
223 6
        return static::generate($value, $attributes);
224
    }
225
226
    /**
227
     * Generate the label with icon & text as value.
228
     *
229
     * @param  string  $value
230
     * @param  string  $icon
231
     * @param  array   $attributes
232
     *
233
     * @return \Illuminate\Support\HtmlString
234
     */
235 6
    protected static function generateWithIcon($value, $icon, array $attributes = [])
236
    {
237 6
        return static::generate(static::generateIcon($icon). ' '.$value, $attributes);
238
    }
239
240
    /**
241
     * Generate icon tag.
242
     *
243
     * @param  string  $class
244
     *
245
     * @return string
246
     */
247 18
    protected static function generateIcon($class)
248
    {
249 18
        return '<i class="'.$class.'"></i>';
250
    }
251
}
252