1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\FormData; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Generate |
10
|
|
|
* ```html |
11
|
|
|
* <input type="checkbox" name="languages[]" value="en"> |
12
|
|
|
* <input type="checkbox" name="languages[]" value="ru"> |
13
|
|
|
* ``` |
14
|
|
|
* html tags |
15
|
|
|
* |
16
|
|
|
* @author Ivan Shcherbak <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class CheckboxList extends Multiple { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public function handle(FormData $data) { |
24
|
|
|
$values = $data->get($this->getName()); |
25
|
|
|
if ($values === null) { |
26
|
|
|
$values = []; |
27
|
|
|
} else { |
28
|
|
|
$values = (array) $values; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->setValue($values); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $data |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function setValue($data) { |
41
|
|
|
if (!is_array($data)) { |
42
|
|
|
$data = (array) $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($data as $i => $value) { |
46
|
|
|
# remove invalid values |
47
|
|
|
if (!isset($this->options[$value])) { |
48
|
|
|
unset($data[$i]); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->value = array_values($data); |
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getValue() { |
61
|
|
|
return (array) parent::getValue(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $optionKey |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isChecked($optionKey) { |
70
|
|
|
return in_array($optionKey, $this->getValue()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Alias of setValue |
76
|
|
|
* |
77
|
|
|
* @param mixed $values |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setChecked($values) { |
81
|
|
|
return $this->setValue($values); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function render() { |
89
|
|
|
$html = ''; |
90
|
|
|
$currentValues = $this->getValue(); |
91
|
|
|
$attributes = $this->attributes; |
92
|
|
|
$attributes['type'] = 'checkbox'; |
93
|
|
|
|
94
|
|
|
$name = $this->getName(); |
95
|
|
|
|
96
|
|
|
foreach ($this->options as $value => $text) { |
97
|
|
|
$attributes['value'] = $value; |
98
|
|
|
$attributes['name'] = $name . '[]'; |
99
|
|
|
if (in_array($value, $currentValues)) { |
100
|
|
|
$attributes['checked'] = 'checked'; |
101
|
|
|
} else { |
102
|
|
|
unset($attributes['checked']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$html .= '<label>' . static::tag('input', $attributes) . $text . '</label>'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $html; |
109
|
|
|
} |
110
|
|
|
} |
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..