Passed
Push — dependabot/github_actions/shiv... ( a0bd3e )
by
unknown
10:33
created

CheckBoxList::items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use Closure;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Html\Widget\CheckboxList\CheckboxItem;
10
use Yiisoft\Widget\Widget;
11
12
final class CheckBoxList extends Widget
13
{
14
    private FormModelInterface $data;
15
    private string $attribute;
16
    private array $options = [];
17
    private array $items = [];
18
    private bool $noUnselect = false;
19
    private string $unselect = '';
20
21
    /**
22
     * @psalm-var Closure(CheckboxItem):string|null
23
     */
24
    private ?Closure $itemFormatter = null;
25
26
    /**
27
     * Generates a list of checkboxes.
28
     *
29
     * A checkbox list allows multiple selection, like {@see ListBox}.
30
     *
31
     * @throws \Yiisoft\Factory\Exception\InvalidConfigException
32
     *
33
     * @return string the generated checkbox list.
34
     */
35 14
    public function run(): string
36
    {
37 14
        return ListInput::widget()
38 14
            ->type('checkBoxList')
0 ignored issues
show
Bug introduced by
The method type() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Widget\ListBox or Yiisoft\Form\Widget\Input or Yiisoft\Form\Widget\ListInput or Yiisoft\Form\Widget\BooleanInput. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            ->/** @scrutinizer ignore-call */ type('checkBoxList')
Loading history...
39 14
            ->config($this->data, $this->attribute, $this->options)
40 14
            ->items($this->items)
41 14
            ->item($this->itemFormatter)
42 14
            ->noUnselect($this->noUnselect)
43 14
            ->unselect($this->unselect)
44 14
            ->run();
45
    }
46
47
    /**
48
     * Set form model, name and options for the widget.
49
     *
50
     * @param FormModelInterface $data Form model.
51
     * @param string $attribute Form model property this widget is rendered for.
52
     * @param array $options The HTML attributes for the widget container tag.
53
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
54
     *
55
     * @return self
56
     */
57 14
    public function config(FormModelInterface $data, string $attribute, array $options = []): self
58
    {
59 14
        $new = clone $this;
60 14
        $new->data = $data;
61 14
        $new->attribute = $attribute;
62 14
        $new->options = $options;
63 14
        return $new;
64
    }
65
66
    /**
67
     * Whether to HTML-encode the list of checkboxes labels.
68
     *
69
     * Defaults to true. This option is ignored if item option is set.
70
     *
71
     * @param bool $value
72
     *
73
     * @return self
74
     */
75 1
    public function noEncode(bool $value = false): self
76
    {
77 1
        $new = clone $this;
78 1
        $new->options['itemOptions']['encode'] = $value;
79 1
        return $new;
80
    }
81
82
    /**
83
     * Set whether the element is disabled or not.
84
     *
85
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
86
     * text.
87
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
88
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
89
     * this will suggest you can set it to false to enable the element again, which is not the case.
90
     *
91
     * @param bool $value
92
     *
93
     * @return self
94
     */
95 1
    public function disabled(bool $value = true): self
96
    {
97 1
        $new = clone $this;
98 1
        $new->options['disabled'] = $value;
99 1
        return $new;
100
    }
101
102
    /**
103
     * Callable, a callback that can be used to customize the generation of the HTML code corresponding to a single
104
     * item in $items.
105
     *
106
     * The signature of this callback must be:
107
     *
108
     * ```php
109
     * function ($index, $label, $name, $checked, $value)
110
     * ```
111
     *
112
     * @param Closure $formatter
113
     *
114
     * @return self
115
     */
116 1
    public function item(?Closure $formatter): self
117
    {
118 1
        $new = clone $this;
119 1
        $new->itemFormatter = $formatter;
120 1
        return $new;
121
    }
122
123
    /**
124
     * The data used to generate the list of checkboxes.
125
     *
126
     * The array keys are the list of checkboxes values, and the array values are the corresponding labels.
127
     *
128
     * Note that the labels will NOT be HTML-encoded, while the values will.
129
     *
130
     * @param array $value
131
     *
132
     * @return self
133
     */
134 14
    public function items(array $value = []): self
135
    {
136 14
        $new = clone $this;
137 14
        $new->items = $value;
138 14
        return $new;
139
    }
140
141
    /**
142
     * The options for generating the list of checkboxes tag using {@see CheckBoxList}.
143
     *
144
     * @param array $value
145
     *
146
     * @return self
147
     */
148 2
    public function itemOptions(array $value = []): self
149
    {
150 2
        $new = clone $this;
151 2
        $new->options['itemOptions'] = $value;
152 2
        return $new;
153
    }
154
155
    /**
156
     * Allows you to disable the widgets hidden input tag.
157
     *
158
     * @param bool $value
159
     *
160
     * @return self
161
     */
162 1
    public function noUnselect(bool $value = true): self
163
    {
164 1
        $new = clone $this;
165 1
        $new->noUnselect = $value;
166 1
        return $new;
167
    }
168
169
    /**
170
     * The HTML code that separates items.
171
     *
172
     * @param string $value
173
     *
174
     * @return self
175
     */
176 1
    public function separator(string $value = ''): self
177
    {
178 1
        $new = clone $this;
179 1
        $new->options['separator'] = $value;
180 1
        return $new;
181
    }
182
183
    /**
184
     * The tag name of the container element.
185
     *
186
     * Null to render list of checkboxes without container {@see \Yiisoft\Html\Html::tag()}.
187
     *
188
     * @param string|null $value
189
     *
190
     * @return self
191
     */
192 1
    public function tag(?string $value = null): self
193
    {
194 1
        $new = clone $this;
195 1
        $new->options['tag'] = $value;
196 1
        return $new;
197
    }
198
199
    /**
200
     * The value that should be submitted when none of the list of checkboxes is selected.
201
     *
202
     * You may set this option to be null to prevent default value submission. If this option is not set, an empty
203
     * string will be submitted.
204
     *
205
     * @param string $value
206
     *
207
     * @return self
208
     */
209 1
    public function unselect(string $value = ''): self
210
    {
211 1
        $new = clone $this;
212 1
        $new->unselect = $value;
213 1
        return $new;
214
    }
215
}
216