1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Html; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\Html\Elements\A; |
8
|
|
|
use Spatie\Html\Elements\I; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Spatie\Html\Elements\Div; |
11
|
|
|
use Spatie\Html\Elements\Img; |
12
|
|
|
use Spatie\Html\Elements\File; |
13
|
|
|
use Spatie\Html\Elements\Form; |
14
|
|
|
use Spatie\Html\Elements\Span; |
15
|
|
|
use Spatie\Html\Elements\Input; |
16
|
|
|
use Spatie\Html\Elements\Label; |
17
|
|
|
use Spatie\Html\Elements\Button; |
18
|
|
|
use Spatie\Html\Elements\Legend; |
19
|
|
|
use Spatie\Html\Elements\Option; |
20
|
|
|
use Spatie\Html\Elements\Select; |
21
|
|
|
use Spatie\Html\Elements\Element; |
22
|
|
|
use Illuminate\Support\Collection; |
23
|
|
|
use Illuminate\Support\HtmlString; |
24
|
|
|
use Spatie\Html\Elements\Fieldset; |
25
|
|
|
use Spatie\Html\Elements\Textarea; |
26
|
|
|
use Illuminate\Support\Traits\Macroable; |
27
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
28
|
|
|
|
29
|
|
|
class Html |
30
|
|
|
{ |
31
|
|
|
use Macroable; |
32
|
|
|
|
33
|
|
|
const HTML_DATE_FORMAT = 'Y-m-d'; |
34
|
|
|
const HTML_TIME_FORMAT = 'H:i:s'; |
35
|
|
|
|
36
|
|
|
/** @var \Illuminate\Http\Request */ |
37
|
|
|
protected $request; |
38
|
|
|
|
39
|
|
|
/** @var \ArrayAccess|array */ |
40
|
|
|
protected $model; |
41
|
|
|
|
42
|
|
|
public function __construct(Request $request) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->request = $request; |
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
|
|
|
*/ |
66
|
|
|
public function i($contents = null) |
67
|
|
|
{ |
68
|
|
|
return I::create() |
69
|
|
|
->html($contents); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|null $type |
74
|
|
|
* @param string|null $text |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return \Spatie\Html\Elements\Button |
77
|
|
|
*/ |
78
|
|
|
public function button($contents = null, $type = null, $name = null) |
79
|
|
|
{ |
80
|
|
|
return Button::create() |
|
|
|
|
81
|
|
|
->attributeIf($type, 'type', $type) |
82
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
83
|
|
|
->html($contents); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Illuminate\Support\Collection|iterable|string $classes |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
90
|
|
|
*/ |
91
|
|
|
public function class($classes): Htmlable |
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 = null, $value = '1') |
113
|
|
|
{ |
114
|
|
|
return $this->input('checkbox', $name, $value) |
|
|
|
|
115
|
|
|
->attributeIf(!is_null($value), 'value', $value) |
116
|
|
|
->attributeIf((bool) $this->old($name, $checked), 'checked'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
121
|
|
|
* |
122
|
|
|
* @return \Spatie\Html\Elements\Div |
123
|
|
|
*/ |
124
|
|
|
public function div($contents = null) |
125
|
|
|
{ |
126
|
|
|
return Div::create()->children($contents); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string|null $name |
131
|
|
|
* @param string|null $value |
132
|
|
|
* |
133
|
|
|
* @return \Spatie\Html\Elements\Input |
134
|
|
|
*/ |
135
|
|
|
public function email($name = null, $value = null) |
136
|
|
|
{ |
137
|
|
|
return $this->input('email', $name, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string|null $name |
142
|
|
|
* @param string|null $value |
143
|
|
|
* @param bool $format |
144
|
|
|
* |
145
|
|
|
* @return \Spatie\Html\Elements\Input |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function date($name = '', $value = null, $format = true) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$element = $this->input('date', $name, $value); |
150
|
|
|
|
151
|
|
|
if (!$format || empty($element->getAttribute('value'))) { |
152
|
|
|
return $element; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string|null $name |
160
|
|
|
* @param string|null $value |
161
|
|
|
* @param string|null $min |
162
|
|
|
* @param string|null $max |
163
|
|
|
* @param string|null $step |
164
|
|
|
* |
165
|
|
|
* @return \Spatie\Html\Elements\Input |
166
|
|
|
*/ |
167
|
|
|
public function range($name = '', $value = '', $min = null, $max = null, $step = null) |
168
|
|
|
{ |
169
|
|
|
return $this->input('range', $name, $value) |
|
|
|
|
170
|
|
|
->attributeIfNotNull($min, 'min', $min) |
171
|
|
|
->attributeIfNotNull($max, 'max', $max) |
172
|
|
|
->attributeIfNotNull($step, 'step', $step); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string|null $name |
177
|
|
|
* @param string|null $value |
178
|
|
|
* @param bool $format |
179
|
|
|
* |
180
|
|
|
* @return \Spatie\Html\Elements\Input |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function time($name = '', $value = null, $format = true) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$element = $this->input('time', $name, $value); |
185
|
|
|
|
186
|
|
|
if (!$format || empty($element->getAttribute('value'))) { |
187
|
|
|
return $element; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $tag |
195
|
|
|
* |
196
|
|
|
* @return \Spatie\Html\Elements\Element |
197
|
|
|
*/ |
198
|
|
|
public function element($tag) |
199
|
|
|
{ |
200
|
|
|
return Element::withTag($tag); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string|null $type |
205
|
|
|
* @param string|null $name |
206
|
|
|
* @param string|null $value |
207
|
|
|
* |
208
|
|
|
* @return \Spatie\Html\Elements\Input |
209
|
|
|
*/ |
210
|
|
|
public function input($type = null, $name = null, $value = null) |
211
|
|
|
{ |
212
|
|
|
$hasValue = $name && ($type !== 'password' && !is_null($this->old($name, $value)) || !is_null($value)); |
|
|
|
|
213
|
|
|
|
214
|
|
|
return Input::create() |
|
|
|
|
215
|
|
|
->attributeIf($type, 'type', $type) |
216
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
217
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
218
|
|
|
->attributeIf($hasValue, 'value', $this->old($name, $value)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $legend |
223
|
|
|
* |
224
|
|
|
* @return \Spatie\Html\Elements\Fieldset |
225
|
|
|
*/ |
226
|
|
|
public function fieldset($legend = null) |
227
|
|
|
{ |
228
|
|
|
return $legend ? |
229
|
|
|
Fieldset::create()->legend($legend) : Fieldset::create(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $method |
234
|
|
|
* @param string|null $action |
235
|
|
|
* |
236
|
|
|
* @return \Spatie\Html\Elements\Form |
237
|
|
|
*/ |
238
|
|
|
public function form($method = 'POST', $action = null) |
239
|
|
|
{ |
240
|
|
|
$method = strtoupper($method); |
241
|
|
|
$form = Form::create(); |
242
|
|
|
|
243
|
|
|
// If Laravel needs to spoof the form's method, we'll append a hidden |
244
|
|
|
// field containing the actual method |
245
|
|
|
if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) { |
246
|
|
|
$form = $form->addChild($this->hidden('_method')->value($method)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// On any other method that get, the form needs a CSRF token |
250
|
|
|
if ($method !== 'GET') { |
251
|
|
|
$form = $form->addChild($this->token()); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $form |
|
|
|
|
255
|
|
|
->method($method === 'GET' ? 'GET' : 'POST') |
256
|
|
|
->attributeIf($action, 'action', $action); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param string|null $name |
261
|
|
|
* @param string|null $value |
262
|
|
|
* |
263
|
|
|
* @return \Spatie\Html\Elements\Input |
264
|
|
|
*/ |
265
|
|
|
public function hidden($name = null, $value = null) |
266
|
|
|
{ |
267
|
|
|
return $this->input('hidden', $name, $value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string|null $src |
272
|
|
|
* @param string|null $alt |
273
|
|
|
* |
274
|
|
|
* @return \Spatie\Html\Elements\Img |
275
|
|
|
*/ |
276
|
|
|
public function img($src = null, $alt = null) |
277
|
|
|
{ |
278
|
|
|
return Img::create() |
|
|
|
|
279
|
|
|
->attributeIf($src, 'src', $src) |
280
|
|
|
->attributeIf($alt, 'alt', $alt); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param \Spatie\Html\HtmlElement|iterable|string|null $contents |
285
|
|
|
* @param string|null $for |
286
|
|
|
* |
287
|
|
|
* @return \Spatie\Html\Elements\Label |
288
|
|
|
*/ |
289
|
|
|
public function label($contents = null, $for = null) |
290
|
|
|
{ |
291
|
|
|
return Label::create() |
|
|
|
|
292
|
|
|
->attributeIf($for, 'for', $this->fieldName($for)) |
293
|
|
|
->children($contents); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
298
|
|
|
* |
299
|
|
|
* @return \Spatie\Html\Elements\Legend |
300
|
|
|
*/ |
301
|
|
|
public function legend($contents = null) |
302
|
|
|
{ |
303
|
|
|
return Legend::create()->html($contents); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param string $email |
308
|
|
|
* @param string|null $text |
309
|
|
|
* |
310
|
|
|
* @return \Spatie\Html\Elements\A |
311
|
|
|
*/ |
312
|
|
|
public function mailto($email, $text = null) |
313
|
|
|
{ |
314
|
|
|
return $this->a('mailto:' . $email, $text ?: $email); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param string|null $name |
319
|
|
|
* @param iterable $options |
320
|
|
|
* @param string|iterable|null $value |
321
|
|
|
* |
322
|
|
|
* @return \Spatie\Html\Elements\Select |
323
|
|
|
*/ |
324
|
|
View Code Duplication |
public function multiselect($name = null, $options = [], $value = null) |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
return Select::create() |
|
|
|
|
327
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
328
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
329
|
|
|
->options($options) |
330
|
|
|
->value($name ? $this->old($name, $value) : $value) |
331
|
|
|
->multiple(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param string|null $name |
336
|
|
|
* @param string|null $value |
337
|
|
|
* @param string|null $min |
338
|
|
|
* @param string|null $max |
339
|
|
|
* @param string|null $step |
340
|
|
|
* |
341
|
|
|
* @return \Spatie\Html\Elements\Input |
342
|
|
|
*/ |
343
|
|
|
public function number($name = null, $value = null, $min = null, $max = null, $step = null) |
344
|
|
|
{ |
345
|
|
|
return $this->input('number', $name, $value) |
|
|
|
|
346
|
|
|
->attributeIfNotNull($min, 'min', $min) |
347
|
|
|
->attributeIfNotNull($max, 'max', $max) |
348
|
|
|
->attributeIfNotNull($step, 'step', $step); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string|null $text |
353
|
|
|
* @param string|null $value |
354
|
|
|
* @param bool $selected |
355
|
|
|
* |
356
|
|
|
* @return \Spatie\Html\Elements\Option |
357
|
|
|
*/ |
358
|
|
|
public function option($text = null, $value = null, $selected = false) |
359
|
|
|
{ |
360
|
|
|
return Option::create() |
361
|
|
|
->text($text) |
362
|
|
|
->value($value) |
363
|
|
|
->selectedIf($selected); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string|null $value |
|
|
|
|
368
|
|
|
* |
369
|
|
|
* @return \Spatie\Html\Elements\Input |
370
|
|
|
*/ |
371
|
|
|
public function password($name = null) |
372
|
|
|
{ |
373
|
|
|
return $this->input('password', $name); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param string|null $name |
378
|
|
|
* @param bool $checked |
379
|
|
|
* @param string|null $value |
380
|
|
|
* |
381
|
|
|
* @return \Spatie\Html\Elements\Input |
382
|
|
|
*/ |
383
|
|
|
public function radio($name = null, $checked = null, $value = null) |
384
|
|
|
{ |
385
|
|
|
return $this->input('radio', $name, $value) |
|
|
|
|
386
|
|
|
->attributeIf($name, 'id', $value === null ? $name : ($name . '_' . Str::slug($value))) |
387
|
|
|
->attributeIf(!is_null($value), 'value', $value) |
388
|
|
|
->attributeIf((!is_null($value) && $this->old($name) == $value) || $checked, 'checked'); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param string|null $name |
393
|
|
|
* @param iterable $options |
394
|
|
|
* @param string|iterable|null $value |
395
|
|
|
* |
396
|
|
|
* @return \Spatie\Html\Elements\Select |
397
|
|
|
*/ |
398
|
|
View Code Duplication |
public function select($name = null, $options = [], $value = null) |
|
|
|
|
399
|
|
|
{ |
400
|
|
|
return Select::create() |
|
|
|
|
401
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
402
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
403
|
|
|
->options($options) |
404
|
|
|
->value($name ? $this->old($name, $value) : $value); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
409
|
|
|
* |
410
|
|
|
* @return \Spatie\Html\Elements\Span |
411
|
|
|
*/ |
412
|
|
|
public function span($contents = null) |
413
|
|
|
{ |
414
|
|
|
return Span::create()->children($contents); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param string|null $text |
419
|
|
|
* |
420
|
|
|
* @return \Spatie\Html\Elements\Button |
421
|
|
|
*/ |
422
|
|
|
public function submit($text = null) |
423
|
|
|
{ |
424
|
|
|
return $this->button($text, 'submit'); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @param string|null $text |
429
|
|
|
* |
430
|
|
|
* @return \Spatie\Html\Elements\Button |
431
|
|
|
*/ |
432
|
|
|
public function reset($text = null) |
433
|
|
|
{ |
434
|
|
|
return $this->button($text, 'reset'); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param string $number |
439
|
|
|
* @param string|null $text |
440
|
|
|
* |
441
|
|
|
* @return \Spatie\Html\Elements\A |
442
|
|
|
*/ |
443
|
|
|
public function tel($number, $text = null) |
444
|
|
|
{ |
445
|
|
|
return $this->a('tel:' . $number, $text ?: $number); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @param string|null $name |
450
|
|
|
* @param string|null $value |
451
|
|
|
* |
452
|
|
|
* @return \Spatie\Html\Elements\Input |
453
|
|
|
*/ |
454
|
|
|
public function text($name = null, $value = null) |
455
|
|
|
{ |
456
|
|
|
return $this->input('text', $name, $value); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @param string|null $name |
461
|
|
|
* |
462
|
|
|
* @return \Spatie\Html\Elements\File |
463
|
|
|
*/ |
464
|
|
|
public function file($name = null) |
465
|
|
|
{ |
466
|
|
|
return File::create() |
|
|
|
|
467
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
468
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @param string|null $name |
473
|
|
|
* @param string|null $value |
474
|
|
|
* |
475
|
|
|
* @return \Spatie\Html\Elements\Textarea |
476
|
|
|
*/ |
477
|
|
|
public function textarea($name = null, $value = null) |
478
|
|
|
{ |
479
|
|
|
return Textarea::create() |
|
|
|
|
480
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
481
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
482
|
|
|
->value($this->old($name, $value)); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* @return \Spatie\Html\Elements\Input |
487
|
|
|
*/ |
488
|
|
|
public function token() |
489
|
|
|
{ |
490
|
|
|
return $this |
491
|
|
|
->hidden() |
492
|
|
|
->name('_token') |
493
|
|
|
->value($this->request->session()->token()); |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @param \ArrayAccess|array $model |
498
|
|
|
* |
499
|
|
|
* @return $this |
500
|
|
|
*/ |
501
|
|
|
public function model($model) |
502
|
|
|
{ |
503
|
|
|
$this->model = $model; |
504
|
|
|
|
505
|
|
|
return $this; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* @param \ArrayAccess|array $model |
510
|
|
|
* @param string|null $method |
511
|
|
|
* @param string|null $action |
512
|
|
|
* |
513
|
|
|
* @return \Spatie\Html\Elements\Form |
514
|
|
|
*/ |
515
|
|
|
public function modelForm($model, $method = 'POST', $action = null): Form |
516
|
|
|
{ |
517
|
|
|
$this->model($model); |
518
|
|
|
|
519
|
|
|
return $this->form($method, $action); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @return $this |
524
|
|
|
*/ |
525
|
|
|
public function endModel() |
526
|
|
|
{ |
527
|
|
|
$this->model = null; |
528
|
|
|
|
529
|
|
|
return $this; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
534
|
|
|
*/ |
535
|
|
|
public function closeModelForm(): Htmlable |
536
|
|
|
{ |
537
|
|
|
$this->endModel(); |
538
|
|
|
|
539
|
|
|
return $this->form()->close(); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* @param string $name |
544
|
|
|
* @param mixed $value |
545
|
|
|
* |
546
|
|
|
* @return mixed |
547
|
|
|
*/ |
548
|
|
|
protected function old($name, $value = null) |
549
|
|
|
{ |
550
|
|
|
if (empty($name)) { |
551
|
|
|
return; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
// Convert array format (sth[1]) to dot notation (sth.1) |
555
|
|
|
$name = preg_replace('/\[(.+)\]/U', '.$1', $name); |
556
|
|
|
|
557
|
|
|
// If there's no default value provided, the html builder currently |
558
|
|
|
// has a model assigned and there aren't old input items, |
559
|
|
|
// try to retrieve a value from the model. |
560
|
|
|
if (is_null($value) && $this->model && empty($this->request->old())) { |
561
|
|
|
$value = data_get($this->model, $name) ?? ''; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
return $this->request->old($name, $value); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Retrieve the value from the current session or assigned model. This is |
569
|
|
|
* a public alias for `old`. |
570
|
|
|
* |
571
|
|
|
* @param string $name |
572
|
|
|
* @param mixed $value |
|
|
|
|
573
|
|
|
* |
574
|
|
|
* @return mixed |
575
|
|
|
*/ |
576
|
|
|
public function value($name, $default = null) |
577
|
|
|
{ |
578
|
|
|
return $this->old($name, $default); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* @param string $name |
583
|
|
|
* |
584
|
|
|
* @return string |
585
|
|
|
*/ |
586
|
|
|
protected function fieldName($name) |
587
|
|
|
{ |
588
|
|
|
return $name; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
protected function ensureModelIsAvailable() |
592
|
|
|
{ |
593
|
|
|
if (empty($this->model)) { |
594
|
|
|
throw new Exception('Method requires a model to be set on the html builder'); |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* @param string $value |
600
|
|
|
* @param string $format DateTime formatting string supported by date_format() |
601
|
|
|
* @return string |
602
|
|
|
*/ |
603
|
|
|
protected function formatDateTime($value, $format) |
604
|
|
|
{ |
605
|
|
|
if (empty($value)) { |
606
|
|
|
return $value; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
try { |
610
|
|
|
$date = new DateTimeImmutable($value); |
611
|
|
|
|
612
|
|
|
return $date->format($format); |
613
|
|
|
} catch (\Exception $e) { |
614
|
|
|
return $value; |
615
|
|
|
} |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
|