1
|
|
|
<?php namespace Arcanedev\LaravelHtml; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelHtml\Bases\Builder; |
4
|
|
|
use Arcanedev\LaravelHtml\Contracts\HtmlBuilder as HtmlBuilderContract; |
5
|
|
|
use Arcanedev\LaravelHtml\Helpers\Str; |
6
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HtmlBuilder |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\LaravelHtml |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class HtmlBuilder extends Builder implements HtmlBuilderContract |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Properties |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* The URL generator instance. |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Contracts\Routing\UrlGenerator |
24
|
|
|
*/ |
25
|
|
|
protected $url; |
26
|
|
|
|
27
|
|
|
/* ------------------------------------------------------------------------------------------------ |
28
|
|
|
| Constructor |
29
|
|
|
| ------------------------------------------------------------------------------------------------ |
30
|
|
|
*/ |
31
|
|
|
/** |
32
|
|
|
* Create a new HTML builder instance. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Contracts\Routing\UrlGenerator $url |
35
|
|
|
*/ |
36
|
426 |
|
public function __construct(UrlGenerator $url = null) |
37
|
|
|
{ |
38
|
426 |
|
$this->url = $url; |
39
|
426 |
|
} |
40
|
|
|
|
41
|
|
|
/* ------------------------------------------------------------------------------------------------ |
42
|
|
|
| Main Functions |
43
|
|
|
| ------------------------------------------------------------------------------------------------ |
44
|
|
|
*/ |
45
|
|
|
/** |
46
|
|
|
* Convert an HTML string to entities. |
47
|
|
|
* |
48
|
|
|
* @param string $value |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
39 |
|
public function entities($value) |
53
|
|
|
{ |
54
|
39 |
|
return Str::escape($value, false); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert all applicable characters to HTML entities. |
59
|
|
|
* |
60
|
|
|
* @param string $value |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
57 |
|
public function escape($value) |
65
|
|
|
{ |
66
|
57 |
|
return Str::escape($value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert entities to HTML characters. |
71
|
|
|
* |
72
|
|
|
* @param string $value |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
6 |
|
public function decode($value) |
77
|
|
|
{ |
78
|
6 |
|
return html_entity_decode($value, ENT_QUOTES, 'UTF-8'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate a link to a JavaScript file. |
83
|
|
|
* |
84
|
|
|
* @param string $url |
85
|
|
|
* @param array $attributes |
86
|
|
|
* @param bool $secure |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Support\HtmlString |
89
|
|
|
*/ |
90
|
6 |
|
public function script($url, $attributes = [], $secure = null) |
91
|
|
|
{ |
92
|
6 |
|
$attributes['src'] = $this->url->asset($url, $secure); |
93
|
|
|
|
94
|
6 |
|
return $this->toHtmlString( |
95
|
6 |
|
'<script' . $this->attributes($attributes) . '></script>' . PHP_EOL |
96
|
2 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Generate a link to a CSS file. |
101
|
|
|
* |
102
|
|
|
* @param string $url |
103
|
|
|
* @param array $attributes |
104
|
|
|
* @param bool $secure |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Support\HtmlString |
107
|
|
|
*/ |
108
|
6 |
|
public function style($url, $attributes = [], $secure = null) |
109
|
|
|
{ |
110
|
|
|
$attributes = $attributes + [ |
111
|
6 |
|
'media' => 'all', |
112
|
2 |
|
'type' => 'text/css', |
113
|
|
|
'rel' => 'stylesheet' |
114
|
2 |
|
]; |
115
|
6 |
|
$attributes['href'] = $this->url->asset($url, $secure); |
116
|
|
|
|
117
|
6 |
|
return $this->toHtmlString( |
118
|
6 |
|
'<link' . $this->attributes($attributes) . '>' . PHP_EOL |
119
|
2 |
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Generate an HTML image element. |
124
|
|
|
* |
125
|
|
|
* @param string $url |
126
|
|
|
* @param string $alt |
127
|
|
|
* @param array $attributes |
128
|
|
|
* @param bool $secure |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Support\HtmlString |
131
|
|
|
*/ |
132
|
6 |
|
public function image($url, $alt = null, $attributes = [], $secure = null) |
133
|
|
|
{ |
134
|
6 |
|
$attributes['alt'] = $alt; |
135
|
|
|
|
136
|
6 |
|
return $this->toHtmlString( |
137
|
6 |
|
'<img src="' . $this->url->asset($url, $secure) . '"' . $this->attributes($attributes) . '>' |
138
|
2 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Generate a link to a Favicon file. |
143
|
|
|
* |
144
|
|
|
* @param string $url |
145
|
|
|
* @param array $attributes |
146
|
|
|
* @param bool $secure |
147
|
|
|
* |
148
|
|
|
* @return \Illuminate\Support\HtmlString |
149
|
|
|
*/ |
150
|
6 |
|
public function favicon($url, $attributes = [], $secure = null) |
151
|
|
|
{ |
152
|
6 |
|
$attributes = array_merge($attributes, [ |
153
|
6 |
|
'rel' => 'shortcut icon', |
154
|
6 |
|
'type' => 'image/x-icon', |
155
|
6 |
|
'href' => $this->url->asset($url, $secure) |
156
|
2 |
|
]); |
157
|
|
|
|
158
|
6 |
|
return $this->toHtmlString( |
159
|
6 |
|
'<link' . $this->attributes($attributes) . '>' . PHP_EOL |
160
|
2 |
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Generate a HTML link. |
165
|
|
|
* |
166
|
|
|
* @param string $url |
167
|
|
|
* @param string $title |
168
|
|
|
* @param array $attributes |
169
|
|
|
* @param bool $secure |
170
|
|
|
* @param bool $escaped |
171
|
|
|
* |
172
|
|
|
* @return \Illuminate\Support\HtmlString |
173
|
|
|
*/ |
174
|
27 |
|
public function link($url, $title = null, $attributes = [], $secure = null, $escaped = true) |
175
|
|
|
{ |
176
|
27 |
|
$url = $this->url->to($url, [], $secure); |
177
|
|
|
|
178
|
27 |
|
if (is_null($title) || $title === false) |
179
|
17 |
|
$title = $url; |
180
|
|
|
|
181
|
27 |
|
return $this->toHtmlString( |
182
|
27 |
|
'<a href="' . $url . '"' . $this->attributes($attributes) . '>' . |
183
|
27 |
|
($escaped ? $this->entities($title) : $title). |
184
|
18 |
|
'</a>' |
185
|
9 |
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Generate a HTTPS HTML link. |
190
|
|
|
* |
191
|
|
|
* @param string $url |
192
|
|
|
* @param string $title |
193
|
|
|
* @param array $attributes |
194
|
|
|
* @param bool $escaped |
195
|
|
|
* |
196
|
|
|
* @return \Illuminate\Support\HtmlString |
197
|
|
|
*/ |
198
|
6 |
|
public function secureLink($url, $title = null, $attributes = [], $escaped = true) |
199
|
|
|
{ |
200
|
6 |
|
return $this->link($url, $title, $attributes, true, $escaped); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Generate a HTML link to an asset. |
205
|
|
|
* |
206
|
|
|
* @param string $url |
207
|
|
|
* @param string $title |
208
|
|
|
* @param array $attributes |
209
|
|
|
* @param bool $secure |
210
|
|
|
* |
211
|
|
|
* @return \Illuminate\Support\HtmlString |
212
|
|
|
*/ |
213
|
9 |
|
public function linkAsset($url, $title = null, $attributes = [], $secure = null) |
214
|
|
|
{ |
215
|
9 |
|
$url = $this->url->asset($url, $secure); |
216
|
|
|
|
217
|
9 |
|
return $this->link($url, $title ?: $url, $attributes, $secure); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Generate a HTTPS HTML link to an asset. |
222
|
|
|
* |
223
|
|
|
* @param string $url |
224
|
|
|
* @param string $title |
225
|
|
|
* @param array $attributes |
226
|
|
|
* |
227
|
|
|
* @return \Illuminate\Support\HtmlString |
228
|
|
|
*/ |
229
|
3 |
|
public function linkSecureAsset($url, $title = null, $attributes = []) |
230
|
|
|
{ |
231
|
3 |
|
return $this->linkAsset($url, $title, $attributes, true); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Generate a HTML link to a named route. |
236
|
|
|
* |
237
|
|
|
* @param string $name |
238
|
|
|
* @param string $title |
239
|
|
|
* @param array $parameters |
240
|
|
|
* @param array $attributes |
241
|
|
|
* @param bool $escaped |
242
|
|
|
* |
243
|
|
|
* @return \Illuminate\Support\HtmlString |
244
|
|
|
*/ |
245
|
3 |
|
public function linkRoute($name, $title = null, $parameters = [], $attributes = [], $escaped = true) |
246
|
|
|
{ |
247
|
3 |
|
$url = $this->url->route($name, $parameters); |
248
|
|
|
|
249
|
3 |
|
return $this->link($url, $title, $attributes, null, $escaped); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Generate a HTML link to a controller action. |
254
|
|
|
* |
255
|
|
|
* @param string $action |
256
|
|
|
* @param string $title |
257
|
|
|
* @param array $parameters |
258
|
|
|
* @param array $attributes |
259
|
|
|
* @param bool $escaped |
260
|
|
|
* |
261
|
|
|
* @return \Illuminate\Support\HtmlString |
262
|
|
|
*/ |
263
|
3 |
|
public function linkAction($action, $title = null, $parameters = [], $attributes = [], $escaped = true) |
264
|
|
|
{ |
265
|
3 |
|
$url = $this->url->action($action, $parameters); |
266
|
|
|
|
267
|
3 |
|
return $this->link($url, $title, $attributes, null, $escaped); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Generate a HTML link to an email address. |
272
|
|
|
* |
273
|
|
|
* @param string $email |
274
|
|
|
* @param string $title |
275
|
|
|
* @param array $attributes |
276
|
|
|
* @param bool $escaped |
277
|
|
|
* |
278
|
|
|
* @return \Illuminate\Support\HtmlString |
279
|
|
|
*/ |
280
|
3 |
|
public function mailto($email, $title = null, $attributes = [], $escaped = true) |
281
|
|
|
{ |
282
|
3 |
|
$email = $this->email($email); |
283
|
3 |
|
$title = $title ?: $email; |
284
|
3 |
|
$email = $this->obfuscate('mailto:') . $email; |
285
|
|
|
|
286
|
3 |
|
return $this->toHtmlString( |
287
|
3 |
|
'<a href="' . $email . '"' . $this->attributes($attributes) . '>' . |
288
|
3 |
|
($escaped ? $this->entities($title) : $title) . |
289
|
2 |
|
'</a>' |
290
|
1 |
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Obfuscate an e-mail address to prevent spam-bots from sniffing it. |
295
|
|
|
* |
296
|
|
|
* @param string $email |
297
|
|
|
* |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
3 |
|
public function email($email) |
301
|
|
|
{ |
302
|
3 |
|
return str_replace('@', '@', $this->obfuscate($email)); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Generate an ordered list of items. |
307
|
|
|
* |
308
|
|
|
* @param array $list |
309
|
|
|
* @param array $attributes |
310
|
|
|
* |
311
|
|
|
* @return \Illuminate\Support\HtmlString |
312
|
|
|
*/ |
313
|
6 |
|
public function ol(array $list, array $attributes = []) |
314
|
|
|
{ |
315
|
6 |
|
return $this->toHtmlString( |
316
|
6 |
|
Helpers\Lister::ol($list, $attributes) |
317
|
2 |
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Generate an un-ordered list of items. |
322
|
|
|
* |
323
|
|
|
* @param array $list |
324
|
|
|
* @param array $attributes |
325
|
|
|
* |
326
|
|
|
* @return \Illuminate\Support\HtmlString |
327
|
|
|
*/ |
328
|
6 |
|
public function ul(array $list, array $attributes = []) |
329
|
|
|
{ |
330
|
6 |
|
return $this->toHtmlString( |
331
|
6 |
|
Helpers\Lister::ul($list, $attributes) |
332
|
2 |
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Generate a description list of items. |
337
|
|
|
* |
338
|
|
|
* @param array $list |
339
|
|
|
* @param array $attributes |
340
|
|
|
* |
341
|
|
|
* @return \Illuminate\Support\HtmlString |
342
|
|
|
*/ |
343
|
9 |
|
public function dl(array $list, array $attributes = []) |
344
|
|
|
{ |
345
|
9 |
|
return $this->toHtmlString( |
346
|
9 |
|
Helpers\Lister::dl($list, $attributes) |
347
|
2 |
|
); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Generates non-breaking space entities based on a supplied multiplier. |
352
|
|
|
* |
353
|
|
|
* @param int $multiplier |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
3 |
|
public function nbsp($multiplier = 1) |
358
|
|
|
{ |
359
|
3 |
|
return str_repeat(' ', $multiplier); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Build an HTML attribute string from an array. |
364
|
|
|
* |
365
|
|
|
* @param array $attributes |
366
|
|
|
* |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
330 |
|
public function attributes(array $attributes) |
370
|
|
|
{ |
371
|
330 |
|
return Helpers\Attributes::make($attributes); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Obfuscate a string to prevent spam-bots from sniffing it. |
376
|
|
|
* |
377
|
|
|
* @param string $value |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
6 |
|
public function obfuscate($value) |
382
|
|
|
{ |
383
|
6 |
|
return Helpers\Obfuscater::make($value); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Generate a meta tag. |
388
|
|
|
* |
389
|
|
|
* @param string $name |
390
|
|
|
* @param string $content |
391
|
|
|
* @param array $attributes |
392
|
|
|
* |
393
|
|
|
* @return \Illuminate\Support\HtmlString |
394
|
|
|
*/ |
395
|
12 |
|
public function meta($name, $content, array $attributes = []) |
396
|
|
|
{ |
397
|
12 |
|
return $this->toHtmlString( |
398
|
12 |
|
Helpers\Meta::make($name, $content, $attributes) |
399
|
4 |
|
); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Generate a HTML link to an phone number (call). |
404
|
|
|
* |
405
|
|
|
* @param string $phone |
406
|
|
|
* @param string $title |
407
|
|
|
* @param array $attributes |
408
|
|
|
* @param bool $escaped |
409
|
|
|
* |
410
|
|
|
* @return \Illuminate\Support\HtmlString |
411
|
|
|
*/ |
412
|
3 |
|
public function tel($phone, $title = null, $attributes = [], $escaped = true) |
413
|
|
|
{ |
414
|
3 |
|
$title = $title ?: $phone; |
415
|
|
|
|
416
|
3 |
|
return $this->toHtmlString( |
417
|
3 |
|
'<a href="tel:'.$phone.'"'.$this->attributes($attributes).'>'. |
418
|
3 |
|
($escaped ? $this->entities($title):$title). |
419
|
2 |
|
'</a>' |
420
|
1 |
|
); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|