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