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