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