Completed
Pull Request — master (#5)
by ARCANEDEV
05:18
created

FormBuilder::getRadioCheckedState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4286
ccs 4
cts 4
cp 1
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php namespace Arcanedev\LaravelHtml;
2
3
use Arcanedev\LaravelHtml\Contracts\FormBuilderInterface;
4
use Arcanedev\LaravelHtml\Traits\Componentable;
5
use BadMethodCallException;
6
use DateTime;
7
use Illuminate\Contracts\Routing\UrlGenerator;
8
use Illuminate\Session\SessionInterface as Session;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Traits\Macroable;
11
12
/**
13
 * Class     FormBuilder
14
 *
15
 * @package  Arcanedev\LaravelHtml\Builders
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class FormBuilder implements FormBuilderInterface
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Traits
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    use Macroable, Componentable {
25
        Macroable::__call     as macroCall;
26
        Componentable::__call as componentCall;
27
    }
28
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Properties
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    /**
34
    * The HTML builder instance.
35
    *
36
    * @var \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface
37
    */
38
    protected $html;
39
40
    /**
41
    * The URL generator instance.
42
    *
43
    * @var \Illuminate\Contracts\Routing\UrlGenerator
44
    */
45
    protected $url;
46
47
    /**
48
    * The CSRF token used by the form builder.
49
    *
50
    * @var string
51
    */
52
    protected $csrfToken;
53
54
    /**
55
    * The session store implementation.
56
    *
57
    * @var \Illuminate\Session\SessionInterface
58
    */
59
    protected $session;
60
61
    /**
62
    * The current model instance for the form.
63
    *
64
    * @var mixed
65
    */
66
    protected $model;
67
68
    /**
69
    * An array of label names we've created.
70
    *
71
    * @var array
72
    */
73
    protected $labels = [];
74
75
    /**
76
    * The reserved form open attributes.
77
    *
78
    * @var array
79
    */
80
    protected $reserved = ['method', 'url', 'route', 'action', 'files'];
81
82
    /**
83
    * The form methods that should be spoofed, in uppercase.
84
    *
85
    * @var array
86
    */
87
    protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT'];
88
89
    /**
90
    * The types of inputs to not fill values on by default.
91
    *
92
    * @var array
93
    */
94
    protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
95
96
    /* ------------------------------------------------------------------------------------------------
97
     |  Constructor
98
     | ------------------------------------------------------------------------------------------------
99
     */
100
    /**
101
    * Create a new form builder instance.
102
    *
103
    * @param  \Illuminate\Contracts\Routing\UrlGenerator             $url
104
    * @param  \Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface  $html
105
    * @param  string                                                 $csrfToken
106
    */
107 760
    public function __construct(
108
        Contracts\HtmlBuilderInterface $html,
109
        UrlGenerator $url,
110
        $csrfToken
111
    ) {
112 760
        $this->url       = $url;
113 760
        $this->html      = $html;
114 760
        $this->csrfToken = $csrfToken;
115 760
    }
116
117
    /* ------------------------------------------------------------------------------------------------
118
     |  Getters & Setters
119
     | ------------------------------------------------------------------------------------------------
120
     */
121
    /**
122
     * Get the session store implementation.
123
     *
124
     * @return  \Illuminate\Session\SessionInterface
125
     */
126 8
    public function getSessionStore()
127
    {
128 8
        return $this->session;
129
    }
130
131
    /**
132
     * Set the session store implementation.
133
     *
134
     * @param  \Illuminate\Session\SessionInterface  $session
135
     *
136
     * @return self
137
     */
138 120
    public function setSessionStore(Session $session)
139
    {
140 120
        $this->session = $session;
141
142 120
        return $this;
143
    }
144
145
    /**
146
     * Set the model instance on the form builder.
147
     *
148
     * @param  mixed  $model
149
     *
150
     * @return self
151
     */
152 80
    public function setModel($model)
153
    {
154 80
        $this->model = $model;
155
156 80
        return $this;
157
    }
158
159
    /**
160
     * Get the ID attribute for a field name.
161
     *
162
     * @param  string  $name
163
     * @param  array   $attributes
164
     *
165
     * @return string
166
     */
167 632
    public function getIdAttribute($name, array $attributes)
168
    {
169 632
        if (array_key_exists('id', $attributes)) {
170 48
            return $attributes['id'];
171
        }
172
173 600
        if (in_array($name, $this->labels)) {
174 8
            return $name;
175
        }
176
177 592
        return null;
178
    }
179
180
    /**
181
     * Get the value that should be assigned to the field.
182
     *
183
     * @param  string  $name
184
     * @param  mixed   $value
185
     *
186
     * @return mixed
187
     */
188 560
    public function getValueAttribute($name, $value = null)
189
    {
190 560
        if (is_null($name)) {
191 24
            return $value;
192
        }
193
194 536
        if ( ! is_null($this->old($name))) {
195 32
            return $this->old($name);
196
        }
197
198 520
        if ( ! is_null($value)) {
199 296
            return $value;
200
        }
201
202 304
        if (isset($this->model)) {
203 40
            return $this->getModelValueAttribute($name);
204
        }
205
206 264
        return null;
207
    }
208
209
    /**
210
     * Get the model value that should be assigned to the field.
211
     *
212
     * @param  string  $name
213
     *
214
     * @return mixed
215
     */
216 64
    private function getModelValueAttribute($name)
217
    {
218 64
        return data_get($this->model, $this->transformKey($name));
219
    }
220
221
    /**
222
     * Get a value from the session's old input.
223
     *
224
     * @param  string  $name
225
     *
226
     * @return mixed
227
     */
228 560
    public function old($name)
229
    {
230 560
        return isset($this->session)
231 436
            ? $this->session->getOldInput($this->transformKey($name))
232 560
            : null;
233
    }
234
235
    /**
236
     * Transform key from array to dot syntax.
237
     *
238
     * @param  string  $key
239
     *
240
     * @return string
241
     */
242 96
    private function transformKey($key)
243
    {
244 96
        return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
245
    }
246
247
    /**
248
     * Determine if the old input is empty.
249
     *
250
     * @return bool
251
     */
252 24
    public function oldInputIsEmpty()
253
    {
254 24
        return isset($this->session) && (count($this->session->getOldInput()) == 0);
255
    }
256
257
    /**
258
     * Parse the form action method.
259
     *
260
     * @param  string  $method
261
     *
262
     * @return string
263
     */
264 128
    private function getMethod($method)
265
    {
266 128
        $method = strtoupper($method);
267
268 128
        return $method !== 'GET' ? 'POST' : $method;
269
    }
270
271
    /* ------------------------------------------------------------------------------------------------
272
     |  Main Functions
273
     | ------------------------------------------------------------------------------------------------
274
     */
275
    /**
276
    * Open up a new HTML form.
277
    *
278
    * @param  array  $options
279
    *
280
    * @return string
281
    */
282 128
    public function open(array $options = [])
283
    {
284 128
        $method = array_get($options, 'method', 'post');
285
286
        // We need to extract the proper method from the attributes. If the method is
287
        // something other than GET or POST we'll use POST since we will spoof the
288
        // actual method since forms don't support the reserved methods in HTML.
289 128
        $attributes = [];
290 128
        $attributes['method']         = $this->getMethod($method);
291 128
        $attributes['action']         = $this->getAction($options);
292 128
        $attributes['accept-charset'] = 'UTF-8';
293
294 128
        if (isset($options['files']) && $options['files']) {
295 16
            $options['enctype'] = 'multipart/form-data';
296 12
        }
297
298
        // Finally we're ready to create the final form HTML field. We will attribute
299
        // format the array of attributes. We will also add on the appendage which
300
        // is used to spoof requests for this PUT, PATCH, etc. methods on forms.
301 128
        $attributes = array_merge(
302 128
            $attributes, array_except($options, $this->reserved)
303 96
        );
304
305
        // Finally, we will concatenate all of the attributes into a single string so
306
        // we can build out the final form open statement. We'll also append on an
307
        // extra value for the hidden _method field if it's needed for the form.
308 128
        $attributes = $this->html->attributes($attributes);
309
310
        // If the method is PUT, PATCH or DELETE we will need to add a spoofer hidden
311
        // field that will instruct the Symfony request to pretend the method is a
312
        // different method than it actually is, for convenience from the forms.
313 128
        $append = $this->getAppendage($method);
314
315 128
        return '<form' . $attributes . '>' . $append;
316
    }
317
318
    /**
319
    * Create a new model based form builder.
320
    *
321
    * @param  mixed  $model
322
    * @param  array  $options
323
    *
324
    * @return string
325
    */
326 40
    public function model($model, array $options = [])
327
    {
328 40
        $this->setModel($model);
329
330 40
        return $this->open($options);
331
    }
332
333
    /**
334
     * Close the current form.
335
     *
336
     * @return string
337
     */
338 16
    public function close()
339
    {
340 16
        $this->labels = [];
341 16
        $this->setModel(null);
342
343 16
        return '</form>';
344
    }
345
346
    /**
347
     * Generate a hidden field with the current CSRF token.
348
     *
349
     * @return string
350
     */
351 56
    public function token()
352
    {
353 56
        $token = ! empty($this->csrfToken)
354 52
            ? $this->csrfToken
355 56
            : $this->session->getToken();
356
357 56
        return $this->hidden('_token', $token);
358
    }
359
360
    /**
361
     * Create a form label element.
362
     *
363
     * @param  string  $name
364
     * @param  string  $value
365
     * @param  array   $options
366
     *
367
     * @return string
368
     */
369 24
    public function label($name, $value = null, array $options = [])
370
    {
371 24
        $this->labels[] = $name;
372
373 24
        return Helpers\Label::make($name, $value, $options);
374
    }
375
376
    /**
377
     * Create a form input field.
378
     *
379
     * @param  string  $type
380
     * @param  string  $name
381
     * @param  string  $value
382
     * @param  array   $options
383
     *
384
     * @return string
385
     */
386 496
    public function input($type, $name, $value = null, array $options = [])
387
    {
388 496
        if ( ! isset($options['name'])) {
389 496
            $options['name'] = $name;
390 372
        }
391
392
        // We will get the appropriate value for the given field. We will look for the
393
        // value in the session for the value in the old input data then we'll look
394
        // in the model instance if one is set. Otherwise we will just use empty.
395 496
        $id = $this->getIdAttribute($name, $options);
396
397 496
        if ( ! in_array($type, $this->skipValueTypes)) {
398 400
            $value = $this->getValueAttribute($name, $value);
399 300
        }
400
401
        // Once we have the type, value, and ID we can merge them into the rest of the
402
        // attributes array so we can convert them into their HTML attribute format
403
        // when creating the HTML element. Then, we will return the entire input.
404 496
        $options = array_merge($options, compact('type', 'value', 'id'));
405
406 496
        return '<input' . $this->html->attributes($options) . '>';
407
    }
408
409
    /**
410
     * Create a text input field.
411
     *
412
     * @param  string  $name
413
     * @param  string  $value
414
     * @param  array   $options
415
     *
416
     * @return string
417
     */
418 48
    public function text($name, $value = null, array $options = [])
419
    {
420 48
        return $this->input('text', $name, $value, $options);
421
    }
422
423
    /**
424
     * Create a password input field.
425
     *
426
     * @param  string  $name
427
     * @param  array   $options
428
     *
429
     * @return string
430
     */
431 24
    public function password($name, array $options = [])
432
    {
433 24
        return $this->input('password', $name, '', $options);
434
    }
435
436
    /**
437
     * Create a hidden input field.
438
     *
439
     * @param  string  $name
440
     * @param  string  $value
441
     * @param  array   $options
442
     *
443
     * @return string
444
     */
445 80
    public function hidden($name, $value = null, array $options = [])
446
    {
447 80
        return $this->input('hidden', $name, $value, $options);
448
    }
449
450
    /**
451
     * Create an e-mail input field.
452
     *
453
     * @param  string  $name
454
     * @param  string  $value
455
     * @param  array   $options
456
     *
457
     * @return string
458
     */
459 24
    public function email($name, $value = null, array $options = [])
460
    {
461 24
        return $this->input('email', $name, $value, $options);
462
    }
463
464
    /**
465
     * Create a tel input field.
466
     *
467
     * @param  string  $name
468
     * @param  string  $value
469
     * @param  array   $options
470
     *
471
     * @return string
472
     */
473 24
    public function tel($name, $value = null, array $options = [])
474
    {
475 24
        return $this->input('tel', $name, $value, $options);
476
    }
477
478
    /**
479
     * Create a number input field.
480
     *
481
     * @param  string  $name
482
     * @param  string  $value
483
     * @param  array   $options
484
     *
485
     * @return string
486
     */
487 24
    public function number($name, $value = null, array $options = [])
488
    {
489 24
        return $this->input('number', $name, $value, $options);
490
    }
491
492
    /**
493
     * Create a date input field.
494
     *
495
     * @param  string  $name
496
     * @param  string  $value
497
     * @param  array   $options
498
     *
499
     * @return string
500
     */
501 32
    public function date($name, $value = null, array $options = [])
502
    {
503 32
        if ($value instanceof DateTime) {
504 8
            $value = $value->format('Y-m-d');
505 6
        }
506
507 32
        return $this->input('date', $name, $value, $options);
508
    }
509
510
    /**
511
     * Create a datetime input field.
512
     *
513
     * @param  string  $name
514
     * @param  string  $value
515
     * @param  array   $options
516
     *
517
     * @return string
518
     */
519 32
    public function datetime($name, $value = null, array $options = [])
520
    {
521 32
        if ($value instanceof DateTime) {
522 16
            $value = $value->format(DateTime::RFC3339);
523 12
        }
524
525 32
        return $this->input('datetime', $name, $value, $options);
526
    }
527
528
    /**
529
     * Create a datetime-local input field.
530
     *
531
     * @param  string  $name
532
     * @param  string  $value
533
     * @param  array   $options
534
     *
535
     * @return string
536
     */
537 32
    public function datetimeLocal($name, $value = null, array $options = [])
538
    {
539 32
        if ($value instanceof DateTime) {
540 16
            $value = $value->format('Y-m-d\TH:i');
541 12
        }
542
543 32
        return $this->input('datetime-local', $name, $value, $options);
544
    }
545
546
    /**
547
     * Create a time input field.
548
     *
549
     * @param  string  $name
550
     * @param  string  $value
551
     * @param  array   $options
552
     *
553
     * @return string
554
     */
555 24
    public function time($name, $value = null, array $options = [])
556
    {
557 24
        return $this->input('time', $name, $value, $options);
558
    }
559
560
    /**
561
     * Create a url input field.
562
     *
563
     * @param  string  $name
564
     * @param  string  $value
565
     * @param  array   $options
566
     *
567
     * @return string
568
     */
569 16
    public function url($name, $value = null, array $options = [])
570
    {
571 16
        return $this->input('url', $name, $value, $options);
572
    }
573
574
    /**
575
     * Create a file input field.
576
     *
577
     * @param  string  $name
578
     * @param  array   $options
579
     *
580
     * @return string
581
     */
582 24
    public function file($name, array $options = [])
583
    {
584 24
        return $this->input('file', $name, null, $options);
585
    }
586
587
    /**
588
     * Create a textarea input field.
589
     *
590
     * @param  string  $name
591
     * @param  string  $value
592
     * @param  array   $options
593
     *
594
     * @return string
595
     */
596 32
    public function textarea($name, $value = null, array $options = [])
597
    {
598 32
        if ( ! isset($options['name'])) {
599 32
            $options['name'] = $name;
600 24
        }
601
602
        // Next we will look for the rows and cols attributes, as each of these are put
603
        // on the textarea element definition. If they are not present, we will just
604
        // assume some sane default values for these attributes for the developer.
605 32
        $options       = $this->setTextAreaSize($options);
606 32
        $options['id'] = $this->getIdAttribute($name, $options);
607 32
        $value         = (string) $this->getValueAttribute($name, $value);
608
609 32
        unset($options['size']);
610
611
        // Next we will convert the attributes into a string form. Also we have removed
612
        // the size attribute, as it was merely a short-cut for the rows and cols on
613
        // the element. Then we'll create the final textarea elements HTML for us.
614 32
        $options = $this->html->attributes($options);
615
616 32
        return '<textarea' . $options . '>' . e($value) . '</textarea>';
617
    }
618
619
    /**
620
     * Set the text area size on the attributes.
621
     *
622
     * @param  array  $options
623
     *
624
     * @return array
625
     */
626 32
    private function setTextAreaSize(array $options)
627
    {
628 32
        if (isset($options['size'])) {
629 8
            return $this->setQuickTextAreaSize($options);
630
        }
631
632
        // If the "size" attribute was not specified, we will just look for the regular
633
        // columns and rows attributes, using sane defaults if these do not exist on
634
        // the attributes array. We'll then return this entire options array back.
635 24
        $cols = array_get($options, 'cols', 50);
636 24
        $rows = array_get($options, 'rows', 10);
637
638 24
        return array_merge($options, compact('cols', 'rows'));
639
    }
640
641
    /**
642
     * Set the text area size using the quick "size" attribute.
643
     *
644
     * @param  array  $options
645
     *
646
     * @return array
647
     */
648 8
    protected function setQuickTextAreaSize(array $options)
649
    {
650 8
        list($cols, $rows) = explode('x', $options['size']);
651
652 8
        return array_merge($options, [
653 8
            'cols' => $cols,
654 2
            'rows' => $rows
655 6
        ]);
656
    }
657
658
    /**
659
     * Create a select box field.
660
     *
661
     * @param  string  $name
662
     * @param  array   $list
663
     * @param  string  $selected
664
     * @param  array   $options
665
     *
666
     * @return string
667
     */
668 104
    public function select($name, $list = [], $selected = null, array $options = [])
669
    {
670
        // When building a select box the "value" attribute is really the selected one
671
        // so we will use that when checking the model or session for a value which
672
        // should provide a convenient method of re-populating the forms on post.
673 104
        $selected      = $this->getValueAttribute($name, $selected);
674
675
        // Transform to array if it is a collection
676 104
        if ($selected instanceof Collection) {
677 8
            $selected = $selected->all();
678 6
        }
679
680 104
        $options['id'] = $this->getIdAttribute($name, $options);
681
682 104
        if ( ! isset($options['name'])) {
683 72
            $options['name'] = $name;
684 54
        }
685
686
        // We will simply loop through the options and build an HTML value for each of
687
        // them until we have an array of HTML declarations. Then we will join them
688
        // all together into one single HTML element that can be put on the form.
689 104
        $html = [];
690
691 104
        if (isset($options['placeholder'])) {
692 8
            $html[] = $this->placeholderOption($options['placeholder'], $selected);
693 8
            unset($options['placeholder']);
694 6
        }
695
696 104
        foreach($list as $value => $display) {
697 96
            $html[] = $this->getSelectOption($display, $value, $selected);
698 78
        }
699
700
        // Once we have all of this HTML, we can join this into a single element after
701
        // formatting the attributes into an HTML "attributes" string, then we will
702
        // build out a final select statement, which will contain all the values.
703 104
        $options = $this->html->attributes($options);
704
705 104
        return "<select{$options}>" . implode('', $html) . "</select>";
706
    }
707
708
    /**
709
     * Create a select range field.
710
     *
711
     * @param  string  $name
712
     * @param  string  $begin
713
     * @param  string  $end
714
     * @param  string  $selected
715
     * @param  array   $options
716
     *
717
     * @return string
718
     */
719 16
    public function selectRange($name, $begin, $end, $selected = null, array $options = [])
720
    {
721 16
        $range = array_combine($range = range($begin, $end), $range);
722
723 16
        return $this->select($name, $range, $selected, $options);
724
    }
725
726
    /**
727
     * Create a select year field.
728
     *
729
     * @param  string  $name
730
     * @param  string  $begin
731
     * @param  string  $end
732
     * @param  string  $selected
733
     * @param  array   $options
734
     *
735
     * @return string
736
     */
737 8
    public function selectYear($name, $begin, $end, $selected = null, array $options = [])
738
    {
739 8
        return call_user_func_array(
740 8
            [$this, 'selectRange'],
741 8
            compact('name', 'begin', 'end', 'selected', 'options')
742 6
        );
743
    }
744
745
    /**
746
     * Create a select month field.
747
     *
748
     * @param  string  $name
749
     * @param  string  $selected
750
     * @param  array   $options
751
     * @param  string  $format
752
     *
753
     * @return string
754
     */
755 8
    public function selectMonth($name, $selected = null, array $options = [], $format = '%B')
756
    {
757 8
        $months = [];
758
759 8
        foreach(range(1, 12) as $month) {
760 8
            $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
761 6
        }
762
763 8
        return $this->select($name, $months, $selected, $options);
764
    }
765
766
    /**
767
     * Get the select option for the given value.
768
     *
769
     * @param  string  $display
770
     * @param  string  $value
771
     * @param  string  $selected
772
     *
773
     * @return string
774
     */
775 96
    private function getSelectOption($display, $value, $selected)
776
    {
777 96
        if (is_array($display)) {
778 8
            return $this->optionGroup($display, $value, $selected);
779
        }
780
781 88
        return $this->option($display, $value, $selected);
782
    }
783
784
    /**
785
     * Create an option group form element.
786
     *
787
     * @param  array   $list
788
     * @param  string  $label
789
     * @param  string  $selected
790
     *
791
     * @return string
792
     */
793 8
    private function optionGroup(array $list, $label, $selected)
794
    {
795 8
        $html = [];
796
797 8
        foreach($list as $value => $display) {
798 8
            $html[] = $this->option($display, $value, $selected);
799 6
        }
800
801 8
        return '<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>';
802
    }
803
804
    /**
805
     * Create a select element option.
806
     *
807
     * @param  string  $display
808
     * @param  string  $value
809
     * @param  string  $selected
810
     *
811
     * @return string
812
     */
813 96
    private function option($display, $value, $selected)
814
    {
815 96
        $selected = $this->getSelectedValue($value, $selected);
816 96
        $options  = compact('value', 'selected');
817
818 96
        return '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
819
    }
820
821
    /**
822
     * Create a placeholder select element option.
823
     *
824
     * @param  string  $display
825
     * @param  string  $selected
826
     *
827
     * @return string
828
     */
829 8
    private function placeholderOption($display, $selected)
830
    {
831 8
        $selected         = $this->getSelectedValue(null, $selected);
832 8
        $options          = compact('selected');
833 8
        $options['value'] = '';
834
835 8
        return '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
836
    }
837
838
    /**
839
     * Determine if the value is selected.
840
     *
841
     * @param  string  $value
842
     * @param  string  $selected
843
     *
844
     * @return string|null
845
     */
846 96
    private function getSelectedValue($value, $selected)
847
    {
848 96
        if (is_array($selected)) {
849 32
            return in_array($value, $selected) ? 'selected' : null;
850
        }
851
852 72
        return ((string) $value == (string) $selected) ? 'selected' : null;
853
    }
854
855
    /**
856
     * Create a checkbox input field.
857
     *
858
     * @param  string     $name
859
     * @param  mixed      $value
860
     * @param  bool|null  $checked
861
     * @param  array      $options
862
     *
863
     * @return string
864
     */
865 32
    public function checkbox($name, $value = 1, $checked = null, array $options = [])
866
    {
867 32
        return $this->checkable('checkbox', $name, $value, $checked, $options);
868
    }
869
870
    /**
871
     * Create a radio button input field.
872
     *
873
     * @param  string  $name
874
     * @param  mixed   $value
875
     * @param  bool    $checked
876
     * @param  array   $options
877
     *
878
     * @return string
879
     */
880 16
    public function radio($name, $value = null, $checked = null, array $options = [])
881
    {
882 16
        if (is_null($value)) {
883 8
            $value = $name;
884 6
        }
885
886 16
        return $this->checkable('radio', $name, $value, $checked, $options);
887
    }
888
889
    /**
890
     * Create a checkable input field.
891
     *
892
     * @param  string     $type
893
     * @param  string     $name
894
     * @param  mixed      $value
895
     * @param  bool|null  $checked
896
     * @param  array      $options
897
     *
898
     * @return string
899
     */
900 56
    protected function checkable($type, $name, $value, $checked, array $options)
901
    {
902 56
        $checked = $this->getCheckedState($type, $name, $value, $checked);
903
904 56
        if ( ! is_null($checked) && $checked) {
905 48
            $options['checked'] = 'checked';
906 36
        }
907
908 56
        return $this->input($type, $name, $value, $options);
909
    }
910
911
    /**
912
     * Get the check state for a checkable input.
913
     *
914
     * @param  string     $type
915
     * @param  string     $name
916
     * @param  mixed      $value
917
     * @param  bool|null  $checked
918
     *
919
     * @return bool
920
     */
921 56
    private function getCheckedState($type, $name, $value, $checked)
922
    {
923
        switch($type) {
924 56
            case 'checkbox':
925 32
                return $this->getCheckboxCheckedState($name, $value, $checked);
926
927 20
            case 'radio':
928 16
                return $this->getRadioCheckedState($name, $value, $checked);
929
930 6
            default:
931 8
                return $this->getValueAttribute($name) == $value;
932 6
        }
933
    }
934
935
    /**
936
     * Get the check state for a checkbox input.
937
     *
938
     * @param  string     $name
939
     * @param  mixed      $value
940
     * @param  bool|null  $checked
941
     *
942
     * @return bool
943
     */
944 32
    private function getCheckboxCheckedState($name, $value, $checked)
945
    {
946
        if (
947 32
            isset($this->session) &&
948 32
            ! $this->oldInputIsEmpty() &&
949 14
            is_null($this->old($name))
950 24
        ) {
951 8
            return false;
952
        }
953
954 32
        if ($this->missingOldAndModel($name)) {
955 16
            return $checked;
956
        }
957
958 16
        $posted = $this->getValueAttribute($name, $checked);
959
960 16
        if (is_array($posted)) {
961 8
            return in_array($value, $posted);
962
        }
963
964 16
        if ($posted instanceof Collection) {
965 8
            return $posted->contains('id', $value);
966
        }
967
968 16
        return (bool) $posted;
969
    }
970
971
    /**
972
     * Get the check state for a radio input.
973
     *
974
     * @param  string     $name
975
     * @param  mixed      $value
976
     * @param  bool|null  $checked
977
     *
978
     * @return bool
979
     */
980 16
    private function getRadioCheckedState($name, $value, $checked)
981
    {
982 16
        return $this->missingOldAndModel($name)
983 14
            ? $checked
984 16
            : $this->getValueAttribute($name) == $value;
985
    }
986
987
    /**
988
     * Determine if old input or model input exists for a key.
989
     *
990
     * @param  string  $name
991
     *
992
     * @return bool
993
     */
994 48
    private function missingOldAndModel($name)
995
    {
996 48
        return (is_null($this->old($name)) && is_null($this->getModelValueAttribute($name)));
997
    }
998
999
    /**
1000
     * Create a HTML reset input element.
1001
     *
1002
     * @param  string  $value
1003
     * @param  array   $attributes
1004
     *
1005
     * @return string
1006
     */
1007 8
    public function reset($value, array $attributes = [])
1008
    {
1009 8
        return $this->input('reset', null, $value, $attributes);
1010
    }
1011
1012
    /**
1013
    * Create a HTML image input element.
1014
    *
1015
    * @param  string  $url
1016
    * @param  string  $name
1017
    * @param  array   $attributes
1018
    *
1019
    * @return string
1020
    */
1021 8
    public function image($url, $name = null, array $attributes = [])
1022
    {
1023 8
        $attributes['src'] = $this->url->asset($url);
1024
1025 8
        return $this->input('image', $name, null, $attributes);
1026
    }
1027
1028
    /**
1029
     * Create a submit button element.
1030
     *
1031
     * @param  string  $value
1032
     * @param  array   $options
1033
     *
1034
     * @return string
1035
     */
1036 8
    public function submit($value = null, array $options = [])
1037
    {
1038 8
        return $this->input('submit', null, $value, $options);
1039
    }
1040
1041
    /**
1042
    * Create a button element.
1043
    *
1044
    * @param  string  $value
1045
    * @param  array   $options
1046
    *
1047
    * @return string
1048
    */
1049 8
    public function button($value = null, array $options = [])
1050
    {
1051 8
        if ( ! array_key_exists('type', $options)) {
1052 8
            $options['type'] = 'button';
1053 6
        }
1054
1055 8
        return '<button' . $this->html->attributes($options) . '>' . $value . '</button>';
1056
    }
1057
1058
    /**
1059
     * Create a color input field.
1060
     *
1061
     * @param  string  $name
1062
     * @param  string  $value
1063
     * @param  array   $options
1064
     *
1065
     * @return string
1066
     */
1067 24
    public function color($name, $value = null, array $options = [])
1068
    {
1069 24
        return $this->input('color', $name, $value, $options);
1070
    }
1071
1072
    /**
1073
     * Dynamically handle calls to the class.
1074
     *
1075
     * @param  string  $method
1076
     * @param  array   $parameters
1077
     *
1078
     * @return \Illuminate\Contracts\View\View|mixed
1079
     *
1080
     * @throws \BadMethodCallException
1081
     */
1082
    public function __call($method, $parameters)
1083
    {
1084
        try {
1085
            return $this->componentCall($method, $parameters);
1086
        }
1087
        catch (BadMethodCallException $e) {
1088
            // Continue
1089
        }
1090
1091
        return $this->macroCall($method, $parameters);
1092
    }
1093
1094
    /* ------------------------------------------------------------------------------------------------
1095
     |  Other Functions
1096
     | ------------------------------------------------------------------------------------------------
1097
     */
1098
    /**
1099
     * Get the form action from the options.
1100
     *
1101
     * @param  array  $options
1102
     *
1103
     * @return string
1104
     */
1105 128
    private function getAction(array $options)
1106
    {
1107
        // We will also check for a "route" or "action" parameter on the array so that
1108
        // developers can easily specify a route or controller action when creating
1109
        // a form providing a convenient interface for creating the form actions.
1110 128
        if (isset($options['url'])) {
1111 40
            return $this->getUrlAction($options['url']);
1112
        }
1113 88
        elseif (isset($options['route'])) {
1114 8
            return $this->getRouteAction($options['route']);
1115
        }
1116 88
        elseif (isset($options['action'])) {
1117
            // If an action is available, we are attempting to open a form to a controller
1118
            // action route. So, we will use the URL generator to get the path to these
1119
            // actions and return them from the method. Otherwise, we'll use current.
1120 8
            return $this->getControllerAction($options['action']);
1121
        }
1122
1123 80
        return $this->url->current();
1124
    }
1125
1126
    /**
1127
     * Get the action for a "url" option.
1128
     *
1129
     * @param  array|string  $options
1130
     *
1131
     * @return string
1132
     */
1133 40
    private function getUrlAction($options)
1134
    {
1135 40
        return is_array($options)
1136 30
            ? $this->url->to($options[0], array_slice($options, 1))
1137 40
            : $this->url->to($options);
1138
    }
1139
1140
    /**
1141
     * Get the action for a "route" option.
1142
     *
1143
     * @param  array|string  $options
1144
     *
1145
     * @return string
1146
     */
1147 8
    private function getRouteAction($options)
1148
    {
1149 8
        return is_array($options)
1150 6
            ? $this->url->route($options[0], array_slice($options, 1))
1151 8
            : $this->url->route($options);
1152
    }
1153
1154
    /**
1155
     * Get the action for an "action" option.
1156
     *
1157
     * @param  array|string  $options
1158
     *
1159
     * @return string
1160
     */
1161 8
    private function getControllerAction($options)
1162
    {
1163 8
        return is_array($options)
1164 6
            ? $this->url->action($options[0], array_slice($options, 1))
1165 8
            : $this->url->action($options);
1166
    }
1167
1168
    /**
1169
     * Get the form appendage for the given method.
1170
     *
1171
     * @param  string  $method
1172
     *
1173
     * @return string
1174
     */
1175 128
    private function getAppendage($method)
1176
    {
1177 128
        list($method, $appendage) = [strtoupper($method), ''];
1178
1179
        // If the HTTP method is in this list of spoofed methods, we will attach the
1180
        // method spoofer hidden input to the form. This allows us to use regular
1181
        // form to initiate PUT and DELETE requests in addition to the typical.
1182 128
        if (in_array($method, $this->spoofedMethods)) {
1183 16
            $appendage .= $this->hidden('_method', $method);
1184 12
        }
1185
1186
        // If the method is something other than GET we will go ahead and attach the
1187
        // CSRF token to the form, as this can't hurt and is convenient to simply
1188
        // always have available on every form the developers creates for them.
1189 128
        if ($method != 'GET') {
1190 56
            $appendage .= $this->token();
1191 42
        }
1192
1193 128
        return $appendage;
1194
    }
1195
}
1196