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\Form; |
9
|
|
|
use Spatie\Html\Elements\Span; |
10
|
|
|
use Spatie\Html\Elements\File; |
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 $this->input('checkbox', $name, $value) |
95
|
|
|
->attributeIf((bool) $this->old($name, $checked), 'checked'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
100
|
|
|
* |
101
|
|
|
* @return \Spatie\Html\Elements\Div |
102
|
|
|
*/ |
103
|
|
|
public function div($contents = null) |
104
|
|
|
{ |
105
|
|
|
return Div::create()->children($contents); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string|null $name |
110
|
|
|
* @param string|null $value |
111
|
|
|
* |
112
|
|
|
* @return \Spatie\Html\Elements\Input |
113
|
|
|
*/ |
114
|
|
|
public function email($name = '', $value = '') |
115
|
|
|
{ |
116
|
|
|
return $this->input('email', $name, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $tag |
121
|
|
|
* |
122
|
|
|
* @return \Spatie\Html\Elements\Element |
123
|
|
|
*/ |
124
|
|
|
public function element($tag) |
125
|
|
|
{ |
126
|
|
|
return Element::withTag($tag); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string|null $type |
131
|
|
|
* @param string|null $name |
132
|
|
|
* @param string|null $value |
133
|
|
|
* |
134
|
|
|
* @return \Spatie\Html\Elements\Input |
135
|
|
|
*/ |
136
|
|
|
public function input($type = null, $name = null, $value = null) |
137
|
|
|
{ |
138
|
|
|
return Input::create() |
139
|
|
|
->attributeIf($type, 'type', $type) |
|
|
|
|
140
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
|
|
|
|
141
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
|
|
|
|
142
|
|
|
->attributeIf($name && $this->old($name, $value), 'value', $this->old($name, $value)); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $legend |
147
|
|
|
* |
148
|
|
|
* @return \Spatie\Html\Elements\Fieldset |
149
|
|
|
*/ |
150
|
|
|
public function fieldset($legend = null) |
151
|
|
|
{ |
152
|
|
|
return $legend ? |
153
|
|
|
Fieldset::create()->legend($legend) : |
154
|
|
|
Fieldset::create(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $method |
159
|
|
|
* @param string|null $action |
160
|
|
|
* |
161
|
|
|
* @return \Spatie\Html\Elements\Form |
162
|
|
|
*/ |
163
|
|
|
public function form($method = 'POST', $action = null) |
164
|
|
|
{ |
165
|
|
|
$method = strtoupper($method); |
166
|
|
|
$form = Form::create(); |
167
|
|
|
|
168
|
|
|
// If Laravel needs to spoof the form's method, we'll append a hidden |
169
|
|
|
// field containing the actual method |
170
|
|
|
if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) { |
171
|
|
|
$form = $form->addChild($this->hidden('_method')->value($method)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// On any other method that get, the form needs a CSRF token |
175
|
|
|
if ($method !== 'GET') { |
176
|
|
|
$form = $form->addChild($this->token()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $form |
180
|
|
|
->method($method === 'GET' ? 'GET' : 'POST') |
181
|
|
|
->attributeIf($action, 'action', $action); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string|null $name |
186
|
|
|
* @param string|null $value |
187
|
|
|
* |
188
|
|
|
* @return \Spatie\Html\Elements\Input |
189
|
|
|
*/ |
190
|
|
|
public function hidden($name = null, $value = null) |
191
|
|
|
{ |
192
|
|
|
return $this->input('hidden', $name, $value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
197
|
|
|
* @param string|null $for |
198
|
|
|
* |
199
|
|
|
* @return \Spatie\Html\Elements\Label |
200
|
|
|
*/ |
201
|
|
|
public function label($contents = null, $for = null) |
202
|
|
|
{ |
203
|
|
|
return Label::create() |
204
|
|
|
->attributeIf($for, 'for', $this->fieldName($for)) |
|
|
|
|
205
|
|
|
->children($contents); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
210
|
|
|
* |
211
|
|
|
* @return \Spatie\Html\Elements\Legend |
212
|
|
|
*/ |
213
|
|
|
public function legend($contents = null) |
214
|
|
|
{ |
215
|
|
|
return Legend::create()->html($contents); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $email |
220
|
|
|
* @param string|null $text |
221
|
|
|
* |
222
|
|
|
* @return \Spatie\Html\Elements\A |
223
|
|
|
*/ |
224
|
|
|
public function mailto($email, $text = null) |
225
|
|
|
{ |
226
|
|
|
return $this->a('mailto:'.$email, $text); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string|null $name |
231
|
|
|
* @param iterable $options |
232
|
|
|
* @param string|iterable|null $value |
233
|
|
|
* |
234
|
|
|
* @return \Spatie\Html\Elements\Select |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function multiselect($name = null, $options = [], $value = null) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
return Select::create() |
239
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
|
|
|
|
240
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
|
|
|
|
241
|
|
|
->options($options) |
|
|
|
|
242
|
|
|
->value($name ? $this->old($name, $value) : $value) |
243
|
|
|
->multiple(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string|null $text |
248
|
|
|
* @param string|null $value |
249
|
|
|
* @param bool $selected |
250
|
|
|
* |
251
|
|
|
* @return \Spatie\Html\Elements\Option |
252
|
|
|
*/ |
253
|
|
|
public function option($text = null, $value = null, $selected = false) |
254
|
|
|
{ |
255
|
|
|
return Option::create() |
256
|
|
|
->text($text) |
257
|
|
|
->value($value) |
258
|
|
|
->selectedIf($selected); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string|null $value |
|
|
|
|
263
|
|
|
* |
264
|
|
|
* @return \Spatie\Html\Elements\Input |
265
|
|
|
*/ |
266
|
|
|
public function password($name = null) |
267
|
|
|
{ |
268
|
|
|
return $this->input('password', $name); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param string|null $name |
273
|
|
|
* @param bool $checked |
274
|
|
|
* @param string|null $value |
275
|
|
|
* |
276
|
|
|
* @return \Spatie\Html\Elements\Input |
277
|
|
|
*/ |
278
|
|
|
public function radio($name = null, $checked = false, $value = null) |
279
|
|
|
{ |
280
|
|
|
return $this->input('radio', $name, $value) |
281
|
|
|
->attributeIf((bool) $this->old($name, $checked), 'checked'); |
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 select($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
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
302
|
|
|
* |
303
|
|
|
* @return \Spatie\Html\Elements\Span |
304
|
|
|
*/ |
305
|
|
|
public function span($contents = null) |
306
|
|
|
{ |
307
|
|
|
return Span::create()->children($contents); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param string|null $test |
|
|
|
|
312
|
|
|
* |
313
|
|
|
* @return \Spatie\Html\Elements\Button |
314
|
|
|
*/ |
315
|
|
|
public function submit($text = null) |
316
|
|
|
{ |
317
|
|
|
return $this->button($text, 'submit'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param string $number |
322
|
|
|
* @param string|null $text |
323
|
|
|
* |
324
|
|
|
* @return \Spatie\Html\Elements\A |
325
|
|
|
*/ |
326
|
|
|
public function tel($number, $text = null) |
327
|
|
|
{ |
328
|
|
|
return $this->a('tel:'.$number, $text); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param string|null $name |
333
|
|
|
* @param string|null $value |
334
|
|
|
* |
335
|
|
|
* @return \Spatie\Html\Elements\Input |
336
|
|
|
*/ |
337
|
|
|
public function text($name = null, $value = null) |
338
|
|
|
{ |
339
|
|
|
return $this->input('text', $name, $value); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param string|null $name |
344
|
|
|
* |
345
|
|
|
* @return \Spatie\Html\Elements\File |
346
|
|
|
*/ |
347
|
|
|
public function file($name = null) |
348
|
|
|
{ |
349
|
|
|
return File::create() |
350
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
|
|
|
|
351
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)); |
|
|
|
|
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @param string|null $name |
356
|
|
|
* @param string|null $value |
357
|
|
|
* |
358
|
|
|
* @return \Spatie\Html\Elements\Textarea |
359
|
|
|
*/ |
360
|
|
|
public function textarea($name = null, $value = null) |
361
|
|
|
{ |
362
|
|
|
return Textarea::create() |
363
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
|
|
|
|
364
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
|
|
|
|
365
|
|
|
->value($this->old($name, $value)); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return \Spatie\Html\Elements\Input |
370
|
|
|
*/ |
371
|
|
|
public function token() |
372
|
|
|
{ |
373
|
|
|
return $this->hidden('_token')->value($this->request->session()->token()); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param \ArrayAccess|array $model |
378
|
|
|
* |
379
|
|
|
* @return $this |
380
|
|
|
*/ |
381
|
|
|
public function model($model) |
382
|
|
|
{ |
383
|
|
|
$this->model = $model; |
384
|
|
|
|
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param \ArrayAccess|array $model |
390
|
|
|
* @param string|null $method |
391
|
|
|
* @param string|null $action |
392
|
|
|
* |
393
|
|
|
* @return \Spatie\Html\Elements\Form |
394
|
|
|
*/ |
395
|
|
|
public function modelForm($model, $method = 'POST', $action = null): Form |
396
|
|
|
{ |
397
|
|
|
$this->model($model); |
398
|
|
|
|
399
|
|
|
return $this->form($method, $action); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @return $this |
404
|
|
|
*/ |
405
|
|
|
public function endModel() |
406
|
|
|
{ |
407
|
|
|
$this->model = null; |
408
|
|
|
|
409
|
|
|
return $this; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
414
|
|
|
*/ |
415
|
|
|
public function closeModelForm(): Htmlable |
416
|
|
|
{ |
417
|
|
|
$this->endModel(); |
418
|
|
|
|
419
|
|
|
return $this->form()->close(); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param string $name |
424
|
|
|
* @param mixed $value |
425
|
|
|
* |
426
|
|
|
* @return mixed |
427
|
|
|
*/ |
428
|
|
|
protected function old($name, $value = null) |
429
|
|
|
{ |
430
|
|
|
if (empty($name)) { |
431
|
|
|
return; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
// If there's no default value provided, and the html builder currently |
435
|
|
|
// has a model assigned, try to retrieve a value from the model. |
436
|
|
|
if (empty($value) && $this->model) { |
437
|
|
|
$value = $this->model[$name] ?? ''; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
return $this->request->old($name, $value); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param string $name |
445
|
|
|
* |
446
|
|
|
* @return string |
447
|
|
|
*/ |
448
|
|
|
protected function fieldName($name) |
449
|
|
|
{ |
450
|
|
|
return $name; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
protected function ensureModelIsAvailable() |
454
|
|
|
{ |
455
|
|
|
if (empty($this->model)) { |
456
|
|
|
throw new Exception('Method requires a model to be set on the html builder'); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|