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

Link::setDisabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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