|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tuum\Form; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Tuum\Form\Data\Inputs; |
|
6
|
|
|
use Tuum\Form\Tags\Attribute; |
|
7
|
|
|
use Tuum\Form\Tags\Input; |
|
8
|
|
|
use Tuum\Form\Tags\InputList; |
|
9
|
|
|
use Tuum\Form\Tags\Select; |
|
10
|
|
|
use Tuum\Form\Tags\Tag; |
|
11
|
|
|
use Tuum\Form\Tags\TextArea; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Form |
|
15
|
|
|
* |
|
16
|
|
|
* @package Tuum\Form |
|
17
|
|
|
* |
|
18
|
|
|
* @method Input text(string $name, string $value = null) |
|
19
|
|
|
* @method Input hidden(string $name, string $value = null) |
|
20
|
|
|
* @method Input search(string $name, string $value = null) |
|
21
|
|
|
* @method Input tel(string $name, string $value = null) |
|
22
|
|
|
* @method Input url(string $name, string $value = null) |
|
23
|
|
|
* @method Input email(string $name, string $value = null) |
|
24
|
|
|
* @method Input password(string $name, string $value = null) |
|
25
|
|
|
* @method Input datetime(string $name, string $value = null) |
|
26
|
|
|
* @method Input date(string $name, string $value = null) |
|
27
|
|
|
* @method Input month(string $name, string $value = null) |
|
28
|
|
|
* @method Input week(string $name, string $value = null) |
|
29
|
|
|
* @method Input time(string $name, string $value = null) |
|
30
|
|
|
* @method Input number(string $name, string $value = null) |
|
31
|
|
|
* @method Input range(string $name, string $value = null) |
|
32
|
|
|
* @method Input color(string $name, string $value = null) |
|
33
|
|
|
* @method Input file(string $name, string $value = null) |
|
34
|
|
|
*/ |
|
35
|
|
|
class Forms |
|
36
|
|
|
{ |
|
37
|
|
|
private $inputTags = [ |
|
38
|
|
|
'text', |
|
39
|
|
|
'hidden', |
|
40
|
|
|
'search', |
|
41
|
|
|
'tel', |
|
42
|
|
|
'url', |
|
43
|
|
|
'email', |
|
44
|
|
|
'password', |
|
45
|
|
|
'datetime', |
|
46
|
|
|
'date', |
|
47
|
|
|
'month', |
|
48
|
|
|
'week', |
|
49
|
|
|
'time', |
|
50
|
|
|
'number', |
|
51
|
|
|
'range', |
|
52
|
|
|
'color', |
|
53
|
|
|
'file', |
|
54
|
|
|
'radio', |
|
55
|
|
|
'checkbox', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Inputs |
|
60
|
|
|
*/ |
|
61
|
|
|
private $inputs; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
private $default_class = ''; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Inputs $inputs |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function withInputs($inputs) |
|
73
|
|
|
{ |
|
74
|
|
|
$self = clone($this); |
|
75
|
|
|
$self->inputs = $inputs; |
|
76
|
|
|
return $self; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* sets default class name for input form elements. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $class |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function withClass($class) |
|
86
|
|
|
{ |
|
87
|
|
|
$self = clone($this); |
|
88
|
|
|
$self->default_class = $class; |
|
89
|
|
|
return $self; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Tag $form |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
private function setClass($form) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->default_class) { |
|
99
|
|
|
$form->class($this->default_class); |
|
100
|
|
|
} |
|
101
|
|
|
return $form; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $type |
|
106
|
|
|
* @param array $args |
|
107
|
|
|
* @return $this|string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function __call($type, $args) |
|
110
|
|
|
{ |
|
111
|
|
|
/* |
|
112
|
|
|
* create Input objects. |
|
113
|
|
|
*/ |
|
114
|
|
|
if (in_array($type, $this->inputTags)) { |
|
115
|
|
|
if (!array_key_exists(0, $args)) { |
|
116
|
|
|
throw new \InvalidArgumentException(); |
|
117
|
|
|
} |
|
118
|
|
|
$name = $args[0]; |
|
119
|
|
|
$value = array_key_exists(1, $args) ? $args[1] : null; |
|
120
|
|
|
return $this->input($type, $name, $value); |
|
121
|
|
|
} |
|
122
|
|
|
return ''; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* constructs Input form element object with any type. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $type |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* @param string|null $value |
|
131
|
|
|
* @return Input |
|
132
|
|
|
*/ |
|
133
|
|
|
public function input($type, $name, $value = null) |
|
134
|
|
|
{ |
|
135
|
|
|
$value = $this->inputs ? $this->inputs->get($name, $value) : $value; |
|
136
|
|
|
$form = (new Input($type, $name))->value($value); |
|
137
|
|
|
return $this->setClass($form); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* construct Input[type=radio] form element object. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @param string $value |
|
145
|
|
|
* @return Input |
|
146
|
|
|
*/ |
|
147
|
|
|
public function radio($name, $value) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->checkedInput('radio', $name, $value); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* construct Input[type=radio] form element object. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $name |
|
156
|
|
|
* @param string $value |
|
157
|
|
|
* @return Input |
|
158
|
|
|
*/ |
|
159
|
|
|
public function checkbox($name, $value) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->checkedInput('checkbox', $name, $value); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $type |
|
166
|
|
|
* @param string $name |
|
167
|
|
|
* @param string $value |
|
168
|
|
|
* @return Input |
|
169
|
|
|
*/ |
|
170
|
|
|
private function checkedInput($type, $name, $value) |
|
171
|
|
|
{ |
|
172
|
|
|
$form = (new Input($type, $name))->value($value); |
|
173
|
|
|
if ($this->inputs && $this->inputs->exists($name, $value)) { |
|
174
|
|
|
$form->checked(); |
|
175
|
|
|
} |
|
176
|
|
|
return $this->setClass($form); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $label |
|
181
|
|
|
* @param null|string $for |
|
182
|
|
|
* @return Tag |
|
183
|
|
|
*/ |
|
184
|
|
|
public function label($label, $for = null) |
|
185
|
|
|
{ |
|
186
|
|
|
return (new Tag('label'))->contents($label)->setAttribute('for', $for); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param null $value |
|
191
|
|
|
* @return Input |
|
192
|
|
|
*/ |
|
193
|
|
|
public function submit($value = null) |
|
194
|
|
|
{ |
|
195
|
|
|
return (new Input('submit', null))->value($value); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param null $value |
|
200
|
|
|
* @return Input |
|
201
|
|
|
*/ |
|
202
|
|
|
public function reset($value = null) |
|
203
|
|
|
{ |
|
204
|
|
|
return (new Input('reset', null))->value($value); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param string $name |
|
209
|
|
|
* @param string|null $value |
|
210
|
|
|
* @return TextArea |
|
211
|
|
|
*/ |
|
212
|
|
|
public function textArea($name, $value = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$value = $this->inputs ? $this->inputs->raw($name, $value) : $value; |
|
215
|
|
|
$form = (new TextArea($name))->contents($value); |
|
216
|
|
|
return $this->setClass($form); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $name |
|
221
|
|
|
* @param array|Traversable $list |
|
222
|
|
|
* @param null|string $value |
|
223
|
|
|
* @return Select |
|
224
|
|
|
*/ |
|
225
|
|
|
public function select($name, $list, $value = null) |
|
226
|
|
|
{ |
|
227
|
|
|
$value = $this->inputs ? $this->inputs->raw($name, $value) : $value; |
|
228
|
|
|
$form = new Select($name, $list, $value); |
|
229
|
|
|
return $this->setClass($form); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param string $name |
|
234
|
|
|
* @param array|Traversable $list |
|
235
|
|
|
* @param null|string $value |
|
236
|
|
|
* @return InputList |
|
237
|
|
|
*/ |
|
238
|
|
|
public function checkList($name, $list, $value = null) |
|
239
|
|
|
{ |
|
240
|
|
|
$value = $this->inputs ? $this->inputs->raw($name, $value) : $value; |
|
241
|
|
|
return new InputList('checkbox', $name, $list, $value); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param string $name |
|
246
|
|
|
* @param array|Traversable $list |
|
247
|
|
|
* @param null|string $value |
|
248
|
|
|
* @return InputList |
|
249
|
|
|
*/ |
|
250
|
|
|
public function radioList($name, $list, $value = null) |
|
251
|
|
|
{ |
|
252
|
|
|
$value = $this->inputs ? $this->inputs->raw($name, $value) : $value; |
|
253
|
|
|
return new InputList('radio', $name, $list, $value); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return Tags\Form |
|
258
|
|
|
*/ |
|
259
|
|
|
public function open() |
|
260
|
|
|
{ |
|
261
|
|
|
return new Tags\Form(); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
|
|
public function close() |
|
268
|
|
|
{ |
|
269
|
|
|
return Tags\Form::close(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param string $arg |
|
274
|
|
|
* @return Tag |
|
275
|
|
|
*/ |
|
276
|
|
|
public function formGroup($arg = '') |
|
|
|
|
|
|
277
|
|
|
{ |
|
278
|
|
|
$html = ""; |
|
279
|
|
|
$args = func_get_args(); |
|
280
|
|
|
foreach ($args as $element) { |
|
281
|
|
|
$html .= "\n " . $element; |
|
282
|
|
|
} |
|
283
|
|
|
$div = new Tag('div'); |
|
284
|
|
|
$div->contents($html)->class('form-group'); |
|
285
|
|
|
return $div; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param array $attribute |
|
290
|
|
|
* @return Attribute |
|
291
|
|
|
*/ |
|
292
|
|
|
public function newAttribute(array $attribute = []) |
|
293
|
|
|
{ |
|
294
|
|
|
return new Attribute($attribute); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.