Completed
Pull Request — master (#5)
by ARCANEDEV
03:13
created

Button::size()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php namespace Arcanesoft\Core\Helpers\UI;
2
3
use Illuminate\Contracts\Support\Htmlable;
4
use Illuminate\Support\Str;
5
6
/**
7
 * Class     Button
8
 *
9
 * @package  Arcanesoft\Core\Helpers\UI
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Button implements Htmlable
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
    /** @var string */
19
    protected $action;
20
21
    /** @var string */
22
    protected $type;
23
24
    /** @var \Illuminate\Support\Collection */
25
    protected $attributes;
26
27
    /** @var bool */
28
    protected $disabled;
29
30
    /** @var string */
31
    protected $size = 'md';
32
33
    /** @var bool */
34
    protected $withTitle = true;
35
36
    /** @var bool */
37
    protected $withIcon = true;
38
39
    /** @var bool */
40
    protected $withTooltip = false;
41
42
    /* -----------------------------------------------------------------
43
     |  Constructor
44
     | -----------------------------------------------------------------
45
     */
46
    /**
47
     * Button constructor.
48
     *
49
     * @param  string  $action
50
     * @param  string  $type
51
     * @param  array   $attributes
52
     * @param  bool    $disabled
53
     */
54 39
    public function __construct($action, $type = 'button', array $attributes = [], $disabled)
55
    {
56 39
        $this->action = $action;
57 39
        $this->setType($type);
58 39
        $this->setAttributes($attributes);
59 39
        $this->setDisabled($disabled);
60 39
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Getters & Setters
64
     | -----------------------------------------------------------------
65
     */
66
    /**
67
     * Set the button type.
68
     *
69
     * @param  string  $type
70
     *
71
     * @return self
72
     */
73 39
    public function setType($type)
74
    {
75 39
        $this->type = in_array($type = strtolower($type), ['button', 'submit', 'reset']) ? $type : 'button';
76
77 39
        return $this;
78
    }
79
80
    /**
81
     * Set the attributes.
82
     *
83
     * @param  array  $attributes
84
     *
85
     * @return self
86
     */
87 39
    public function setAttributes(array $attributes)
88
    {
89 39
        $this->attributes = collect($attributes);
90
91 39
        return $this;
92
    }
93
94
    /**
95
     * Set an attribute.
96
     *
97
     * @param  string  $key
98
     * @param  mixed   $value
99
     *
100
     * @return self
101
     */
102 3
    public function setAttribute($key, $value)
103
    {
104 3
        $this->attributes->put($key, $value);
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Set disabled state.
111
     *
112
     * @param  bool  $disabled
113
     *
114
     * @return self
115
     */
116 39
    public function setDisabled($disabled)
117
    {
118 39
        $this->disabled = (bool) $disabled;
119
120 39
        if ($this->disabled) {
121 3
            $this->attributes = $this->attributes->reject(function ($value, $key) {
122
                return Str::startsWith($key, ['data-']);
123 3
            });
124 1
        }
125
126 39
        return $this;
127
    }
128
129
    /**
130
     * Set the size.
131
     *
132
     * @param  string  $size
133
     *
134
     * @return self
135
     */
136 6
    public function size($size)
137
    {
138 6
        $this->size = $size;
139
140 6
        return $this;
141
    }
142
143
    /**
144
     * Show/Hide the title.
145
     *
146
     * @param  bool  $withTitle
147
     *
148
     * @return self
149
     */
150 3
    public function withTitle($withTitle)
151
    {
152 3
        $this->withTitle = $withTitle;
153
154 3
        return $this;
155
    }
156
157
    /**
158
     * Show/Hide the icon.
159
     *
160
     * @var  bool  $withIcon
161
     *
162
     * @return self
163
     */
164 9
    public function withIcon($withIcon)
165
    {
166 9
        $this->withIcon = (bool) $withIcon;
167
168 9
        return $this;
169
    }
170
171
    /**
172
     * Enable the tooltip.
173
     *
174
     * @param  bool  $withTooltip
175
     *
176
     * @return self
177
     */
178 6
    public function withTooltip($withTooltip)
179
    {
180 6
        $this->withTooltip = (bool) $withTooltip;
181
182 6
        return $this;
183
    }
184
185
    /**
186
     * Show only the icon and the title as tooltip.
187
     *
188
     * @return self
189
     */
190 3
    public function onlyIcon()
191
    {
192 3
        return $this->withIcon(true)->withTooltip(true);
193
    }
194
195
    /* -----------------------------------------------------------------
196
     |  Main Methods
197
     | -----------------------------------------------------------------
198
     */
199
    /**
200
     * Make a button instance.
201
     *
202
     * @param  string  $action
203
     * @param  string  $type
204
     * @param  array   $attributes
205
     * @param  bool    $disabled
206
     *
207
     * @return self
208
     */
209 39
    public static function make($action, $type = 'button', array $attributes = [], $disabled = false)
210
    {
211 39
        return new static($action, $type, $attributes, $disabled);
212
    }
213
214
    /**
215
     * Get content as a string of HTML.
216
     *
217
     * @return string
218
     */
219 39
    public function toHtml()
220
    {
221 39
        return '<button'.$this->renderAttributes().'>'.$this->renderValue().'</button>';
222
    }
223
224
    /**
225
     * Get the string content for the link instance.
226
     *
227
     * @return string
228
     */
229 3
    public function __toString()
230
    {
231 3
        return $this->toHtml();
232
    }
233
234
    /* -----------------------------------------------------------------
235
     |  Other Methods
236
     | -----------------------------------------------------------------
237
     */
238
    /**
239
     * Render the attributes.
240
     *
241
     * @return string
242
     */
243 39
    protected function renderAttributes()
244
    {
245 39
        $attributes = collect();
246 39
        $attributes->put('type', $this->type);
247 39
        $attributes->put('class', $this->getStyleClass());
248
249 39
        if ($this->withTooltip) {
250
            // This is for Bootstrap
251 6
            $attributes->put('data-toggle', 'tooltip');
252 6
            $attributes->put('data-original-title', $this->getTitle());
253 2
        }
254
255 39
        if ($this->disabled) {
256 3
            $attributes->put('type', 'button');
257 3
            $attributes->put('disabled', 'disabled');
258 1
        }
259
260 39
        return html()->attributes($attributes->merge($this->attributes)->toArray());
261
    }
262
263
    /**
264
     * Render the link value.
265
     *
266
     * @return string
267
     */
268 39
    protected function renderValue()
269
    {
270 39
        if ($this->withTooltip || ! $this->withTitle)
271 19
            return $this->renderIcon();
272
273 30
        return $this->withIcon
274 26
            ? $this->renderIcon().' '.$this->getTitle()
275 30
            : $this->getTitle();
276
    }
277
278
    /**
279
     * Get the button class.
280
     *
281
     * @return string
282
     */
283 39
    protected function getStyleClass()
284
    {
285 39
        return implode(' ', array_filter(['btn', $this->getSize(), $this->getColor()]));
286
    }
287
288
    /**
289
     * Get the button size.
290
     *
291
     * @return string|null
292
     */
293 39
    protected function getSize()
294
    {
295 39
        return $this->getConfig("sizes.{$this->size}");
296
    }
297
298
    /**
299
     * Get the action title.
300
     *
301
     * @return string
302
     */
303 36
    protected function getTitle()
304
    {
305 36
        return Str::ucfirst(trans("core::actions.{$this->action}"));
306
    }
307
308
    /**
309
     * Get the link color.
310
     *
311
     * @return string
312
     */
313 39
    protected function getColor()
314
    {
315 39
        $state = $this->disabled ? 'default' : $this->action;
316
317 39
        return $this->getConfig("colors.{$state}");
318
    }
319
320
    /**
321
     * Render the icon.
322
     *
323
     * @return string
324
     */
325 33
    protected function renderIcon()
326
    {
327 33
        return '<i class="'.$this->getIcon().'"></i>';
328
    }
329
330
    /**
331
     * Get the icon.
332
     *
333
     * @return string|null
334
     */
335 33
    protected function getIcon()
336
    {
337 33
        return $this->getConfig("icons.{$this->action}");
338
    }
339
340
    /**
341
     * Get the value from config.
342
     *
343
     * @param  string  $key
344
     *
345
     * @return mixed
346
     */
347 39
    protected function getConfig($key)
348
    {
349 39
        return config("arcanesoft.core.ui.buttons.{$key}");
350
    }
351
}
352