1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Phalcon\Html\Helper\Input; |
13
|
|
|
|
14
|
|
|
use Phalcon\Html\Escaper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Checkbox |
18
|
|
|
* |
19
|
|
|
* @property array $label |
20
|
|
|
*/ |
21
|
|
|
class Checkbox extends AbstractInput |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $label = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $type = "checkbox"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* AbstractHelper constructor. |
35
|
|
|
* |
36
|
|
|
* @param Escaper $escaper |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(Escaper $escaper) |
39
|
|
|
{ |
40
|
2 |
|
parent::__construct($escaper); |
41
|
|
|
|
42
|
2 |
|
$this->label = [ |
43
|
|
|
"start" => "", |
44
|
|
|
"text" => "", |
45
|
|
|
"end" => "", |
46
|
|
|
]; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the HTML for the input. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public function __toString() |
55
|
|
|
{ |
56
|
2 |
|
$this->processChecked(); |
57
|
2 |
|
$unchecked = $this->processUnchecked(); |
58
|
2 |
|
$element = parent::__toString(); |
59
|
2 |
|
$label = $this->label; |
60
|
2 |
|
$this->label = [ |
61
|
|
|
"start" => "", |
62
|
|
|
"text" => "", |
63
|
|
|
"end" => "", |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
return $unchecked |
67
|
2 |
|
. $label["start"] |
68
|
2 |
|
. $element |
69
|
2 |
|
. $label["text"] |
70
|
2 |
|
. $label["end"]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Attaches a label to the element |
75
|
|
|
* |
76
|
|
|
* @param array $attributes |
77
|
|
|
* |
78
|
|
|
* @return Checkbox |
79
|
|
|
*/ |
80
|
2 |
|
public function label(array $attributes = []): Checkbox |
81
|
|
|
{ |
82
|
2 |
|
$text = $attributes["text"] ?? ""; |
83
|
2 |
|
unset($attributes["text"]); |
84
|
|
|
|
85
|
2 |
|
$attributes = array_merge( |
86
|
|
|
[ |
87
|
2 |
|
'for' => $this->attributes["id"], |
88
|
|
|
], |
89
|
|
|
$attributes |
90
|
|
|
); |
91
|
|
|
|
92
|
2 |
|
$this->label = [ |
93
|
2 |
|
"start" => $this->renderTag('label', $attributes), |
94
|
2 |
|
"text" => $text, |
95
|
2 |
|
"end" => "</label>", |
96
|
|
|
]; |
97
|
|
|
|
98
|
2 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Processes the checked value |
103
|
|
|
*/ |
104
|
2 |
|
private function processChecked(): void |
105
|
|
|
{ |
106
|
2 |
|
$checked = $this->attributes["checked"] ?? ""; |
107
|
2 |
|
unset($this->attributes["checked"]); |
108
|
|
|
|
109
|
2 |
|
if (!empty($checked)) { |
110
|
2 |
|
$value = $this->attributes["value"] ?? ""; |
111
|
2 |
|
if ($checked === $value) { |
112
|
2 |
|
$this->attributes["checked"] = "checked"; |
113
|
|
|
} |
114
|
|
|
} |
115
|
2 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the unchecked hidden element if available |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
2 |
|
private function processUnchecked(): string |
123
|
|
|
{ |
124
|
2 |
|
$unchecked = $this->attributes["unchecked"] ?? ""; |
125
|
2 |
|
unset($this->attributes["unchecked"]); |
126
|
|
|
|
127
|
2 |
|
if (!empty($unchecked)) { |
128
|
2 |
|
$unchecked = $this->renderTag( |
129
|
2 |
|
"hidden", |
130
|
|
|
[ |
131
|
2 |
|
"name" => $this->attributes["name"], |
132
|
2 |
|
"value" => $unchecked, |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
return $unchecked; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|