1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Cygnite package.
5
+ *
6
+ * (c) Sanjoy Dey <dey.sanjoy0@gmail.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Cygnite \FormBuilder ;
13
+
14
+ use Closure ;
15
+ use Cygnite \Proxy \StaticResolver ;
16
+
17
+ /**
18
+ * Form.
19
+ *
20
+ * Build your form on the fly.
21
+ *
22
+ * @author Sanjoy Dey <dey.sanjoy0@gmail.com>
23
+ */
24
+
25
+ class Form extends StaticResolver implements FormInterface
26
+ {
27
+
28
+ private static $ formHolder = array ();
29
+
30
+ public static $ formName ;
31
+
32
+ public static $ formOpen ;
33
+
34
+ public $ attributes = array ();
35
+
36
+ public $ elements = array ();
37
+
38
+ public $ value = array ();
39
+
40
+ public $ element = array ();
41
+
42
+ private static $ object ;
43
+
44
+ private $ validArray = array ('text ' , 'button ' , 'select ' , 'textarea ' );
45
+
46
+ public $ validator ;
47
+
48
+ public $ errorClass = 'error ' ;
49
+
50
+ protected function getInstance (Closure $ callback = null )
51
+ {
52
+ if ($ callback instanceof Closure) {
53
+ return $ callback (new self );
54
+ }
55
+
56
+ return new self ;
57
+ }
58
+
59
+ /**
60
+ * Form open tag
61
+ *
62
+ * @param $formName
63
+ * @param array $attributes
64
+ * @return $this
65
+ */
66
+ public function open ($ formName , $ attributes = array ())
67
+ {
68
+ self ::$ formName = $ formName ;
69
+ self ::$ formHolder [$ formName ] = $ formName ;
70
+
71
+ self ::$ formOpen = true ;
72
+ $ this ->form ($ formName , $ attributes );
73
+
74
+ return $ this ;
75
+ }
76
+
77
+ /*
78
+ * Add form elements
79
+ *
80
+ * @param $key
81
+ * @param $rule set up your validation rule
82
+ * @return $this
83
+ *
84
+ */
85
+ public function addElement ($ type , $ key , $ values = array ())
86
+ {
87
+ $ values ['type ' ] = $ type ;
88
+ $ this ->value [$ key ] = $ values ;
89
+
90
+ return $ this ;
91
+ }
92
+
93
+ public function withHtml ()
94
+ {
95
+ //$this->value[$key] = $values;
96
+ return $ this ;
97
+ }
98
+
99
+ public function setValidation ()
100
+ {
101
+
102
+ }
103
+
104
+ /**
105
+ * @param array $elements
106
+ * @return $this
107
+ */
108
+ public function addElements ($ elements = array ())
109
+ {
110
+ $ this ->value = array_shift ($ elements );
111
+
112
+ return $ this ;
113
+ }
114
+
115
+ /**
116
+ * @return $this
117
+ */
118
+ public function createForm ()
119
+ {
120
+ foreach ($ this ->value as $ key => $ val ) {
121
+
122
+ switch ($ val ['type ' ]) {
123
+ case 'textarea ' :
124
+ unset($ val ['type ' ]);
125
+ $ this ->textarea ($ key , $ val );
126
+ break ;
127
+ case 'select ' :
128
+ unset($ val ['type ' ]);
129
+ $ this ->select ($ key , $ val );
130
+ break ;
131
+ case 'label ' :
132
+ unset($ val ['type ' ]);
133
+ $ this ->label ($ key , $ val );
134
+ break ;
135
+ case 'button ' :
136
+ unset($ val ['type ' ]);
137
+ $ this ->button ($ key , $ val );
138
+ break ;
139
+ default :
140
+ $ this ->input ($ key , $ val );
141
+ break ;
142
+ }
143
+
144
+ if (isset ($ val ['type ' ]) && in_array ($ val ['type ' ], $ this ->validArray )) {
145
+
146
+ if (!in_array ('submit ' , $ val )) {
147
+
148
+ if (is_object ($ this ->validator ) && isset ($ this ->validator ->errors [$ key .'_error ' ])) {
149
+ $ this ->elements [self ::$ formHolder [self ::$ formName ]][$ key .'_error ' ] =
150
+ '<span class=" ' .$ this ->errorClass .'"> ' .$ this ->validator ->errors [$ key .'_error ' ].'</span> ' .PHP_EOL ;
151
+ }
152
+ }
153
+ }
154
+
155
+ }
156
+
157
+ return $ this ;
158
+ }
159
+
160
+ /**
161
+ * @return bool
162
+ */
163
+ public function isValidRequest ()
164
+ {
165
+ return ($ this ->getMethod () == 'post ' ) ? true : false ;
166
+ }
167
+
168
+ /**
169
+ * @param $key
170
+ * @param $val
171
+ */
172
+ private function form ($ key , $ val )
173
+ {
174
+ $ type = null ;
175
+ $ type = strtolower (__FUNCTION__ );
176
+
177
+ $ this ->attributes [self ::$ formHolder [self ::$ formName ]][$ key ] =
178
+ "< $ type name=' " .self ::$ formName ."' " .$ this ->attributes ($ val )."> " .PHP_EOL ;
179
+
180
+ }
181
+
182
+ /**
183
+ * @param $key
184
+ * @param $val
185
+ */
186
+ private function input ($ key , $ val )
187
+ {
188
+ $ type = null ;
189
+ $ type = strtolower (__FUNCTION__ );
190
+
191
+ $ this ->elements [self ::$ formHolder [self ::$ formName ]][$ key ] =
192
+ "< $ type name=' " .$ key ."' " .$ this ->attributes ($ val )." /> " .PHP_EOL ;
193
+
194
+ }
195
+
196
+ /**
197
+ * @param $key
198
+ * @param $val
199
+ */
200
+ private function label ($ key , $ val )
201
+ {
202
+ $ type = null ;
203
+ $ type = strtolower (__FUNCTION__ );
204
+
205
+ $ this ->elements [self ::$ formHolder [self ::$ formName ]][$ key ] =
206
+ "< $ type for=' " .$ key ."' " .$ this ->attributes ($ val )."> " .$ key ."</ $ type> " .PHP_EOL ;
207
+ }
208
+
209
+ /**
210
+ * @param $key
211
+ * @param $val
212
+ */
213
+ private function textarea ($ key , $ val )
214
+ {
215
+ $ value = '' ;
216
+ $ value = $ val ['value ' ];
217
+ $ type = strtolower (__FUNCTION__ );
218
+ unset($ val ['value ' ]);
219
+ $ this ->elements [self ::$ formHolder [self ::$ formName ]][$ key ] =
220
+ "< " .$ type ." name=' " .$ key ."' " .$ this ->attributes ($ val )." > " .$ value ."</ " .$ type ."> " .PHP_EOL ;
221
+ }
222
+
223
+ /**
224
+ * @param $key
225
+ * @param $values
226
+ */
227
+ private function select ($ key , $ values )
228
+ {
229
+ $ select = $ selectValue = '' ;
230
+ $ attributes = array ();
231
+
232
+ $ selectOptions = $ values ['options ' ];
233
+ $ selected = $ values ['selected ' ];
234
+ unset($ values ['options ' ]);
235
+ unset($ values ['selected ' ]);
236
+ $ attributes = $ values ;
237
+
238
+ $ select .= "< " .strtolower (__FUNCTION__ )." name=' " .$ key ."' " .$ this ->attributes ($ attributes )."> " .PHP_EOL ;
239
+
240
+ foreach ($ selectOptions as $ key => $ value ) {
241
+ $ selectValue = ($ selected == $ key ) ? 'selected="selected" ' : '' ;
242
+ $ select .= "<option $ selectValue value=' " .$ key ."'> " .$ value ."</option> " .PHP_EOL ;
243
+ }
244
+
245
+ $ select .= '</ ' .strtolower (__FUNCTION__ ).'> ' .PHP_EOL ;
246
+
247
+ $ this ->elements [self ::$ formHolder [self ::$ formName ]][$ key ] = $ select ;
248
+ }
249
+
250
+ /**
251
+ * @param $key
252
+ * @param $value
253
+ */
254
+ public function __set ($ key , $ value )
255
+ {
256
+ $ this ->attributes [$ key ] = $ value ;
257
+ }
258
+
259
+ /**
260
+ * @param $key
261
+ * @return null
262
+ */
263
+ public function __get ($ key )
264
+ {
265
+ return isset ($ this ->attributes [$ key ]) ? $ this ->attributes [$ key ] : null ;
266
+ }
267
+
268
+ /**
269
+ * @return mixed
270
+ */
271
+ public function getForm ()
272
+ {
273
+ $ elementString = "" ;
274
+
275
+ if (isset ($ this ->attributes [self ::$ formHolder [self ::$ formName ]])) {
276
+ $ elementString .= $ this ->attributes [self ::$ formHolder [self ::$ formName ]][self ::$ formName ];
277
+ }
278
+
279
+ foreach ($ this ->elements [self ::$ formHolder [self ::$ formName ]] as $ key => $ val ) {
280
+ $ elementString .= $ val ;
281
+
282
+ }
283
+
284
+ $ close = "" ;
285
+ $ close = self ::$ formHolder [self ::$ formName ].'_close ' ;
286
+
287
+ if (isset ($ this ->attributes [$ close ])) {
288
+ $ elementString .= $ this ->attributes [$ close ];
289
+ }
290
+
291
+
292
+ $ this ->element [self ::$ formHolder [self ::$ formName ]] = $ elementString ;
293
+
294
+ return $ this ->element [self ::$ formHolder [self ::$ formName ]];
295
+ }
296
+
297
+ //Error occured while using this method
298
+ //Have to work on this.
299
+ public function __toString ()
300
+ {
301
+ return $ this ->element [@self ::$ formHolder [self ::$ formName ]];
302
+ }
303
+
304
+ /**
305
+ * @param $attributes
306
+ * @return string
307
+ */
308
+ protected function attributes ($ attributes )
309
+ {
310
+ $ element_str = "" ;
311
+
312
+ foreach ($ attributes as $ key => $ value ) {
313
+ $ element_str .= "{$ key }=' {$ value }' " ;
314
+ }
315
+
316
+ return $ element_str ;
317
+ }
318
+
319
+ public function close ()
320
+ {
321
+ if (self ::$ formOpen ) {
322
+ $ close = trim (self ::$ formHolder [self ::$ formName ].'_close ' );
323
+ $ this ->{$ close } = "</form> " .PHP_EOL ;
324
+ }
325
+
326
+ return $ this ;
327
+ }
328
+
329
+ public function flush ()
330
+ {
331
+ $ this ->__destruct ();
332
+ }
333
+
334
+ public function __destruct ()
335
+ {
336
+ unset($ this ->elements );
337
+ unset($ this ->element );
338
+ }
339
+ }
0 commit comments