@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | */ |
40 | 40 | public static function init() |
41 | 41 | { |
42 | - if(is_null(self::$instance)) { |
|
42 | + if (is_null(self::$instance)) { |
|
43 | 43 | self::$instance = new Form(); |
44 | 44 | } |
45 | 45 | return self::$instance; |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * @param string $token CSRF token |
52 | 52 | * @param string $action Url action |
53 | 53 | */ |
54 | - public function setup($datas=[], $token='', $action='') |
|
54 | + public function setup($datas = [], $token = '', $action = '') |
|
55 | 55 | { |
56 | 56 | $this->datas = $datas; |
57 | 57 | $this->token = $token; |
@@ -66,11 +66,11 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public function open($method = 'POST', $action = '') |
68 | 68 | { |
69 | - if(empty($action)) { |
|
69 | + if (empty($action)) { |
|
70 | 70 | $action = $this->action; |
71 | 71 | } |
72 | 72 | $html = '<form action="'.$action.'" method="'.$method.'"'.$this->attributesToHtml().'>'; |
73 | - if(!empty($this->token)) { |
|
73 | + if (!empty($this->token)) { |
|
74 | 74 | $html .= $this->addAttr('value', $this->token)->input('token', 'hidden'); |
75 | 75 | } |
76 | 76 | return $html; |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | public function label($value, $required = null) |
86 | 86 | { |
87 | 87 | $labelValue = ucfirst($value); |
88 | - if($required) { |
|
88 | + if ($required) { |
|
89 | 89 | $labelValue .= ' <span class="required">*</span>'; |
90 | 90 | } |
91 | 91 | return $this->addTag('label', $labelValue); |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | public function select($fieldName, $options = []) |
101 | 101 | { |
102 | 102 | $fieldOptions = ''; |
103 | - if(!empty($options)) { |
|
103 | + if (!empty($options)) { |
|
104 | 104 | $fieldValue = $this->get($fieldName); |
105 | - foreach($options as $value => $text) { |
|
105 | + foreach ($options as $value => $text) { |
|
106 | 106 | $selected = $fieldValue != $value ?: ' selected="selected"'; |
107 | 107 | $fieldOptions .= '<option value="'.$value.'"'.$selected.'>'.ucfirst($text).'</option>'; |
108 | 108 | } |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public function button($value = 'Submit', $iconClass = null) |
131 | 131 | { |
132 | - if($iconClass){ |
|
132 | + if ($iconClass) { |
|
133 | 133 | $value .= ' <i class="'.$iconClass.'"></i>'; |
134 | 134 | } |
135 | 135 | return $this->addTag('button', $value); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | public function addAttr($key, $value = null) |
155 | 155 | { |
156 | - if(is_array($key)) |
|
156 | + if (is_array($key)) |
|
157 | 157 | $this->attributes += $key; |
158 | 158 | else |
159 | 159 | $this->attributes[$key] = $value; |
@@ -168,9 +168,9 @@ discard block |
||
168 | 168 | */ |
169 | 169 | public function get($key = '', $source = 'datas') |
170 | 170 | { |
171 | - if(property_exists($this, $source)) { |
|
171 | + if (property_exists($this, $source)) { |
|
172 | 172 | $property = $this->$source; |
173 | - if(!empty($property) && array_key_exists($key, $property)) |
|
173 | + if (!empty($property) && array_key_exists($key, $property)) |
|
174 | 174 | return $property[$key]; |
175 | 175 | } |
176 | 176 | return null; |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | */ |
185 | 185 | protected function input($fieldName, $type) { |
186 | 186 | $value = $this->get($fieldName); |
187 | - if(!is_null($value)) { |
|
187 | + if (!is_null($value)) { |
|
188 | 188 | $this->attributes['value'] = $this->get($fieldName); |
189 | 189 | } |
190 | 190 | return '<input type="'.$type.'" name="'.$fieldName.'"'.$this->attributesToHtml().'>'; |
@@ -201,13 +201,13 @@ discard block |
||
201 | 201 | { |
202 | 202 | $fieldValue = $this->get($fieldName); |
203 | 203 | $value = $this->get('value', 'attrs'); |
204 | - if($fieldValue && $value){ |
|
204 | + if ($fieldValue && $value) { |
|
205 | 205 | $checked = is_array($fieldValue) ? array_key_exists($value, array_flip($fieldValue)) : $fieldValue == $value; |
206 | - if($checked){ |
|
206 | + if ($checked) { |
|
207 | 207 | $this->attributes['checked'] = 'checked'; |
208 | 208 | } |
209 | 209 | } |
210 | - return $this->input($fieldName, $type) . " " . $fieldLabel; |
|
210 | + return $this->input($fieldName, $type)." ".$fieldLabel; |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -229,8 +229,8 @@ discard block |
||
229 | 229 | */ |
230 | 230 | private function attributesToHtml($html = '') |
231 | 231 | { |
232 | - if(!empty($this->attributes)) { |
|
233 | - foreach($this->attributes as $key => $value) { |
|
232 | + if (!empty($this->attributes)) { |
|
233 | + foreach ($this->attributes as $key => $value) { |
|
234 | 234 | $value = is_array($value) ? implode(' ', $value) : $value; |
235 | 235 | $html .= is_numeric($key) ? $value.' ' : $key.'="'.$value.'" '; |
236 | 236 | } |
@@ -248,18 +248,18 @@ discard block |
||
248 | 248 | public function __call($tagName, $methodParams) |
249 | 249 | { |
250 | 250 | $method = null; |
251 | - if(in_array($tagName, ['text','password','date','time','file','hidden','reset'])) { |
|
251 | + if (in_array($tagName, ['text', 'password', 'date', 'time', 'file', 'hidden', 'reset'])) { |
|
252 | 252 | array_push($methodParams, $tagName); |
253 | 253 | $method = "input"; |
254 | 254 | } |
255 | - elseif(in_array($tagName, ['checkbox','radio'])) { |
|
255 | + elseif (in_array($tagName, ['checkbox', 'radio'])) { |
|
256 | 256 | array_push($methodParams, $tagName); |
257 | 257 | $method = "box"; |
258 | 258 | } |
259 | - elseif($tagName == 'close') { |
|
259 | + elseif ($tagName == 'close') { |
|
260 | 260 | return '</form>'; |
261 | 261 | } |
262 | - elseif($tagName == 'reset') { |
|
262 | + elseif ($tagName == 'reset') { |
|
263 | 263 | $this->datas = []; |
264 | 264 | } |
265 | 265 | $handler = [$this, $method]; |
@@ -153,10 +153,11 @@ discard block |
||
153 | 153 | */ |
154 | 154 | public function addAttr($key, $value = null) |
155 | 155 | { |
156 | - if(is_array($key)) |
|
157 | - $this->attributes += $key; |
|
158 | - else |
|
159 | - $this->attributes[$key] = $value; |
|
156 | + if(is_array($key)) { |
|
157 | + $this->attributes += $key; |
|
158 | + } else { |
|
159 | + $this->attributes[$key] = $value; |
|
160 | + } |
|
160 | 161 | return $this; |
161 | 162 | } |
162 | 163 | |
@@ -170,8 +171,9 @@ discard block |
||
170 | 171 | { |
171 | 172 | if(property_exists($this, $source)) { |
172 | 173 | $property = $this->$source; |
173 | - if(!empty($property) && array_key_exists($key, $property)) |
|
174 | - return $property[$key]; |
|
174 | + if(!empty($property) && array_key_exists($key, $property)) { |
|
175 | + return $property[$key]; |
|
176 | + } |
|
175 | 177 | } |
176 | 178 | return null; |
177 | 179 | } |
@@ -251,15 +253,12 @@ discard block |
||
251 | 253 | if(in_array($tagName, ['text','password','date','time','file','hidden','reset'])) { |
252 | 254 | array_push($methodParams, $tagName); |
253 | 255 | $method = "input"; |
254 | - } |
|
255 | - elseif(in_array($tagName, ['checkbox','radio'])) { |
|
256 | + } elseif(in_array($tagName, ['checkbox','radio'])) { |
|
256 | 257 | array_push($methodParams, $tagName); |
257 | 258 | $method = "box"; |
258 | - } |
|
259 | - elseif($tagName == 'close') { |
|
259 | + } elseif($tagName == 'close') { |
|
260 | 260 | return '</form>'; |
261 | - } |
|
262 | - elseif($tagName == 'reset') { |
|
261 | + } elseif($tagName == 'reset') { |
|
263 | 262 | $this->datas = []; |
264 | 263 | } |
265 | 264 | $handler = [$this, $method]; |
@@ -70,13 +70,13 @@ discard block |
||
70 | 70 | <label>Gender</label> |
71 | 71 | <label> |
72 | 72 | <?php |
73 | - echo $form->addAttr('value','F') |
|
73 | + echo $form->addAttr('value', 'F') |
|
74 | 74 | ->radio('user_gender', 'Female'); |
75 | 75 | ?> |
76 | 76 | </label> |
77 | 77 | <label> |
78 | 78 | <?php |
79 | - echo $form->addAttr('value','M') |
|
79 | + echo $form->addAttr('value', 'M') |
|
80 | 80 | ->radio('user_gender', 'Male'); |
81 | 81 | ?> |
82 | 82 | </label> |
@@ -85,19 +85,19 @@ discard block |
||
85 | 85 | <label>Skills</label> |
86 | 86 | <label> |
87 | 87 | <?php |
88 | - echo $form->addAttr('value','css') |
|
88 | + echo $form->addAttr('value', 'css') |
|
89 | 89 | ->checkbox('user_skills[]', 'CSS'); |
90 | 90 | ?> |
91 | 91 | </label> |
92 | 92 | <label> |
93 | 93 | <?php |
94 | - echo $form->addAttr('value','php') |
|
94 | + echo $form->addAttr('value', 'php') |
|
95 | 95 | ->checkbox('user_skills[]', 'PHP'); |
96 | 96 | ?> |
97 | 97 | </label> |
98 | 98 | <label> |
99 | 99 | <?php |
100 | - echo $form->addAttr('value','mysql') |
|
100 | + echo $form->addAttr('value', 'mysql') |
|
101 | 101 | ->checkbox('user_skills[]', 'MySQL'); |
102 | 102 | ?> |
103 | 103 | </label> |