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