1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/form package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Form\Input; |
11
|
|
|
|
12
|
|
|
use Slick\Filter\StaticFilter; |
13
|
|
|
use Slick\Form\Input\Filter\Boolean; |
14
|
|
|
use Slick\Form\InputInterface; |
15
|
|
|
use Slick\Form\Renderer\Checkbox as CheckboxRenderer; |
16
|
|
|
use Slick\Form\Utils\AttributesMapInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Checkbox input |
20
|
|
|
* |
21
|
|
|
* @package Slick\Form\Input |
22
|
|
|
* @author Filipe Silva <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Checkbox extends AbstractInput implements InputInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $checked = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|mixed |
33
|
|
|
*/ |
34
|
|
|
protected $value = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string Renderer class |
38
|
|
|
*/ |
39
|
|
|
protected $rendererClass = CheckboxRenderer::class; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Crates the input with the attribute type as text |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct() |
45
|
|
|
{ |
46
|
4 |
|
$this->setAttribute('type', 'checkbox'); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets the value of the checkbox |
51
|
|
|
* |
52
|
|
|
* @param mixed $value |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
2 |
|
public function setValue($value) |
57
|
|
|
{ |
58
|
2 |
|
$this->checked = null; |
59
|
2 |
|
return parent::setValue($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check whenever the checkbox state is checked |
64
|
|
|
* |
65
|
|
|
* @return bool|mixed |
66
|
|
|
*/ |
67
|
4 |
|
public function isChecked() |
68
|
|
|
{ |
69
|
4 |
|
if (null == $this->checked) { |
70
|
4 |
|
$this->checked = StaticFilter::filter( |
71
|
4 |
|
Boolean::class, |
72
|
4 |
|
$this->getValue() |
73
|
4 |
|
); |
74
|
4 |
|
} |
75
|
4 |
|
return $this->checked; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the attributes map collection |
80
|
|
|
* |
81
|
|
|
* @return AttributesMapInterface |
82
|
|
|
*/ |
83
|
4 |
|
public function getAttributes() |
84
|
|
|
{ |
85
|
4 |
|
$attributes = parent::getAttributes(); |
86
|
4 |
|
if ($this->isChecked()) { |
87
|
2 |
|
$attributes->set('checked', null); |
88
|
2 |
|
} |
89
|
4 |
|
return $attributes; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
} |