Completed
Push — master ( 4fa96a...ba6212 )
by ARCANEDEV
03:18
created

Link::getLinkIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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