|
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
|
1 |
|
public function handle(FormData $data) { |
|
24
|
1 |
|
$values = $data->get($this->getName()); |
|
25
|
1 |
|
if ($values === null) { |
|
26
|
|
|
$values = []; |
|
27
|
|
|
} else { |
|
28
|
1 |
|
$values = (array) $values; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
$this->setValue($values); |
|
32
|
1 |
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $data |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function setValue($data) { |
|
41
|
1 |
|
if (!is_array($data)) { |
|
42
|
|
|
$data = (array) $data; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
foreach ($data as $i => $value) { |
|
46
|
|
|
# remove invalid values |
|
47
|
1 |
|
if (!isset($this->options[$value])) { |
|
48
|
1 |
|
unset($data[$i]); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$this->value = array_values($data); |
|
|
|
|
|
|
53
|
1 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function getValue() { |
|
61
|
2 |
|
return (array) parent::getValue(); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $optionKey |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function isChecked($optionKey) { |
|
70
|
1 |
|
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
|
1 |
|
public function render() { |
|
89
|
1 |
|
$html = ''; |
|
90
|
1 |
|
$currentValues = $this->getValue(); |
|
91
|
1 |
|
$attributes = $this->attributes; |
|
92
|
1 |
|
$attributes['type'] = 'checkbox'; |
|
93
|
|
|
|
|
94
|
1 |
|
$name = $this->getName(); |
|
95
|
|
|
|
|
96
|
1 |
|
foreach ($this->options as $value => $text) { |
|
97
|
1 |
|
$attributes['value'] = $value; |
|
98
|
1 |
|
$attributes['name'] = $name . '[]'; |
|
99
|
1 |
|
if (in_array($value, $currentValues)) { |
|
100
|
|
|
$attributes['checked'] = 'checked'; |
|
101
|
|
|
} else { |
|
102
|
1 |
|
unset($attributes['checked']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
$html .= '<label>' . static::tag('input', $attributes) . $text . '</label>'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
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..