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