1
|
|
|
<?php namespace Arcanesoft\Core\Helpers\UI; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Settings\Helpers\Arr; |
4
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
5
|
|
|
use Illuminate\Support\HtmlString; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Link implements Htmlable |
9
|
|
|
{ |
10
|
|
|
/* ----------------------------------------------------------------- |
11
|
|
|
| Properties |
12
|
|
|
| ----------------------------------------------------------------- |
13
|
|
|
*/ |
14
|
|
|
/** @var string */ |
15
|
|
|
protected $url; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $title; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $icon; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $attributes = []; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Constructor |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
/** |
31
|
|
|
* Link constructor. |
32
|
|
|
*/ |
33
|
6 |
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
// |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/* ----------------------------------------------------------------- |
39
|
|
|
| Getters & Setters |
40
|
|
|
| ----------------------------------------------------------------- |
41
|
|
|
*/ |
42
|
3 |
|
public function setUrl($url) |
43
|
|
|
{ |
44
|
3 |
|
$this->url = $url; |
45
|
|
|
|
46
|
3 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the title. |
51
|
|
|
* |
52
|
|
|
* @param string $title |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
3 |
|
public function setTitle($title) |
57
|
|
|
{ |
58
|
3 |
|
$this->title = $title; |
59
|
|
|
|
60
|
3 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the icon. |
65
|
|
|
* |
66
|
|
|
* @param string $icon |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
3 |
|
public function setIcon($icon) |
71
|
|
|
{ |
72
|
3 |
|
$this->icon = $icon; |
73
|
|
|
|
74
|
3 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the attributes. |
79
|
|
|
* |
80
|
|
|
* @param array $attributes |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
3 |
|
public function setAttributes(array $attributes) |
85
|
|
|
{ |
86
|
3 |
|
$this->attributes = $attributes; |
87
|
|
|
|
88
|
3 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* ----------------------------------------------------------------- |
92
|
|
|
| Main Methods |
93
|
|
|
| ----------------------------------------------------------------- |
94
|
|
|
*/ |
95
|
|
|
/** |
96
|
|
|
* @param string $url |
97
|
|
|
* @param string $title |
98
|
|
|
* @param array $attributes |
99
|
|
|
* @param bool $withIcon |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
3 |
|
public function addLink($url, $title = null, array $attributes = [], $withIcon = true) |
104
|
|
|
{ |
105
|
3 |
|
$icon = $withIcon ? 'fa fa-fw fa-plus' : null; |
106
|
|
|
|
107
|
3 |
|
return $this->build('add', $url, $title, $attributes, $icon); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $action |
112
|
|
|
* @param string $url |
113
|
|
|
* @param string $title |
114
|
|
|
* @param array $attributes |
115
|
|
|
* @param bool $withIcon |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
3 |
|
public function build($action, $url, $title = null, array $attributes = [], $withIcon) |
120
|
|
|
{ |
121
|
3 |
|
if (is_null($title)) |
122
|
3 |
|
$title = "core::actions.{$action}"; |
123
|
|
|
|
124
|
3 |
|
return $this->setUrl($url) |
125
|
3 |
|
->setTitle($title) |
126
|
3 |
|
->setAttributes($attributes) |
127
|
3 |
|
->setIcon($withIcon ? $this->getIconFromAction($action) : null); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Render the link. |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Support\HtmlString |
134
|
|
|
*/ |
135
|
3 |
|
public function render() |
136
|
|
|
{ |
137
|
3 |
|
$attributes = html()->attributes($this->attributes); |
138
|
3 |
|
$value = $this->renderIcon() . $this->title(); |
139
|
|
|
|
140
|
3 |
|
return new HtmlString( |
141
|
3 |
|
'<a href="'.$this->url.'"'.$attributes.'>'.$value.'</a>' |
142
|
1 |
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get content as a string of HTML. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
3 |
|
public function toHtml() |
151
|
|
|
{ |
152
|
3 |
|
return $this->render()->toHtml(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* ----------------------------------------------------------------- |
156
|
|
|
| Check Functions |
157
|
|
|
| ----------------------------------------------------------------- |
158
|
|
|
*/ |
159
|
|
|
/** |
160
|
|
|
* Render the icon. |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
3 |
|
private function renderIcon() |
165
|
|
|
{ |
166
|
3 |
|
return $this->hasIcon() ? '<i class="'.$this->icon.'"></i> ' : ''; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check if the icon exists. |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
3 |
|
protected function hasIcon() |
175
|
|
|
{ |
176
|
3 |
|
return ! is_null($this->icon); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the title. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
3 |
|
private function title() |
185
|
|
|
{ |
186
|
|
|
/** @var \Illuminate\Translation\Translator $trans */ |
187
|
3 |
|
$trans = trans(); |
188
|
|
|
|
189
|
3 |
|
$title = $trans->has($this->title) ? $trans->get($this->title) : $this->title; |
190
|
|
|
|
191
|
3 |
|
return Str::ucfirst($title); |
192
|
|
|
} |
193
|
|
|
|
194
|
3 |
|
private function getIconFromAction($action) |
195
|
|
|
{ |
196
|
|
|
$icons = [ |
197
|
3 |
|
'add' => 'fa fa-fw fa-plus', |
198
|
1 |
|
]; |
199
|
|
|
|
200
|
3 |
|
return Arr::get($icons, $action); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: