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