1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\Element; |
6
|
|
|
use Fiv\Form\FormData; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Generate <input type="submit" /> element |
10
|
|
|
* |
11
|
|
|
* @author Ivan Shcherbak <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Checkbox extends Element\Input { |
14
|
|
|
|
15
|
|
|
protected $attributes = [ |
16
|
|
|
'type' => 'checkbox', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $label = ''; |
23
|
|
|
|
24
|
|
|
protected $isChecked = false; |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
public function isValid() { |
28
|
1 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
8 |
|
public function handle(FormData $data) { |
36
|
8 |
|
$this->setChecked($data->has($this->getName())); |
37
|
8 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
7 |
|
public function isChecked() : bool { |
42
|
7 |
|
return $this->isChecked; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
8 |
|
public function setChecked(bool $isChecked) : self { |
47
|
8 |
|
$this->isChecked = ($isChecked === true); |
48
|
8 |
|
if ($this->isChecked) { |
49
|
8 |
|
$this->setAttribute('checked', 'checked'); |
50
|
|
|
} else { |
51
|
2 |
|
$this->removeAttribute('checked'); |
52
|
|
|
} |
53
|
8 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
9 |
|
public function setAttribute($name, $value) { |
61
|
9 |
|
if ($name == 'checked') { |
62
|
8 |
|
$this->isChecked = true; |
63
|
|
|
} |
64
|
9 |
|
return parent::setAttribute($name, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
2 |
|
public function removeAttribute($name) { |
72
|
2 |
|
if ($name == 'checked') { |
73
|
2 |
|
$this->isChecked = false; |
74
|
|
|
} |
75
|
2 |
|
return parent::removeAttribute($name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $text |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setLabel($text) { |
84
|
|
|
$this->label = $text; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
public function render() { |
93
|
1 |
|
return '<label>' . static::tag($this->tag, $this->attributes) . $this->label . '</label>'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @deprecated |
102
|
|
|
*/ |
103
|
|
|
public function getValue() { |
104
|
|
|
return $this->isChecked() ? 1 : 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @deprecated |
110
|
|
|
*/ |
111
|
|
|
public function setValue($value) { |
112
|
|
|
trigger_error('Deprecated', E_USER_DEPRECATED); |
113
|
|
|
return parent::setValue($value); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |