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

Link::withIcon()   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     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
    public $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
     * Get the action title.
68
     *
69
     * @return string
70
     */
71 36
    protected function getActionTitle()
72
    {
73 36
        return Str::ucfirst(trans("core::actions.{$this->action}"));
74
    }
75
76
    /**
77
     * Set the attributes.
78
     *
79
     * @param  array  $attributes
80
     *
81
     * @return self
82
     */
83 39
    public function setAttributes(array $attributes)
84
    {
85 39
        $this->attributes = collect($attributes);
86
87 39
        return $this;
88
    }
89
90
    /**
91
     * Set an attribute.
92
     *
93
     * @param  string  $key
94
     * @param  mixed   $value
95
     *
96
     * @return self
97
     */
98 3
    public function setAttribute($key, $value)
99
    {
100 3
        $this->attributes->put($key, $value);
101
102 3
        return $this;
103
    }
104
105
    /**
106
     * Set disabled state.
107
     *
108
     * @param  bool  $disabled
109
     *
110
     * @return self
111
     */
112 39
    public function setDisabled($disabled)
113
    {
114 39
        $this->disabled = (bool) $disabled;
115
116 39
        if ($this->disabled) {
117 3
            $this->attributes = $this->attributes->reject(function ($value, $key) {
118
                return Str::startsWith($key, ['data-']);
119 3
            });
120 2
        }
121
122 39
        return $this;
123
    }
124
125
    /**
126
     * Set the size.
127
     *
128
     * @param  string  $size
129
     *
130
     * @return self
131
     */
132 6
    public function size($size)
133
    {
134 6
        $this->size = $size;
135
136 6
        return $this;
137
    }
138
139
    /**
140
     * @param  bool  $withTitle
141
     *
142
     * @return self
143
     */
144 3
    public function withTitle($withTitle)
145
    {
146 3
        $this->withTitle = $withTitle;
147
148 3
        return $this;
149
    }
150
151
    /**
152
     * Show/Hide the icon.
153
     *
154
     * @var  bool  $withIcon
155
     *
156
     * @return self
157
     */
158 9
    public function withIcon($withIcon)
159
    {
160 9
        $this->withIcon = (bool) $withIcon;
161
162 9
        return $this;
163
    }
164
165
    /**
166
     * Enable the tooltip.
167
     *
168
     * @param  bool  $withTooltip
169
     *
170
     * @return self
171
     */
172 6
    public function withTooltip($withTooltip)
173
    {
174 6
        $this->withTooltip = (bool) $withTooltip;
175
176 6
        return $this;
177
    }
178
179
    /**
180
     * Show only the icon and the title as tooltip.
181
     *
182
     * @return self
183
     */
184 3
    public function onlyIcon()
185
    {
186 3
        return $this->withIcon(true)->withTooltip(true);
187
    }
188
189
    /* -----------------------------------------------------------------
190
     |  Main Methods
191
     | -----------------------------------------------------------------
192
     */
193
    /**
194
     * Make link instance.
195
     *
196
     * @param  string  $action
197
     * @param  string  $url
198
     * @param  array   $attributes
199
     * @param  bool    $disabled
200
     *
201
     * @return self
202
     */
203 39
    public static function make($action, $url, array $attributes = [], $disabled = false)
204
    {
205 39
        return new static($action, $url, $attributes, $disabled);
206
    }
207
208
    /**
209
     * Get content as a string of HTML.
210
     *
211
     * @return string
212
     */
213 39
    public function toHtml()
214
    {
215 39
        return '<a'.$this->renderAttributes().'>'.$this->renderValue().'</a>';
216
    }
217
218
    /**
219
     * Get the string content for the link instance.
220
     *
221
     * @return string
222
     */
223 3
    public function __toString()
224
    {
225 3
        return $this->toHtml();
226
    }
227
228
    /* -----------------------------------------------------------------
229
     |  Other Functions
230
     | -----------------------------------------------------------------
231
     */
232
    /**
233
     * Render the link value.
234
     *
235
     * @return string
236
     */
237 39
    protected function renderValue()
238
    {
239 39
        if ($this->withTooltip || ! $this->withTitle)
240 29
            return $this->renderIcon();
241
242 30
        return $this->withIcon
243 28
            ? $this->renderIcon().' '.$this->getActionTitle()
244 30
            : $this->getActionTitle();
245
    }
246
247
    /**
248
     * Render the icon.
249
     *
250
     * @return string
251
     */
252 33
    protected function renderIcon()
253
    {
254 33
        return '<i class="'.$this->getLinkIcon().'"></i>';
255
    }
256
257
    /**
258
     * Render the attributes.
259
     *
260
     * @return string
261
     */
262 39
    protected function renderAttributes()
263
    {
264 39
        $attributes = collect();
265 39
        $attributes->put('href',  $this->disabled ? 'javascript:void(0);' : $this->url);
266 39
        $attributes->put('class', $this->getLinkClass());
267
268 39
        if ($this->withTooltip) {
269
            // This is for Bootstrap
270 6
            $attributes->put('data-toggle', 'tooltip');
271 6
            $attributes->put('data-original-title', $this->getActionTitle());
272 4
        }
273
274 39
        if ($this->disabled)
275 27
            $attributes->put('disabled', 'disabled');
276
277 39
        return html()->attributes($attributes->merge($this->attributes)->toArray());
278
    }
279
280
    /**
281
     * Get the link class.
282
     *
283
     * @return string
284
     */
285 39
    protected function getLinkClass()
286
    {
287 39
        return implode(' ', array_filter(['btn', $this->getLinkSize(), $this->getLinkColor()]));
288
    }
289
290
    /**
291
     * Get the link color.
292
     *
293
     * @return string
294
     */
295 39
    protected function getLinkColor()
296
    {
297 39
        $state = $this->disabled ? 'default' : $this->action;
298
299 39
        return config("arcanesoft.core.ui.links.colors.{$state}");
300
    }
301
302
    /**
303
     * Get the link size.
304
     *
305
     * @return string|null
306
     */
307 39
    protected function getLinkSize()
308
    {
309 39
        return config("arcanesoft.core.ui.links.sizes.{$this->size}");
310
    }
311
312
    /**
313
     * Get the icon.
314
     *
315
     * @return string|null
316
     */
317 33
    protected function getLinkIcon()
318
    {
319 33
        return config("arcanesoft.core.ui.links.icons.{$this->action}");
320
    }
321
}
322