|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of the Aura project for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Aura.Input |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT-license.php MIT |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Aura\Input; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* A single field in a fieldset. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Aura.Input |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class Field extends AbstractInput |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* The field type. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $type; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* HTML attributes for the field as key-value pairs. The key is the |
|
34
|
|
|
* attribute name and the value is the attribute value. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $attribs = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* Options for the field as key-value pairs (typically for select and |
|
44
|
|
|
* radio elements). The key is the option value and the value is the |
|
45
|
|
|
* option label. Nested options may be honored as the key being an |
|
46
|
|
|
* optgroup label and the array value as the options under that optgroup. |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $options = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* The value for the field. This may or may not be the same as the |
|
56
|
|
|
* 'value' attribue. |
|
57
|
|
|
* |
|
58
|
|
|
* @var mixed |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $value; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* Constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $type The field type. |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct($type) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->type = $type; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* Fills this field with a value. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $value The value for the field. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
* |
|
83
|
|
|
*/ |
|
84
|
|
|
public function fill($value) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->value = $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
* Reads the value from this field. |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
* |
|
95
|
|
|
*/ |
|
96
|
|
|
public function read() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* Returns this field as a plain old PHP array for use in a view. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array An array with keys `'type'`, `'name'`, `'attribs'`, |
|
106
|
|
|
* `'options'`, and `'value'`. |
|
107
|
|
|
* |
|
108
|
|
|
*/ |
|
109
|
|
|
public function get() |
|
110
|
|
|
{ |
|
111
|
|
|
$attribs = array_merge( |
|
112
|
|
|
[ |
|
113
|
|
|
// force a particular order on some attributes |
|
114
|
|
|
'id' => null, |
|
115
|
|
|
'type' => null, |
|
116
|
|
|
'name' => null, |
|
117
|
|
|
], |
|
118
|
|
|
$this->attribs |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
return [ |
|
122
|
|
|
'type' => $this->type, |
|
123
|
|
|
'name' => $this->getFullName(), |
|
124
|
|
|
'attribs' => $attribs, |
|
125
|
|
|
'options' => $this->options, |
|
126
|
|
|
'value' => $this->value, |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* |
|
132
|
|
|
* Sets the HTML attributes on this field. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $attribs HTML attributes for the field as key-value pairs; |
|
135
|
|
|
* the key is the attribute name and the value is the attribute value. |
|
136
|
|
|
* |
|
137
|
|
|
* @return self |
|
138
|
|
|
* |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setAttribs(array $attribs) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->attribs = $attribs; |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* |
|
148
|
|
|
* Sets the value options for this field, typically for select and radios. |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $options Options for the field as key-value pairs. The key |
|
151
|
|
|
* is the option value and the value is the option label. Nested options |
|
152
|
|
|
* may be honored as the key being an optgroup label and the array value |
|
153
|
|
|
* as the options under that optgroup. |
|
154
|
|
|
* |
|
155
|
|
|
* @return self |
|
156
|
|
|
* |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setOptions(array $options) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->options = $options; |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* |
|
166
|
|
|
* Sets the value on this field. |
|
167
|
|
|
* |
|
168
|
|
|
* @param mixed $value The value for the field. |
|
169
|
|
|
* |
|
170
|
|
|
* @return self |
|
171
|
|
|
* |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setValue($value) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->value = $value; |
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* |
|
181
|
|
|
* Returns the value of this input for use in arrays. |
|
182
|
|
|
* |
|
183
|
|
|
* @return mixed |
|
184
|
|
|
* |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getValue() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->value; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|