Completed
Push — master ( 2760df...c42cfb )
by ARCANEDEV
13s
created

Label   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
dl 0
loc 246
ccs 62
cts 74
cp 0.8378
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 3

10 Methods

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