Completed
Push — master ( d72b67...4a63ea )
by ARCANEDEV
07:14
created

FormBuilder::setQuickTextAreaSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelHtml;
2
3
use Arcanedev\Html\Elements\{
4
    Button, File, Form, Input, Label, Select, Textarea
5
};
6
use Arcanedev\LaravelHtml\Bases\Builder;
7
use Arcanedev\LaravelHtml\Contracts\FormBuilder as FormBuilderContract;
8
use DateTime;
9
use Illuminate\Contracts\Routing\UrlGenerator;
10
use Illuminate\Contracts\Session\Session;
11
use Illuminate\Support\{
12
    Arr, Collection, Str
13
};
14
15
/**
16
 * Class     FormBuilder
17
 *
18
 * @package  Arcanedev\LaravelHtml
19
 * @author   ARCANEDEV <[email protected]>
20
 */
21
class FormBuilder extends Builder implements FormBuilderContract
22
{
23
    /* -----------------------------------------------------------------
24
     |  Properties
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
    * The HTML builder instance.
30
    *
31
    * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilder
32
    */
33
    protected $html;
34
35
    /**
36
    * The URL generator instance.
37
    *
38
    * @var \Illuminate\Contracts\Routing\UrlGenerator
39
    */
40
    protected $url;
41
42
    /**
43
    * The CSRF token used by the form builder.
44
    *
45
    * @var string
46
    */
47
    protected $csrfToken;
48
49
    /**
50
    * The session store implementation.
51
    *
52
    * @var \Illuminate\Contracts\Session\Session|\Illuminate\Session\Store
53
    */
54
    protected $session;
55
56
    /**
57
    * The current model instance for the form.
58
    *
59
    * @var \Illuminate\Database\Eloquent\Model|null
60
    */
61
    protected $model;
62
63
    /**
64
    * An array of label names we've created.
65
    *
66
    * @var array
67
    */
68
    protected $labels = [];
69
70
    /**
71
    * The reserved form open attributes.
72
    *
73
    * @var array
74
    */
75
    protected $reserved = ['method', 'url', 'route', 'action', 'files'];
76
77
    /**
78
    * The form methods that should be spoofed, in uppercase.
79
    *
80
    * @var array
81
    */
82
    protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT'];
83
84
    /**
85
    * The types of inputs to not fill values on by default.
86
    *
87
    * @var array
88
    */
89
    protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
90
91
    /* -----------------------------------------------------------------
92
     |  Constructor
93
     | -----------------------------------------------------------------
94
     */
95
96
    /**
97
    * Create a new form builder instance.
98
    *
99
    * @param  \Arcanedev\LaravelHtml\Contracts\HtmlBuilder  $html
100
    * @param  \Illuminate\Contracts\Routing\UrlGenerator    $url
101
    * @param  \Illuminate\Contracts\Session\Session         $session
102
    */
103 321
    public function __construct(Contracts\HtmlBuilder $html, UrlGenerator $url, Session $session)
104
    {
105 321
        $this->url       = $url;
106 321
        $this->html      = $html;
107 321
        $this->csrfToken = $session->token();
108
109 321
        $this->setSessionStore($session);
110 321
    }
111
112
    /* -----------------------------------------------------------------
113
     |  Getters & Setters
114
     | -----------------------------------------------------------------
115
     */
116
117
    /**
118
     * Get the session store implementation.
119
     *
120
     * @return  \Illuminate\Contracts\Session\Session
121
     */
122 3
    public function getSessionStore()
123
    {
124 3
        return $this->session;
125
    }
126
127
    /**
128
     * Set the session store implementation.
129
     *
130
     * @param  \Illuminate\Contracts\Session\Session  $session
131
     *
132
     * @return self
133
     */
134 321
    public function setSessionStore(Session $session)
135
    {
136 321
        $this->session = $session;
137
138 321
        return $this;
139
    }
140
141
    /**
142
     * Set the model instance on the form builder.
143
     *
144
     * @param  \Illuminate\Database\Eloquent\Model|null  $model
145
     *
146
     * @return self
147
     */
148 36
    public function setModel($model)
149
    {
150 36
        $this->model = $model;
151
152 36
        return $this;
153
    }
154
155
    /**
156
     * Get the model instance on the form builder.
157
     *
158
     * @return \Illuminate\Database\Eloquent\Model
159
     */
160 138
    public function getModel()
161
    {
162 138
        return $this->model;
163
    }
164
165
    /**
166
     * Get the ID attribute for a field name.
167
     *
168
     * @param  string  $name
169
     * @param  array   $attributes
170
     *
171
     * @return string
172
     */
173 249
    public function getIdAttribute($name, array $attributes)
174
    {
175 249
        if (array_key_exists('id', $attributes))
176 21
            return $attributes['id'];
177
178 237
        if (in_array($name, $this->labels))
179 6
            return $name;
180
181 231
        return null;
182
    }
183
184
    /**
185
     * Get the value that should be assigned to the field.
186
     *
187
     * @param  string  $name
188
     * @param  mixed   $value
189
     *
190
     * @return mixed
191
     */
192 234
    public function getValueAttribute($name, $value = null)
193
    {
194 234
        if (is_null($name))
195 6
            return $value;
196
197 228
        if ( ! is_null($this->old($name)) && $name !== '_method')
198 15
            return $this->old($name);
199
200 222
        if ( ! is_null($value))
201 129
            return $value;
202
203 123
        return $this->getModelValueAttribute($name);
204
    }
205
206
    /**
207
     * Get the model value that should be assigned to the field.
208
     *
209
     * @param  string                               $name
210
     * @param  \Illuminate\Database\Eloquent\Model  $model
211
     *
212
     * @return mixed
213
     */
214 132
    private function getModelValueAttribute($name, $model = null)
215
    {
216 132
        $model = $model ?: $this->getModel();
217
218 132
        $key = self::transformKey($name);
219
220 132
        if (strpos($key, '.') !== false) {
221 15
            $keys = explode('.', $key, 2);
222
223 15
            return $this->getModelValueAttribute(
224 15
                $keys[1],
225 15
                $this->getModelValueAttribute($keys[0], $model)
226
            );
227
        }
228
229 132
        return method_exists($model, 'getFormValue')
230 3
            ? $model->getFormValue($key)
231 132
            : data_get($model, $key);
232
    }
233
234
    /**
235
     * Get a value from the session's old input.
236
     *
237
     * @param  string  $name
238
     *
239
     * @return mixed
240
     */
241 237
    public function old($name)
242
    {
243 237
        return ! is_null($this->session)
244 237
            ? $this->session->getOldInput(self::transformKey($name))
245 237
            : null;
246
    }
247
248
    /**
249
     * Transform key from array to dot syntax.
250
     *
251
     * @param  string  $key
252
     *
253
     * @return string
254
     */
255 237
    private static function transformKey($key)
256
    {
257 237
        return str_replace(
258 237
            ['.', '[]', '[', ']'],
259 237
            ['_', '', '.', ''],
260 158
            $key
261
        );
262
    }
263
264
    /**
265
     * Determine if the old input is empty.
266
     *
267
     * @return bool
268
     */
269 12
    public function oldInputIsEmpty()
270
    {
271 12
        return ! is_null($this->session)
272 12
            && (count($this->session->getOldInput()) === 0);
273
    }
274
275
    /* -----------------------------------------------------------------
276
     |  Main Methods
277
     | -----------------------------------------------------------------
278
     */
279
280
    /**
281
     * Open up a new HTML form.
282
     *
283
     * @param  array  $attributes
284
     *
285
     * @return \Illuminate\Support\HtmlString
286
     */
287 57
    public function open(array $attributes = [])
288
    {
289 57
        $method = strtoupper(Arr::pull($attributes, 'method', 'POST'));
290
291 57
        return Form::make()
292 57
            ->method($method !== 'GET' ? 'POST' : $method)
293 57
            ->action($this->getAction($attributes))
294 57
            ->attributes(array_merge(
295 57
                ['accept-charset' => 'UTF-8'],
296 57
                Arr::except($attributes, $this->reserved)
297
            ))
298
            ->if(Arr::pull($attributes, 'files', false), function (Form $form) {
299 6
                return $form->acceptsFiles();
300 57
            })
301
            ->if(in_array($method, $this->spoofedMethods), function (Form $form) use ($method) {
302 6
                return $form->addChild($this->hidden('_method', $method));
303 57
            })
304
            ->if($method !== 'GET', function (Form $form) {
305 30
                return $form->addChild($this->token());
306 57
            })
307 57
            ->open();
308
    }
309
310
    /**
311
     * Create a new model based form builder.
312
     *
313
     * @param  mixed  $model
314
     * @param  array  $attributes
315
     *
316
     * @return \Illuminate\Support\HtmlString
317
     */
318 15
    public function model($model, array $attributes = [])
319
    {
320 15
        return $this->setModel($model)->open($attributes);
321
    }
322
323
    /**
324
     * Close the current form.
325
     *
326
     * @return \Illuminate\Support\HtmlString
327
     */
328 6
    public function close()
329
    {
330 6
        $this->labels = [];
331 6
        $this->setModel(null);
332
333 6
        return Form::make()->close();
334
    }
335
336
    /**
337
     * Generate a hidden field with the current CSRF token.
338
     *
339
     * @return \Illuminate\Support\HtmlString
340
     */
341 30
    public function token()
342
    {
343 30
        return $this->hidden(
344 30
            '_token',
345 30
            empty($this->csrfToken) ? $this->session->token() : $this->csrfToken
346
        );
347
    }
348
349
    /**
350
     * Create a form label element.
351
     *
352
     * @param  string        $name
353
     * @param  string|mixed  $value
354
     * @param  array         $attributes
355
     * @param  bool          $escaped
356
     *
357
     * @return \Illuminate\Support\HtmlString
358
     */
359 15
    public function label($name, $value = null, array $attributes = [], $escaped = true)
360
    {
361 15
        $this->labels[] = $name;
362
363 15
        $value = $value ?: Str::title(str_replace(['_', '-'], ' ', $name));
364
365 15
        return Label::make()
366 15
            ->for($name)
367 15
            ->attributes($attributes)
368 15
            ->html($escaped ? e($value) : $value)
369 15
            ->render();
370
    }
371
372
    /**
373
     * Create a form input field.
374
     *
375
     * @param  string        $type
376
     * @param  string        $name
377
     * @param  string|mixed  $value
378
     * @param  array         $attributes
379
     *
380
     * @return \Illuminate\Support\HtmlString
381
     */
382 186
    public function input($type, $name, $value = null, array $attributes = [])
383
    {
384 186
        if ( ! in_array($type, $this->skipValueTypes))
385 159
            $value = $this->getValueAttribute($name, $value);
386
387 186
        return Input::make()
388 186
            ->type($type)
389 186
            ->attributeUnless(is_null($name), 'name', $name)
390 186
            ->attributeUnless(is_null($id = $this->getIdAttribute($name, $attributes)), 'id', $id)
391 186
            ->attributeUnless(is_null($value) || empty($value), 'value', $value)
392 186
            ->attributes($attributes)
393 186
            ->render();
394
    }
395
396
    /**
397
     * Create a text input field.
398
     *
399
     * @param  string        $name
400
     * @param  string|mixed  $value
401
     * @param  array         $attributes
402
     *
403
     * @return \Illuminate\Support\HtmlString
404
     */
405 21
    public function text($name, $value = null, array $attributes = [])
406
    {
407 21
        return $this->input('text', $name, $value, $attributes);
408
    }
409
410
    /**
411
     * Create a password input field.
412
     *
413
     * @param  string  $name
414
     * @param  array   $attributes
415
     *
416
     * @return \Illuminate\Support\HtmlString
417
     */
418 9
    public function password($name, array $attributes = [])
419
    {
420 9
        return $this->input('password', $name, null, $attributes);
421
    }
422
423
    /**
424
     * Create a hidden input field.
425
     *
426
     * @param  string        $name
427
     * @param  string|mixed  $value
428
     * @param  array         $attributes
429
     *
430
     * @return \Illuminate\Support\HtmlString
431
     */
432 39
    public function hidden($name, $value = null, array $attributes = [])
433
    {
434 39
        return $this->input('hidden', $name, $value, $attributes);
435
    }
436
437
    /**
438
     * Create an e-mail input field.
439
     *
440
     * @param  string        $name
441
     * @param  string|mixed  $value
442
     * @param  array         $attributes
443
     *
444
     * @return \Illuminate\Support\HtmlString
445
     */
446 9
    public function email($name, $value = null, array $attributes = [])
447
    {
448 9
        return $this->input('email', $name, $value, $attributes);
449
    }
450
451
    /**
452
     * Create a tel input field.
453
     *
454
     * @param  string        $name
455
     * @param  string|mixed  $value
456
     * @param  array         $attributes
457
     *
458
     * @return \Illuminate\Support\HtmlString
459
     */
460 9
    public function tel($name, $value = null, array $attributes = [])
461
    {
462 9
        return $this->input('tel', $name, $value, $attributes);
463
    }
464
465
    /**
466
     * Create a number input field.
467
     *
468
     * @param  string        $name
469
     * @param  string|mixed  $value
470
     * @param  array         $attributes
471
     *
472
     * @return \Illuminate\Support\HtmlString
473
     */
474 9
    public function number($name, $value = null, array $attributes = [])
475
    {
476 9
        return $this->input('number', $name, $value, $attributes);
477
    }
478
479
    /**
480
     * Create a date input field.
481
     *
482
     * @param  string  $name
483
     * @param  string  $value
484
     * @param  array   $attributes
485
     *
486
     * @return \Illuminate\Support\HtmlString
487
     */
488 12
    public function date($name, $value = null, array $attributes = [])
489
    {
490 12
        if ($value instanceof DateTime)
491 3
            $value = $value->format('Y-m-d');
492
493 12
        return $this->input('date', $name, $value, $attributes);
494
    }
495
496
    /**
497
     * Create a datetime input field.
498
     *
499
     * @param  string        $name
500
     * @param  string|mixed  $value
501
     * @param  array         $attributes
502
     *
503
     * @return \Illuminate\Support\HtmlString
504
     */
505 12
    public function datetime($name, $value = null, array $attributes = [])
506
    {
507 12
        if ($value instanceof DateTime)
508 6
            $value = $value->format(DateTime::RFC3339);
509
510 12
        return $this->input('datetime', $name, $value, $attributes);
511
    }
512
513
    /**
514
     * Create a datetime-local input field.
515
     *
516
     * @param  string        $name
517
     * @param  string|mixed  $value
518
     * @param  array         $attributes
519
     *
520
     * @return \Illuminate\Support\HtmlString
521
     */
522 12
    public function datetimeLocal($name, $value = null, array $attributes = [])
523
    {
524 12
        if ($value instanceof DateTime)
525 6
            $value = $value->format('Y-m-d\TH:i');
526
527 12
        return $this->input('datetime-local', $name, $value, $attributes);
528
    }
529
530
    /**
531
     * Create a time input field.
532
     *
533
     * @param  string        $name
534
     * @param  string|mixed  $value
535
     * @param  array         $attributes
536
     *
537
     * @return \Illuminate\Support\HtmlString
538
     */
539 9
    public function time($name, $value = null, array $attributes = [])
540
    {
541 9
        return $this->input('time', $name, $value, $attributes);
542
    }
543
544
    /**
545
     * Create a url input field.
546
     *
547
     * @param  string  $name
548
     * @param  string  $value
549
     * @param  array   $attributes
550
     *
551
     * @return \Illuminate\Support\HtmlString
552
     */
553 6
    public function url($name, $value = null, array $attributes = [])
554
    {
555 6
        return $this->input('url', $name, $value, $attributes);
556
    }
557
558
    /**
559
     * Create a file input field.
560
     *
561
     * @param  string  $name
562
     * @param  array   $attributes
563
     *
564
     * @return \Illuminate\Support\HtmlString
565
     */
566 9
    public function file($name, array $attributes = [])
567
    {
568 9
        return File::make()->name($name)->attributes($attributes)->render();
569
    }
570
571
    /**
572
     * Create a textarea input field.
573
     *
574
     * @param  string  $name
575
     * @param  string  $value
576
     * @param  array   $attributes
577
     *
578
     * @return \Illuminate\Support\HtmlString
579
     */
580 18
    public function textarea($name, $value = null, array $attributes = [])
581
    {
582 18
        $id    = $this->getIdAttribute($name, $attributes);
583 18
        $size  = Arr::pull($attributes, 'size');
584 18
        $value = (string) $this->getValueAttribute($name, $value);
585
586 18
        return Textarea::make()
587 18
            ->name($name)
588 18
            ->attributeUnless(is_null($id), 'id', $id)
589
            ->unless(is_null($size), function (Textarea $elt) use ($size) {
590 9
                return $elt->size($size);
591 18
            })
592 18
            ->attributes($attributes)
593 18
            ->html($this->html->escape($value))
594 18
            ->render();
595
    }
596
597
    /**
598
     * Create a select box field.
599
     *
600
     * @param  string                                $name
601
     * @param  array|\Illuminate\Support\Collection  $list
602
     * @param  string|bool                           $selected
603
     * @param  array                                 $attributes
604
     * @param  array                                 $optionsAttributes
605
     * @param  array                                 $optgroupsAttributes
606
     *
607
     * @return \Illuminate\Support\HtmlString
608
     */
609 45
    public function select(
610
        $name,
611
        $list = [],
612
        $selected = null,
613
        array $attributes = [],
614
        array $optionsAttributes = [],
615
        array $optgroupsAttributes = []
616
    ) {
617 45
        return Select::make()
618 45
            ->name($name)
619 45
            ->options($list, $optionsAttributes, $optgroupsAttributes)
620 45
            ->attributes($attributes)
621 45
            ->attributeUnless(is_null($id = $this->getIdAttribute($name, $attributes)), 'id', $id)
622 45
            ->value($this->getValueAttribute($name, $selected))
623 45
            ->render();
624
    }
625
626
    /**
627
     * Create a select range field.
628
     *
629
     * @param  string  $name
630
     * @param  string  $begin
631
     * @param  string  $end
632
     * @param  string  $selected
633
     * @param  array   $attributes
634
     *
635
     * @return \Illuminate\Support\HtmlString
636
     */
637 6
    public function selectRange($name, $begin, $end, $selected = null, array $attributes = [])
638
    {
639 6
        $range = array_combine($range = range($begin, $end), $range);
640
641 6
        return $this->select($name, $range, $selected, $attributes);
642
    }
643
644
    /**
645
     * Create a select year field.
646
     *
647
     * @param  string  $name
648
     * @param  string  $begin
649
     * @param  string  $end
650
     * @param  string  $selected
651
     * @param  array   $attributes
652
     *
653
     * @return \Illuminate\Support\HtmlString
654
     */
655 3
    public function selectYear($name, $begin, $end, $selected = null, array $attributes = [])
656
    {
657 3
        return $this->selectRange($name, $begin, $end, $selected, $attributes);
658
    }
659
660
    /**
661
     * Create a select month field.
662
     *
663
     * @param  string  $name
664
     * @param  string  $selected
665
     * @param  array   $attributes
666
     * @param  string  $format
667
     *
668
     * @return \Illuminate\Support\HtmlString
669
     */
670 3
    public function selectMonth($name, $selected = null, array $attributes = [], $format = '%B')
671
    {
672 3
        $months = [];
673
674 3
        foreach(range(1, 12) as $month) {
675 3
            $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
676
        }
677
678 3
        return $this->select($name, $months, $selected, $attributes);
679
    }
680
681
    /**
682
     * Create a checkbox input field.
683
     *
684
     * @param  string     $name
685
     * @param  mixed      $value
686
     * @param  bool|null  $checked
687
     * @param  array      $attributes
688
     *
689
     * @return \Illuminate\Support\HtmlString
690
     */
691 12
    public function checkbox($name, $value = 1, $checked = null, array $attributes = [])
692
    {
693 12
        return $this->checkable('checkbox', $name, $value, $checked, $attributes);
694
    }
695
696
    /**
697
     * Create a radio button input field.
698
     *
699
     * @param  string  $name
700
     * @param  mixed   $value
701
     * @param  bool    $checked
702
     * @param  array   $attributes
703
     *
704
     * @return \Illuminate\Support\HtmlString
705
     */
706 6
    public function radio($name, $value = null, $checked = null, array $attributes = [])
707
    {
708 6
        return $this->checkable('radio', $name, $value ?: $name, $checked, $attributes);
709
    }
710
711
    /**
712
     * Create a HTML reset input element.
713
     *
714
     * @param  string|mixed  $value
715
     * @param  array         $attributes
716
     *
717
     * @return \Illuminate\Support\HtmlString
718
     */
719 3
    public function reset($value, array $attributes = [])
720
    {
721 3
        return $this->input('reset', null, $value, $attributes);
722
    }
723
724
    /**
725
    * Create a HTML image input element.
726
    *
727
    * @param  string       $url
728
    * @param  string|null  $name
729
    * @param  array        $attributes
730
    *
731
     * @return \Illuminate\Support\HtmlString
732
    */
733 3
    public function image($url, $name = null, array $attributes = [])
734
    {
735 3
        return $this->input('image', $name, null, array_merge($attributes, [
736 3
            'src' => $this->url->asset($url),
737
        ]));
738
    }
739
740
    /**
741
     * Create a submit button element.
742
     *
743
     * @param  string|mixed  $value
744
     * @param  array         $attributes
745
     *
746
     * @return \Illuminate\Support\HtmlString
747
     */
748 3
    public function submit($value = null, array $attributes = [])
749
    {
750 3
        return Button::make()->type('submit')->html($value)->attributes($attributes)->render();
751
    }
752
753
    /**
754
     * Create a button element.
755
     *
756
     * @param  string|mixed  $value
757
     * @param  array         $attributes
758
     *
759
     * @return \Illuminate\Support\HtmlString
760
     */
761 3
    public function button($value = null, array $attributes = [])
762
    {
763 3
        return Button::make()
764 3
            ->type(Arr::pull($attributes, 'type', 'button'))
765 3
            ->attributes($attributes)
766 3
            ->html($value)
767 3
            ->render();
768
    }
769
770
    /**
771
     * Create a color input field.
772
     *
773
     * @param  string        $name
774
     * @param  string|mixed  $value
775
     * @param  array         $attributes
776
     *
777
     * @return \Illuminate\Support\HtmlString
778
     */
779 9
    public function color($name, $value = null, array $attributes = [])
780
    {
781 9
        return $this->input('color', $name, $value, $attributes);
782
    }
783
784
    /* -----------------------------------------------------------------
785
     |  Other Methods
786
     | -----------------------------------------------------------------
787
     */
788
789
    /**
790
     * Create a checkable input field.
791
     *
792
     * @param  string     $type
793
     * @param  string     $name
794
     * @param  mixed      $value
795
     * @param  bool|null  $checked
796
     * @param  array      $attributes
797
     *
798
     * @return \Illuminate\Support\HtmlString
799
     */
800 21
    protected function checkable($type, $name, $value, $checked, array $attributes)
801
    {
802 21
        $checked = $this->getCheckedState($type, $name, $value, $checked);
803
804 21
        if ( ! is_null($checked) && $checked)
805 18
            $attributes['checked'] = 'checked';
806
807 21
        return $this->input($type, $name, $value, $attributes);
808
    }
809
810
    /**
811
     * Get the check state for a checkable input.
812
     *
813
     * @param  string     $type
814
     * @param  string     $name
815
     * @param  mixed      $value
816
     * @param  bool|null  $checked
817
     *
818
     * @return bool
819
     */
820 21
    private function getCheckedState($type, $name, $value, $checked)
821
    {
822 7
        switch($type) {
823 21
            case 'checkbox':
824 12
                return $this->getCheckboxCheckedState($name, $value, $checked);
825
826 9
            case 'radio':
827 6
                return $this->getRadioCheckedState($name, $value, $checked);
828
829
            default:
830 3
                return $this->getValueAttribute($name) === $value;
831
        }
832
    }
833
834
    /**
835
     * Get the check state for a checkbox input.
836
     *
837
     * @param  string     $name
838
     * @param  mixed      $value
839
     * @param  bool|null  $checked
840
     *
841
     * @return bool
842
     */
843 12
    private function getCheckboxCheckedState($name, $value, $checked)
844
    {
845
        if (
846 12
            isset($this->session) &&
847 12
            ! $this->oldInputIsEmpty() &&
848 9
            is_null($this->old($name))
849
        )
850 3
            return false;
851
852 12
        if ($this->missingOldAndModel($name))
853 6
            return $checked;
854
855 6
        $posted = $this->getValueAttribute($name, $checked);
856
857 6
        if (is_array($posted))
858 3
            return in_array($value, $posted);
859
860 6
        if ($posted instanceof Collection)
861 3
            return $posted->contains('id', $value);
862
863 6
        return (bool) $posted;
864
    }
865
866
    /**
867
     * Get the check state for a radio input.
868
     *
869
     * @param  string     $name
870
     * @param  mixed      $value
871
     * @param  bool|null  $checked
872
     *
873
     * @return bool
874
     */
875 6
    private function getRadioCheckedState($name, $value, $checked)
876
    {
877 6
        return $this->missingOldAndModel($name)
878 2
            ? $checked
879 6
            : $this->getValueAttribute($name) === $value;
880
    }
881
882
    /**
883
     * Determine if old input or model input exists for a key.
884
     *
885
     * @param  string  $name
886
     *
887
     * @return bool
888
     */
889 18
    private function missingOldAndModel($name)
890
    {
891 18
        return is_null($this->old($name))
892 18
            && is_null($this->getModelValueAttribute($name));
893
    }
894
895
    /**
896
     * Get the form action from the options.
897
     *
898
     * @param  array  $attributes
899
     *
900
     * @return string
901
     */
902 57
    private function getAction(array $attributes)
903
    {
904 57
        if (isset($attributes['url']))
905 18
            return $this->getUrlAction($attributes['url']);
906
907 39
        if (isset($attributes['route']))
908 6
            return $this->getRouteAction($attributes['route']);
909
910 36
        if (isset($attributes['action']))
911 6
            return $this->getControllerAction($attributes['action']);
912
913 30
        return $this->url->current();
914
    }
915
916
    /**
917
     * Get the action for a "url" option.
918
     *
919
     * @param  array|string  $attribute
920
     *
921
     * @return string
922
     */
923 18
    private function getUrlAction($attribute)
924
    {
925 18
        return is_array($attribute)
926 3
            ? $this->url->to($attribute[0], array_slice($attribute, 1))
927 18
            : $this->url->to($attribute);
928
    }
929
930
    /**
931
     * Get the action for a "route" option.
932
     *
933
     * @param  array|string  $attribute
934
     *
935
     * @return string
936
     */
937 6
    private function getRouteAction($attribute)
938
    {
939 6
        return is_array($attribute)
940 3
            ? $this->url->route($attribute[0], array_slice($attribute, 1))
941 6
            : $this->url->route($attribute);
942
    }
943
944
    /**
945
     * Get the action for an "action" option.
946
     *
947
     * @param  array|string  $attribute
948
     *
949
     * @return string
950
     */
951 6
    private function getControllerAction($attribute)
952
    {
953 6
        return is_array($attribute)
954 3
            ? $this->url->action($attribute[0], array_slice($attribute, 1))
955 6
            : $this->url->action($attribute);
956
    }
957
}
958