Completed
Push — master ( 0c661b...2c4834 )
by jxlwqq
05:03 queued 02:15
created

Checkbox::groups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class Checkbox extends MultipleSelect
8
{
9
    protected $inline = true;
10
11
    protected $canCheckAll = false;
12
13
    protected $groups = null;
14
15
    protected static $css = [
16
        '/vendor/laravel-admin/AdminLTE/plugins/iCheck/all.css',
17
    ];
18
19
    protected static $js = [
20
        '/vendor/laravel-admin/AdminLTE/plugins/iCheck/icheck.min.js',
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    protected $cascadeEvent = 'ifChanged';
27
28
    /**
29
     * Set options.
30
     *
31
     * @param array|callable|string $options
32
     *
33
     * @return $this|mixed
34
     */
35
    public function options($options = [])
36
    {
37
        if ($options instanceof Arrayable) {
38
            $options = $options->toArray();
39
        }
40
41
        if (is_callable($options)) {
42
            $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type callable is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        } else {
44
            $this->options = (array) $options;
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * Add a checkbox above this component, so you can select all checkboxes by click on it.
52
     *
53
     * @return $this
54
     */
55
    public function canCheckAll()
56
    {
57
        $this->canCheckAll = true;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Set chekbox groups.
64
     *
65
     * @param array
66
     *
67
     * @return $this
68
     */
69
    public function groups($groups = [])
70
    {
71
        $this->groups = $groups;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set checked.
78
     *
79
     * @param array|callable|string $checked
80
     *
81
     * @return $this
82
     */
83 View Code Duplication
    public function checked($checked = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        if ($checked instanceof Arrayable) {
86
            $checked = $checked->toArray();
87
        }
88
89
        $this->checked = (array) $checked;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Draw inline checkboxes.
96
     *
97
     * @return $this
98
     */
99
    public function inline()
100
    {
101
        $this->inline = true;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Draw stacked checkboxes.
108
     *
109
     * @return $this
110
     */
111
    public function stacked()
112
    {
113
        $this->inline = false;
114
115
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function render()
122
    {
123
        $this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});";
124
125
        $this->addVariables([
126
            'checked'     => $this->checked,
127
            'inline'      => $this->inline,
128
            'canCheckAll' => $this->canCheckAll,
129
            'groups'      => $this->groups,
130
        ]);
131
132
        if ($this->canCheckAll) {
133
            $checkAllClass = uniqid('check-all-');
134
135
            $this->script .= <<<SCRIPT
136
$('.{$checkAllClass}').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
137
    if (this.checked) {
138
        $('{$this->getElementClassSelector()}').iCheck('check');
139
    } else {
140
        $('{$this->getElementClassSelector()}').iCheck('uncheck');
141
    }
142
});
143
SCRIPT;
144
            $this->addVariables(['checkAllClass' => $checkAllClass]);
145
        }
146
147
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type Illuminate\Contracts\Vie...minate\View\View|string adds the type Illuminate\Contracts\View\Factory to the return on line 147 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
148
    }
149
}
150