Completed
Pull Request — master (#5)
by ARCANEDEV
04:26
created

AbstractClickable::setAction()   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
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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     AbstractClickable
8
 *
9
 * @package  Arcanesoft\Core\Helpers\UI
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class AbstractClickable implements Htmlable
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
    /** @var string */
19
    protected $action;
20
21
    /** @var \Illuminate\Support\Collection */
22
    protected $attributes;
23
24
    /** @var bool */
25
    protected $disabled = false;
26
27
    /** @var string */
28
    protected $size = 'md';
29
30
    /** @var bool */
31
    protected $withTitle = true;
32
33
    /** @var bool */
34
    protected $withIcon = true;
35
36
    /** @var bool */
37
    protected $withTooltip = false;
38
39
    /* -----------------------------------------------------------------
40
     |  Getters & Setters
41
     | -----------------------------------------------------------------
42
     */
43
    /**
44
     * Set the action.
45
     *
46
     * @param  string  $action
47
     *
48
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
49
     */
50 78
    public function setAction($action)
51
    {
52 78
        $this->action = $action;
53
54 78
        return $this;
55
    }
56
57
    /**
58
     * Set the attributes.
59
     *
60
     * @param  array  $attributes
61
     *
62
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
63
     */
64 78
    public function setAttributes(array $attributes)
65
    {
66 78
        $this->attributes = collect($attributes);
67
68 78
        return $this;
69
    }
70
71
    /**
72
     * Set an attribute.
73
     *
74
     * @param  string  $key
75
     * @param  mixed   $value
76
     *
77
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
78
     */
79 6
    public function setAttribute($key, $value)
80
    {
81 6
        $this->attributes->put($key, $value);
82
83 6
        return $this;
84
    }
85
86
    /**
87
     * Set disabled state.
88
     *
89
     * @param  bool  $disabled
90
     *
91
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
92
     */
93 78
    public function setDisabled($disabled)
94
    {
95 78
        $this->disabled = (bool) $disabled;
96
97 78
        if ($this->disabled) {
98 6
            $this->attributes = $this->attributes->reject(function ($value, $key) {
99
                return Str::startsWith($key, ['data-']);
100 6
            });
101 2
        }
102
103 78
        return $this;
104
    }
105
106
    /**
107
     * Set the size.
108
     *
109
     * @param  string  $size
110
     *
111
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
112
     */
113 12
    public function size($size)
114
    {
115 12
        $this->size = $size;
116
117 12
        return $this;
118
    }
119
120
    /**
121
     * Show/Hide the title.
122
     *
123
     * @param  bool  $withTitle
124
     *
125
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
126
     */
127 6
    public function withTitle($withTitle)
128
    {
129 6
        $this->withTitle = $withTitle;
130
131 6
        return $this;
132
    }
133
134
    /**
135
     * Show/Hide the icon.
136
     *
137
     * @var  bool  $withIcon
138
     *
139
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
140
     */
141 18
    public function withIcon($withIcon)
142
    {
143 18
        $this->withIcon = (bool) $withIcon;
144
145 18
        return $this;
146
    }
147
148
    /**
149
     * Enable/Disable the tooltip.
150
     *
151
     * @param  bool  $withTooltip
152
     *
153
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
154
     */
155 12
    public function withTooltip($withTooltip)
156
    {
157 12
        $this->withTooltip = (bool) $withTooltip;
158
159 12
        return $this;
160
    }
161
162
    /**
163
     * Show only the icon and the title as tooltip.
164
     *
165
     * @return \Arcanesoft\Core\Helpers\UI\AbstractClickable
166
     */
167 6
    public function onlyIcon()
168
    {
169 6
        return $this->withIcon(true)->withTooltip(true);
170
    }
171
172
    /* -----------------------------------------------------------------
173
     |  Main Methods
174
     | -----------------------------------------------------------------
175
     */
176
    /**
177
     * Get the string content for the link instance.
178
     *
179
     * @return string
180
     */
181 6
    public function __toString()
182
    {
183 6
        return $this->toHtml();
184
    }
185
186
    /* -----------------------------------------------------------------
187
     |  Other Methods
188
     | -----------------------------------------------------------------
189
     */
190
    /**
191
     * Render the value.
192
     *
193
     * @return string
194
     */
195 78
    protected function renderValue()
196
    {
197 78
        if ($this->withTooltip || ! $this->withTitle)
198 38
            return $this->renderIcon();
199
200 60
        return $this->withIcon
201 52
            ? $this->renderIcon().' '.$this->getTitle()
202 60
            : $this->getTitle();
203
    }
204
205
    /**
206
     * Render the icon.
207
     *
208
     * @return string
209
     */
210 66
    protected function renderIcon()
211
    {
212 66
        return '<i class="'.$this->getIcon().'"></i>';
213
    }
214
215
    /**
216
     * Get the action title.
217
     *
218
     * @return string
219
     */
220 72
    protected function getTitle()
221
    {
222 72
        return Str::ucfirst(trans("core::actions.{$this->action}"));
223
    }
224
225
    /**
226
     * Get the icon.
227
     *
228
     * @return string|null
229
     */
230 66
    protected function getIcon()
231
    {
232 66
        return $this->getConfig("icons.{$this->action}");
233
    }
234
235
    /**
236
     * Get the link color.
237
     *
238
     * @return string
239
     */
240 78
    protected function getColor()
241
    {
242 78
        $state = $this->disabled ? 'default' : $this->action;
243
244 78
        return $this->getConfig("colors.{$state}");
245
    }
246
247
    /**
248
     * Get the link size.
249
     *
250
     * @return string|null
251
     */
252 78
    protected function getSize()
253
    {
254 78
        return $this->getConfig("sizes.{$this->size}");
255
    }
256
257
    /**
258
     * Get the value from config.
259
     *
260
     * @param  string  $key
261
     *
262
     * @return mixed
263
     */
264
    abstract protected function getConfig($key);
265
}
266