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