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