1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Html; |
4
|
|
|
|
5
|
|
|
use Spatie\Html\Elements\A; |
6
|
|
|
use Spatie\Html\Elements\I; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Spatie\Html\Elements\Div; |
9
|
|
|
use Spatie\Html\Elements\Img; |
10
|
|
|
use Spatie\Html\Elements\File; |
11
|
|
|
use Spatie\Html\Elements\Form; |
12
|
|
|
use Spatie\Html\Elements\Span; |
13
|
|
|
use Spatie\Html\Elements\Input; |
14
|
|
|
use Spatie\Html\Elements\Label; |
15
|
|
|
use Spatie\Html\Elements\Button; |
16
|
|
|
use Spatie\Html\Elements\Legend; |
17
|
|
|
use Spatie\Html\Elements\Option; |
18
|
|
|
use Spatie\Html\Elements\Select; |
19
|
|
|
use Spatie\Html\Elements\Element; |
20
|
|
|
use Illuminate\Support\Collection; |
21
|
|
|
use Illuminate\Support\HtmlString; |
22
|
|
|
use Spatie\Html\Elements\Fieldset; |
23
|
|
|
use Spatie\Html\Elements\Textarea; |
24
|
|
|
use Illuminate\Support\Traits\Macroable; |
25
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
26
|
|
|
|
27
|
|
|
class Html |
28
|
|
|
{ |
29
|
|
|
use Macroable; |
30
|
|
|
|
31
|
|
|
/** @var \Illuminate\Http\Request */ |
32
|
|
|
protected $request; |
33
|
|
|
|
34
|
|
|
/** @var \ArrayAccess|array */ |
35
|
|
|
protected $model; |
36
|
|
|
|
37
|
|
|
public function __construct(Request $request) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this->request = $request; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|null $href |
44
|
|
|
* @param string|null $text |
|
|
|
|
45
|
|
|
* |
46
|
|
|
* @return \Spatie\Html\Elements\A |
47
|
|
|
*/ |
48
|
|
|
public function a($href = null, $contents = null) |
49
|
|
|
{ |
50
|
|
|
return A::create() |
|
|
|
|
51
|
|
|
->attributeIf($href, 'href', $href) |
52
|
|
|
->html($contents); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|null $href |
|
|
|
|
57
|
|
|
* @param string|null $text |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @return \Spatie\Html\Elements\I |
60
|
|
|
*/ |
61
|
|
|
public function i($contents = null) |
62
|
|
|
{ |
63
|
|
|
return I::create() |
64
|
|
|
->html($contents); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string|null $type |
69
|
|
|
* @param string|null $text |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return \Spatie\Html\Elements\Button |
72
|
|
|
*/ |
73
|
|
|
public function button($contents = null, $type = null) |
74
|
|
|
{ |
75
|
|
|
return Button::create() |
|
|
|
|
76
|
|
|
->attributeIf($type, 'type', $type) |
77
|
|
|
->html($contents); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \Illuminate\Support\Collection|iterable|string $classes |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
84
|
|
|
*/ |
85
|
|
|
public function class($classes): Htmlable |
86
|
|
|
{ |
87
|
|
|
if ($classes instanceof Collection) { |
88
|
|
|
$classes = $classes->toArray(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$attributes = new Attributes(); |
92
|
|
|
$attributes->addClass($classes); |
93
|
|
|
|
94
|
|
|
return new HtmlString( |
95
|
|
|
$attributes->render() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string|null $name |
101
|
|
|
* @param bool $checked |
102
|
|
|
* @param string|null $value |
103
|
|
|
* |
104
|
|
|
* @return \Spatie\Html\Elements\Input |
105
|
|
|
*/ |
106
|
|
|
public function checkbox($name = null, $checked = false, $value = '1') |
107
|
|
|
{ |
108
|
|
|
return Input::create() |
|
|
|
|
109
|
|
|
->attribute('type', 'checkbox') |
110
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
111
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
112
|
|
|
->attributeIf(! is_null($value), 'value', $value) |
113
|
|
|
->attributeIf((bool) $this->old($name, $checked), 'checked'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
118
|
|
|
* |
119
|
|
|
* @return \Spatie\Html\Elements\Div |
120
|
|
|
*/ |
121
|
|
|
public function div($contents = null) |
122
|
|
|
{ |
123
|
|
|
return Div::create()->children($contents); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string|null $name |
128
|
|
|
* @param string|null $value |
129
|
|
|
* |
130
|
|
|
* @return \Spatie\Html\Elements\Input |
131
|
|
|
*/ |
132
|
|
|
public function email($name = '', $value = '') |
133
|
|
|
{ |
134
|
|
|
return $this->input('email', $name, $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string|null $name |
139
|
|
|
* @param string|null $value |
140
|
|
|
* |
141
|
|
|
* @return \Spatie\Html\Elements\Input |
142
|
|
|
*/ |
143
|
|
|
public function date($name = '', $value = '') |
144
|
|
|
{ |
145
|
|
|
return $this->input('date', $name, $value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string|null $name |
150
|
|
|
* @param string|null $value |
151
|
|
|
* |
152
|
|
|
* @return \Spatie\Html\Elements\Input |
153
|
|
|
*/ |
154
|
|
|
public function time($name = '', $value = '') |
155
|
|
|
{ |
156
|
|
|
return $this->input('time', $name, $value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $tag |
161
|
|
|
* |
162
|
|
|
* @return \Spatie\Html\Elements\Element |
163
|
|
|
*/ |
164
|
|
|
public function element($tag) |
165
|
|
|
{ |
166
|
|
|
return Element::withTag($tag); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string|null $type |
171
|
|
|
* @param string|null $name |
172
|
|
|
* @param string|null $value |
173
|
|
|
* |
174
|
|
|
* @return \Spatie\Html\Elements\Input |
175
|
|
|
*/ |
176
|
|
|
public function input($type = null, $name = null, $value = null) |
177
|
|
|
{ |
178
|
|
|
$hasValue = $name && (! is_null($this->old($name, $value)) || ! is_null($value)); |
|
|
|
|
179
|
|
|
|
180
|
|
|
return Input::create() |
|
|
|
|
181
|
|
|
->attributeIf($type, 'type', $type) |
182
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
183
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
184
|
|
|
->attributeIf($hasValue, 'value', $this->old($name, $value)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $legend |
189
|
|
|
* |
190
|
|
|
* @return \Spatie\Html\Elements\Fieldset |
191
|
|
|
*/ |
192
|
|
|
public function fieldset($legend = null) |
193
|
|
|
{ |
194
|
|
|
return $legend ? |
195
|
|
|
Fieldset::create()->legend($legend) : |
196
|
|
|
Fieldset::create(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $method |
201
|
|
|
* @param string|null $action |
202
|
|
|
* |
203
|
|
|
* @return \Spatie\Html\Elements\Form |
204
|
|
|
*/ |
205
|
|
|
public function form($method = 'POST', $action = null) |
206
|
|
|
{ |
207
|
|
|
$method = strtoupper($method); |
208
|
|
|
$form = Form::create(); |
209
|
|
|
|
210
|
|
|
// If Laravel needs to spoof the form's method, we'll append a hidden |
211
|
|
|
// field containing the actual method |
212
|
|
|
if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) { |
213
|
|
|
$form = $form->addChild($this->hidden('_method')->value($method)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// On any other method that get, the form needs a CSRF token |
217
|
|
|
if ($method !== 'GET') { |
218
|
|
|
$form = $form->addChild($this->token()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $form |
|
|
|
|
222
|
|
|
->method($method === 'GET' ? 'GET' : 'POST') |
223
|
|
|
->attributeIf($action, 'action', $action); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string|null $name |
228
|
|
|
* @param string|null $value |
229
|
|
|
* |
230
|
|
|
* @return \Spatie\Html\Elements\Input |
231
|
|
|
*/ |
232
|
|
|
public function hidden($name = null, $value = null) |
233
|
|
|
{ |
234
|
|
|
return $this->input('hidden', $name, $value); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string|null $src |
239
|
|
|
* @param string|null $alt |
240
|
|
|
* |
241
|
|
|
* @return \Spatie\Html\Elements\Img |
242
|
|
|
*/ |
243
|
|
|
public function img($src = null, $alt = null) |
244
|
|
|
{ |
245
|
|
|
return Img::create() |
|
|
|
|
246
|
|
|
->attributeIf($src, 'src', $src) |
247
|
|
|
->attributeIf($alt, 'alt', $alt); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
252
|
|
|
* @param string|null $for |
253
|
|
|
* |
254
|
|
|
* @return \Spatie\Html\Elements\Label |
255
|
|
|
*/ |
256
|
|
|
public function label($contents = null, $for = null) |
257
|
|
|
{ |
258
|
|
|
return Label::create() |
|
|
|
|
259
|
|
|
->attributeIf($for, 'for', $this->fieldName($for)) |
260
|
|
|
->children($contents); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
265
|
|
|
* |
266
|
|
|
* @return \Spatie\Html\Elements\Legend |
267
|
|
|
*/ |
268
|
|
|
public function legend($contents = null) |
269
|
|
|
{ |
270
|
|
|
return Legend::create()->html($contents); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $email |
275
|
|
|
* @param string|null $text |
276
|
|
|
* |
277
|
|
|
* @return \Spatie\Html\Elements\A |
278
|
|
|
*/ |
279
|
|
|
public function mailto($email, $text = null) |
280
|
|
|
{ |
281
|
|
|
return $this->a('mailto:'.$email, $text ?: $email); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param string|null $name |
286
|
|
|
* @param iterable $options |
287
|
|
|
* @param string|iterable|null $value |
288
|
|
|
* |
289
|
|
|
* @return \Spatie\Html\Elements\Select |
290
|
|
|
*/ |
291
|
|
View Code Duplication |
public function multiselect($name = null, $options = [], $value = null) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
return Select::create() |
|
|
|
|
294
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
295
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
296
|
|
|
->options($options) |
297
|
|
|
->value($name ? $this->old($name, $value) : $value) |
298
|
|
|
->multiple(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string|null $text |
303
|
|
|
* @param string|null $value |
304
|
|
|
* @param bool $selected |
305
|
|
|
* |
306
|
|
|
* @return \Spatie\Html\Elements\Option |
307
|
|
|
*/ |
308
|
|
|
public function option($text = null, $value = null, $selected = false) |
309
|
|
|
{ |
310
|
|
|
return Option::create() |
311
|
|
|
->text($text) |
312
|
|
|
->value($value) |
313
|
|
|
->selectedIf($selected); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param string|null $value |
|
|
|
|
318
|
|
|
* |
319
|
|
|
* @return \Spatie\Html\Elements\Input |
320
|
|
|
*/ |
321
|
|
|
public function password($name = null) |
322
|
|
|
{ |
323
|
|
|
return $this->input('password', $name); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string|null $name |
328
|
|
|
* @param bool $checked |
329
|
|
|
* @param string|null $value |
330
|
|
|
* |
331
|
|
|
* @return \Spatie\Html\Elements\Input |
332
|
|
|
*/ |
333
|
|
|
public function radio($name = null, $checked = false, $value = null) |
334
|
|
|
{ |
335
|
|
|
return $this->input('radio', $name, $value) |
|
|
|
|
336
|
|
|
->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value))) |
337
|
|
|
->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked'); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param string|null $name |
342
|
|
|
* @param iterable $options |
343
|
|
|
* @param string|iterable|null $value |
344
|
|
|
* |
345
|
|
|
* @return \Spatie\Html\Elements\Select |
346
|
|
|
*/ |
347
|
|
View Code Duplication |
public function select($name = null, $options = [], $value = null) |
|
|
|
|
348
|
|
|
{ |
349
|
|
|
return Select::create() |
|
|
|
|
350
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
351
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
352
|
|
|
->options($options) |
353
|
|
|
->value($name ? $this->old($name, $value) : $value); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
358
|
|
|
* |
359
|
|
|
* @return \Spatie\Html\Elements\Span |
360
|
|
|
*/ |
361
|
|
|
public function span($contents = null) |
362
|
|
|
{ |
363
|
|
|
return Span::create()->children($contents); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string|null $text |
368
|
|
|
* |
369
|
|
|
* @return \Spatie\Html\Elements\Button |
370
|
|
|
*/ |
371
|
|
|
public function submit($text = null) |
372
|
|
|
{ |
373
|
|
|
return $this->button($text, 'submit'); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param string|null $text |
378
|
|
|
* |
379
|
|
|
* @return \Spatie\Html\Elements\Button |
380
|
|
|
*/ |
381
|
|
|
public function reset($text = null) |
382
|
|
|
{ |
383
|
|
|
return $this->button($text, 'reset'); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @param string $number |
388
|
|
|
* @param string|null $text |
389
|
|
|
* |
390
|
|
|
* @return \Spatie\Html\Elements\A |
391
|
|
|
*/ |
392
|
|
|
public function tel($number, $text = null) |
393
|
|
|
{ |
394
|
|
|
return $this->a('tel:'.$number, $text ?: $number); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param string|null $name |
399
|
|
|
* @param string|null $value |
400
|
|
|
* |
401
|
|
|
* @return \Spatie\Html\Elements\Input |
402
|
|
|
*/ |
403
|
|
|
public function text($name = null, $value = null) |
404
|
|
|
{ |
405
|
|
|
return $this->input('text', $name, $value); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @param string|null $name |
410
|
|
|
* |
411
|
|
|
* @return \Spatie\Html\Elements\File |
412
|
|
|
*/ |
413
|
|
|
public function file($name = null) |
414
|
|
|
{ |
415
|
|
|
return File::create() |
|
|
|
|
416
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
417
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @param string|null $name |
422
|
|
|
* @param string|null $value |
423
|
|
|
* |
424
|
|
|
* @return \Spatie\Html\Elements\Textarea |
425
|
|
|
*/ |
426
|
|
|
public function textarea($name = null, $value = null) |
427
|
|
|
{ |
428
|
|
|
return Textarea::create() |
|
|
|
|
429
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
430
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
431
|
|
|
->value($this->old($name, $value)); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @return \Spatie\Html\Elements\Input |
436
|
|
|
*/ |
437
|
|
|
public function token() |
438
|
|
|
{ |
439
|
|
|
return $this |
440
|
|
|
->hidden() |
441
|
|
|
->name('_token') |
442
|
|
|
->value($this->request->session()->token()); |
|
|
|
|
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* @param \ArrayAccess|array $model |
447
|
|
|
* |
448
|
|
|
* @return $this |
449
|
|
|
*/ |
450
|
|
|
public function model($model) |
451
|
|
|
{ |
452
|
|
|
$this->model = $model; |
453
|
|
|
|
454
|
|
|
return $this; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @param \ArrayAccess|array $model |
459
|
|
|
* @param string|null $method |
460
|
|
|
* @param string|null $action |
461
|
|
|
* |
462
|
|
|
* @return \Spatie\Html\Elements\Form |
463
|
|
|
*/ |
464
|
|
|
public function modelForm($model, $method = 'POST', $action = null): Form |
465
|
|
|
{ |
466
|
|
|
$this->model($model); |
467
|
|
|
|
468
|
|
|
return $this->form($method, $action); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @return $this |
473
|
|
|
*/ |
474
|
|
|
public function endModel() |
475
|
|
|
{ |
476
|
|
|
$this->model = null; |
477
|
|
|
|
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
483
|
|
|
*/ |
484
|
|
|
public function closeModelForm(): Htmlable |
485
|
|
|
{ |
486
|
|
|
$this->endModel(); |
487
|
|
|
|
488
|
|
|
return $this->form()->close(); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @param string $name |
493
|
|
|
* @param mixed $value |
494
|
|
|
* |
495
|
|
|
* @return mixed |
496
|
|
|
*/ |
497
|
|
|
protected function old($name, $value = null) |
498
|
|
|
{ |
499
|
|
|
if (empty($name)) { |
500
|
|
|
return; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// Convert array format (sth[1]) to dot notation (sth.1) |
504
|
|
|
$name = preg_replace('/\[(.+)\]/U', '.$1', $name); |
505
|
|
|
|
506
|
|
|
// If there's no default value provided, and the html builder currently |
507
|
|
|
// has a model assigned, try to retrieve a value from the model. |
508
|
|
|
if (empty($value) && $this->model) { |
509
|
|
|
$value = data_get($this->model, $name) ?? ''; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
return $this->request->old($name, $value); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Retrieve the value from the current session or assigned model. This is |
517
|
|
|
* a public alias for `old`. |
518
|
|
|
* |
519
|
|
|
* @param string $name |
520
|
|
|
* @param mixed $value |
|
|
|
|
521
|
|
|
* |
522
|
|
|
* @return mixed |
523
|
|
|
*/ |
524
|
|
|
public function value($name, $default = null) |
525
|
|
|
{ |
526
|
|
|
return $this->old($name, $default); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @param string $name |
531
|
|
|
* |
532
|
|
|
* @return string |
533
|
|
|
*/ |
534
|
|
|
protected function fieldName($name) |
535
|
|
|
{ |
536
|
|
|
return $name; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
protected function ensureModelIsAvailable() |
540
|
|
|
{ |
541
|
|
|
if (empty($this->model)) { |
542
|
|
|
throw new Exception('Method requires a model to be set on the html builder'); |
543
|
|
|
} |
544
|
|
|
} |
545
|
|
|
} |
546
|
|
|
|