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