1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Html; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use Spatie\Html\Elements\A; |
7
|
|
|
use Spatie\Html\Elements\I; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Spatie\Html\Elements\Div; |
10
|
|
|
use Spatie\Html\Elements\Img; |
11
|
|
|
use Spatie\Html\Elements\File; |
12
|
|
|
use Spatie\Html\Elements\Form; |
13
|
|
|
use Spatie\Html\Elements\Span; |
14
|
|
|
use Spatie\Html\Elements\Input; |
15
|
|
|
use Spatie\Html\Elements\Label; |
16
|
|
|
use Spatie\Html\Elements\Button; |
17
|
|
|
use Spatie\Html\Elements\Legend; |
18
|
|
|
use Spatie\Html\Elements\Option; |
19
|
|
|
use Spatie\Html\Elements\Select; |
20
|
|
|
use Spatie\Html\Elements\Element; |
21
|
|
|
use Illuminate\Support\Collection; |
22
|
|
|
use Illuminate\Support\HtmlString; |
23
|
|
|
use Spatie\Html\Elements\Fieldset; |
24
|
|
|
use Spatie\Html\Elements\Textarea; |
25
|
|
|
use Illuminate\Support\Traits\Macroable; |
26
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
27
|
|
|
|
28
|
|
|
class Html |
29
|
|
|
{ |
30
|
|
|
use Macroable; |
31
|
|
|
|
32
|
|
|
const HTML_DATE_FORMAT = 'Y-m-d'; |
33
|
|
|
const HTML_TIME_FORMAT = 'H:i:s'; |
34
|
|
|
|
35
|
|
|
/** @var \Illuminate\Http\Request */ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** @var \ArrayAccess|array */ |
39
|
|
|
protected $model; |
40
|
|
|
|
41
|
|
|
public function __construct(Request $request) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->request = $request; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string|null $href |
48
|
|
|
* @param string|null $text |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @return \Spatie\Html\Elements\A |
51
|
|
|
*/ |
52
|
|
|
public function a($href = null, $contents = null) |
53
|
|
|
{ |
54
|
|
|
return A::create() |
|
|
|
|
55
|
|
|
->attributeIf($href, 'href', $href) |
56
|
|
|
->html($contents); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string|null $href |
|
|
|
|
61
|
|
|
* @param string|null $text |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return \Spatie\Html\Elements\I |
64
|
|
|
*/ |
65
|
|
|
public function i($contents = null) |
66
|
|
|
{ |
67
|
|
|
return I::create() |
68
|
|
|
->html($contents); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|null $type |
73
|
|
|
* @param string|null $text |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return \Spatie\Html\Elements\Button |
76
|
|
|
*/ |
77
|
|
|
public function button($contents = null, $type = null, $name = null) |
78
|
|
|
{ |
79
|
|
|
return Button::create() |
|
|
|
|
80
|
|
|
->attributeIf($type, 'type', $type) |
81
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
82
|
|
|
->html($contents); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \Illuminate\Support\Collection|iterable|string $classes |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
89
|
|
|
*/ |
90
|
|
|
public function class($classes): Htmlable |
91
|
|
|
{ |
92
|
|
|
if ($classes instanceof Collection) { |
93
|
|
|
$classes = $classes->toArray(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$attributes = new Attributes(); |
97
|
|
|
$attributes->addClass($classes); |
98
|
|
|
|
99
|
|
|
return new HtmlString( |
100
|
|
|
$attributes->render() |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string|null $name |
106
|
|
|
* @param bool $checked |
107
|
|
|
* @param string|null $value |
108
|
|
|
* |
109
|
|
|
* @return \Spatie\Html\Elements\Input |
110
|
|
|
*/ |
111
|
|
|
public function checkbox($name = null, $checked = null, $value = '1') |
112
|
|
|
{ |
113
|
|
|
return $this->input('checkbox', $name, $value) |
|
|
|
|
114
|
|
|
->attributeIf(! is_null($value), 'value', $value) |
115
|
|
|
->attributeIf((bool) $this->old($name, $checked), 'checked'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
120
|
|
|
* |
121
|
|
|
* @return \Spatie\Html\Elements\Div |
122
|
|
|
*/ |
123
|
|
|
public function div($contents = null) |
124
|
|
|
{ |
125
|
|
|
return Div::create()->children($contents); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string|null $name |
130
|
|
|
* @param string|null $value |
131
|
|
|
* |
132
|
|
|
* @return \Spatie\Html\Elements\Input |
133
|
|
|
*/ |
134
|
|
|
public function email($name = null, $value = null) |
135
|
|
|
{ |
136
|
|
|
return $this->input('email', $name, $value); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string|null $name |
141
|
|
|
* @param string|null $value |
142
|
|
|
* @param bool $format |
143
|
|
|
* |
144
|
|
|
* @return \Spatie\Html\Elements\Input |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function date($name = '', $value = null, $format = true) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$element = $this->input('date', $name, $value); |
149
|
|
|
|
150
|
|
|
if (! $format || empty($element->getAttribute('value'))) { |
151
|
|
|
return $element; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string|null $name |
159
|
|
|
* @param string|null $value |
160
|
|
|
* @param string|null $min |
161
|
|
|
* @param string|null $max |
162
|
|
|
* @param string|null $step |
163
|
|
|
* |
164
|
|
|
* @return \Spatie\Html\Elements\Input |
165
|
|
|
*/ |
166
|
|
|
public function range($name = '', $value = '', $min = null, $max = null, $step = null) |
167
|
|
|
{ |
168
|
|
|
return $this->input('range', $name, $value) |
|
|
|
|
169
|
|
|
->attributeIfNotNull($min, 'min', $min) |
170
|
|
|
->attributeIfNotNull($max, 'max', $max) |
171
|
|
|
->attributeIfNotNull($step, 'step', $step); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string|null $name |
176
|
|
|
* @param string|null $value |
177
|
|
|
* @param bool $format |
178
|
|
|
* |
179
|
|
|
* @return \Spatie\Html\Elements\Input |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function time($name = '', $value = null, $format = true) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$element = $this->input('time', $name, $value); |
184
|
|
|
|
185
|
|
|
if (! $format || empty($element->getAttribute('value'))) { |
186
|
|
|
return $element; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $tag |
194
|
|
|
* |
195
|
|
|
* @return \Spatie\Html\Elements\Element |
196
|
|
|
*/ |
197
|
|
|
public function element($tag) |
198
|
|
|
{ |
199
|
|
|
return Element::withTag($tag); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string|null $type |
204
|
|
|
* @param string|null $name |
205
|
|
|
* @param string|null $value |
206
|
|
|
* |
207
|
|
|
* @return \Spatie\Html\Elements\Input |
208
|
|
|
*/ |
209
|
|
|
public function input($type = null, $name = null, $value = null) |
210
|
|
|
{ |
211
|
|
|
$hasValue = $name && ($type !== 'password' && ! is_null($this->old($name, $value)) || ! is_null($value)); |
|
|
|
|
212
|
|
|
|
213
|
|
|
return Input::create() |
|
|
|
|
214
|
|
|
->attributeIf($type, 'type', $type) |
215
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
216
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
217
|
|
|
->attributeIf($hasValue, 'value', $this->old($name, $value)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $legend |
222
|
|
|
* |
223
|
|
|
* @return \Spatie\Html\Elements\Fieldset |
224
|
|
|
*/ |
225
|
|
|
public function fieldset($legend = null) |
226
|
|
|
{ |
227
|
|
|
return $legend ? |
228
|
|
|
Fieldset::create()->legend($legend) : |
229
|
|
|
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 $text |
336
|
|
|
* @param string|null $value |
337
|
|
|
* @param bool $selected |
338
|
|
|
* |
339
|
|
|
* @return \Spatie\Html\Elements\Option |
340
|
|
|
*/ |
341
|
|
|
public function option($text = null, $value = null, $selected = false) |
342
|
|
|
{ |
343
|
|
|
return Option::create() |
344
|
|
|
->text($text) |
345
|
|
|
->value($value) |
346
|
|
|
->selectedIf($selected); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string|null $value |
|
|
|
|
351
|
|
|
* |
352
|
|
|
* @return \Spatie\Html\Elements\Input |
353
|
|
|
*/ |
354
|
|
|
public function password($name = null) |
355
|
|
|
{ |
356
|
|
|
return $this->input('password', $name); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param string|null $name |
361
|
|
|
* @param bool $checked |
362
|
|
|
* @param string|null $value |
363
|
|
|
* |
364
|
|
|
* @return \Spatie\Html\Elements\Input |
365
|
|
|
*/ |
366
|
|
|
public function radio($name = null, $checked = null, $value = null) |
367
|
|
|
{ |
368
|
|
|
return $this->input('radio', $name, $value) |
|
|
|
|
369
|
|
|
->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value))) |
|
|
|
|
370
|
|
|
->attributeIf(! is_null($value), 'value', $value) |
371
|
|
|
->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked'); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param string|null $name |
376
|
|
|
* @param iterable $options |
377
|
|
|
* @param string|iterable|null $value |
378
|
|
|
* |
379
|
|
|
* @return \Spatie\Html\Elements\Select |
380
|
|
|
*/ |
381
|
|
View Code Duplication |
public function select($name = null, $options = [], $value = null) |
|
|
|
|
382
|
|
|
{ |
383
|
|
|
return Select::create() |
|
|
|
|
384
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
385
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
386
|
|
|
->options($options) |
387
|
|
|
->value($name ? $this->old($name, $value) : $value); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param \Spatie\Html\HtmlElement|string|null $contents |
392
|
|
|
* |
393
|
|
|
* @return \Spatie\Html\Elements\Span |
394
|
|
|
*/ |
395
|
|
|
public function span($contents = null) |
396
|
|
|
{ |
397
|
|
|
return Span::create()->children($contents); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @param string|null $text |
402
|
|
|
* |
403
|
|
|
* @return \Spatie\Html\Elements\Button |
404
|
|
|
*/ |
405
|
|
|
public function submit($text = null) |
406
|
|
|
{ |
407
|
|
|
return $this->button($text, 'submit'); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param string|null $text |
412
|
|
|
* |
413
|
|
|
* @return \Spatie\Html\Elements\Button |
414
|
|
|
*/ |
415
|
|
|
public function reset($text = null) |
416
|
|
|
{ |
417
|
|
|
return $this->button($text, 'reset'); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @param string $number |
422
|
|
|
* @param string|null $text |
423
|
|
|
* |
424
|
|
|
* @return \Spatie\Html\Elements\A |
425
|
|
|
*/ |
426
|
|
|
public function tel($number, $text = null) |
427
|
|
|
{ |
428
|
|
|
return $this->a('tel:'.$number, $text ?: $number); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param string|null $name |
433
|
|
|
* @param string|null $value |
434
|
|
|
* |
435
|
|
|
* @return \Spatie\Html\Elements\Input |
436
|
|
|
*/ |
437
|
|
|
public function text($name = null, $value = null) |
438
|
|
|
{ |
439
|
|
|
return $this->input('text', $name, $value); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* @param string|null $name |
444
|
|
|
* |
445
|
|
|
* @return \Spatie\Html\Elements\File |
446
|
|
|
*/ |
447
|
|
|
public function file($name = null) |
448
|
|
|
{ |
449
|
|
|
return File::create() |
|
|
|
|
450
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
451
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @param string|null $name |
456
|
|
|
* @param string|null $value |
457
|
|
|
* |
458
|
|
|
* @return \Spatie\Html\Elements\Textarea |
459
|
|
|
*/ |
460
|
|
|
public function textarea($name = null, $value = null) |
461
|
|
|
{ |
462
|
|
|
return Textarea::create() |
|
|
|
|
463
|
|
|
->attributeIf($name, 'name', $this->fieldName($name)) |
464
|
|
|
->attributeIf($name, 'id', $this->fieldName($name)) |
465
|
|
|
->value($this->old($name, $value)); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return \Spatie\Html\Elements\Input |
470
|
|
|
*/ |
471
|
|
|
public function token() |
472
|
|
|
{ |
473
|
|
|
return $this |
474
|
|
|
->hidden() |
475
|
|
|
->name('_token') |
476
|
|
|
->value($this->request->session()->token()); |
|
|
|
|
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @param \ArrayAccess|array $model |
481
|
|
|
* |
482
|
|
|
* @return $this |
483
|
|
|
*/ |
484
|
|
|
public function model($model) |
485
|
|
|
{ |
486
|
|
|
$this->model = $model; |
487
|
|
|
|
488
|
|
|
return $this; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @param \ArrayAccess|array $model |
493
|
|
|
* @param string|null $method |
494
|
|
|
* @param string|null $action |
495
|
|
|
* |
496
|
|
|
* @return \Spatie\Html\Elements\Form |
497
|
|
|
*/ |
498
|
|
|
public function modelForm($model, $method = 'POST', $action = null): Form |
499
|
|
|
{ |
500
|
|
|
$this->model($model); |
501
|
|
|
|
502
|
|
|
return $this->form($method, $action); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* @return $this |
507
|
|
|
*/ |
508
|
|
|
public function endModel() |
509
|
|
|
{ |
510
|
|
|
$this->model = null; |
511
|
|
|
|
512
|
|
|
return $this; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* @return \Illuminate\Contracts\Support\Htmlable |
517
|
|
|
*/ |
518
|
|
|
public function closeModelForm(): Htmlable |
519
|
|
|
{ |
520
|
|
|
$this->endModel(); |
521
|
|
|
|
522
|
|
|
return $this->form()->close(); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* @param string $name |
527
|
|
|
* @param mixed $value |
528
|
|
|
* |
529
|
|
|
* @return mixed |
530
|
|
|
*/ |
531
|
|
|
protected function old($name, $value = null) |
532
|
|
|
{ |
533
|
|
|
if (empty($name)) { |
534
|
|
|
return; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
// Convert array format (sth[1]) to dot notation (sth.1) |
538
|
|
|
$name = preg_replace('/\[(.+)\]/U', '.$1', $name); |
539
|
|
|
|
540
|
|
|
// If there's no default value provided, the html builder currently |
541
|
|
|
// has a model assigned and there aren't old input items, |
542
|
|
|
// try to retrieve a value from the model. |
543
|
|
|
if (is_null($value) && $this->model && empty($this->request->old())) { |
544
|
|
|
$value = data_get($this->model, $name) ?? ''; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return $this->request->old($name, $value); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Retrieve the value from the current session or assigned model. This is |
552
|
|
|
* a public alias for `old`. |
553
|
|
|
* |
554
|
|
|
* @param string $name |
555
|
|
|
* @param mixed $value |
|
|
|
|
556
|
|
|
* |
557
|
|
|
* @return mixed |
558
|
|
|
*/ |
559
|
|
|
public function value($name, $default = null) |
560
|
|
|
{ |
561
|
|
|
return $this->old($name, $default); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @param string $name |
566
|
|
|
* |
567
|
|
|
* @return string |
568
|
|
|
*/ |
569
|
|
|
protected function fieldName($name) |
570
|
|
|
{ |
571
|
|
|
return $name; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
protected function ensureModelIsAvailable() |
575
|
|
|
{ |
576
|
|
|
if (empty($this->model)) { |
577
|
|
|
throw new Exception('Method requires a model to be set on the html builder'); |
578
|
|
|
} |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* @param string $value |
583
|
|
|
* @param string $format DateTime formatting string supported by date_format() |
584
|
|
|
* @return string |
585
|
|
|
*/ |
586
|
|
|
protected function formatDateTime($value, $format) |
587
|
|
|
{ |
588
|
|
|
if (empty($value)) { |
589
|
|
|
return $value; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
try { |
593
|
|
|
$date = new DateTimeImmutable($value); |
594
|
|
|
|
595
|
|
|
return $date->format($format); |
596
|
|
|
} catch (\Exception $e) { |
597
|
|
|
return $value; |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|