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