Passed
Push — master ( fffd2d...cc8627 )
by Jean-Christophe
03:41
created
Ajax/semantic/widgets/base/FieldAsTrait.php 1 patch
Spacing   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
  */
37 37
 trait FieldAsTrait {
38 38
 
39
-	abstract protected function _getFieldIdentifier($prefix, $name = "");
39
+	abstract protected function _getFieldIdentifier($prefix, $name="");
40 40
 
41 41
 	abstract public function setValueFunction($index, $callback);
42 42
 
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 
45 45
 	abstract protected function _getFieldCaption($index);
46 46
 
47
-	abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement = NULL, $parameters = NULL);
47
+	abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement=NULL, $parameters=NULL);
48 48
 
49 49
 	private $_speProperties;
50 50
 
@@ -53,9 +53,9 @@  discard block
 block discarded – undo
53 53
 	 * @param HtmlFormField $element
54 54
 	 * @param array $attributes
55 55
 	 */
56
-	protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance = null) {
56
+	protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance=null) {
57 57
 		if (isset($attributes["jsCallback"])) {
58
-			$callback = $attributes["jsCallback"];
58
+			$callback=$attributes["jsCallback"];
59 59
 			if (\is_callable($callback)) {
60 60
 				$callback($element, $instance, $index, InstanceViewer::$index);
61 61
 			}
@@ -66,15 +66,15 @@  discard block
 block discarded – undo
66 66
 		$element->fromArray($attributes);
67 67
 	}
68 68
 
69
-	private function _getLabelField($caption, $icon = NULL) {
70
-		$label = new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon);
69
+	private function _getLabelField($caption, $icon=NULL) {
70
+		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon);
71 71
 		return $label;
72 72
 	}
73 73
 
74 74
 	protected function _addRules(HtmlFormField $element, &$attributes) {
75 75
 		if (isset($attributes["rules"])) {
76
-			$this->_hasRules = true;
77
-			$rules = $attributes["rules"];
76
+			$this->_hasRules=true;
77
+			$rules=$attributes["rules"];
78 78
 			if (\is_array($rules)) {
79 79
 				$element->addRules($rules);
80 80
 			} else {
@@ -90,24 +90,24 @@  discard block
 block discarded – undo
90 90
 		return $field;
91 91
 	}
92 92
 
93
-	protected function _fieldAs($elementCallback, &$index, $attributes = NULL, $prefix = null) {
94
-		$this->setValueFunction($index, function ($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) {
95
-			$caption = $this->_getFieldCaption($index);
96
-			$name = $this->_getFieldName($index);
97
-			$id = $this->_getFieldIdentifier($prefix, $name);
93
+	protected function _fieldAs($elementCallback, &$index, $attributes=NULL, $prefix=null) {
94
+		$this->setValueFunction($index, function($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) {
95
+			$caption=$this->_getFieldCaption($index);
96
+			$name=$this->_getFieldName($index);
97
+			$id=$this->_getFieldIdentifier($prefix, $name);
98 98
 			if (isset($attributes["name"])) {
99
-				$name = $attributes["name"];
99
+				$name=$attributes["name"];
100 100
 				unset($attributes["name"]);
101 101
 			}
102
-			$element = $elementCallback($id, $name, $value, $caption);
102
+			$element=$elementCallback($id, $name, $value, $caption);
103 103
 			if (isset($this->_speProperties[$index])) {
104 104
 				$attributes ??= [];
105
-				$attributes += $this->_speProperties[$index];
105
+				$attributes+=$this->_speProperties[$index];
106 106
 			}
107 107
 			if (\is_array($attributes)) {
108 108
 				$this->_applyAttributes($element, $attributes, $index, $instance);
109 109
 			}
110
-			$element->setDisabled(! $this->_edition);
110
+			$element->setDisabled(!$this->_edition);
111 111
 			return $element;
112 112
 		});
113 113
 		return $this;
@@ -123,48 +123,48 @@  discard block
 block discarded – undo
123 123
 	 */
124 124
 	public function setPropertyValues($property, $indexValues) {
125 125
 		foreach ($indexValues as $index => $value) {
126
-			$ind = $this->_getIndex($index);
127
-			$this->_speProperties[$ind][$property] = $value;
126
+			$ind=$this->_getIndex($index);
127
+			$this->_speProperties[$ind][$property]=$value;
128 128
 		}
129 129
 	}
130 130
 
131
-	public function fieldAsProgress($index, $label = NULL, $attributes = array()) {
132
-		$this->setValueFunction($index, function ($value) use ($label, $attributes) {
133
-			$pb = new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes);
131
+	public function fieldAsProgress($index, $label=NULL, $attributes=array()) {
132
+		$this->setValueFunction($index, function($value) use ($label, $attributes) {
133
+			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes);
134 134
 			return $pb;
135 135
 		});
136 136
 		return $this;
137 137
 	}
138 138
 
139
-	public function fieldAsRating($index, $max = 5, $icon = "") {
140
-		$this->setValueFunction($index, function ($value) use ($max, $icon) {
141
-			$rating = new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon);
139
+	public function fieldAsRating($index, $max=5, $icon="") {
140
+		$this->setValueFunction($index, function($value) use ($max, $icon) {
141
+			$rating=new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon);
142 142
 			return $rating;
143 143
 		});
144 144
 		return $this;
145 145
 	}
146 146
 
147
-	public function fieldAsLabel($index, $icon = NULL, $attributes = NULL) {
148
-		return $this->_fieldAs(function ($id, $name, $value) use ($icon) {
149
-			$lbl = new HtmlLabel($id, $value);
147
+	public function fieldAsLabel($index, $icon=NULL, $attributes=NULL) {
148
+		return $this->_fieldAs(function($id, $name, $value) use ($icon) {
149
+			$lbl=new HtmlLabel($id, $value);
150 150
 			if (isset($icon))
151 151
 				$lbl->addIcon($icon);
152 152
 			return $lbl;
153 153
 		}, $index, $attributes, "label");
154 154
 	}
155 155
 
156
-	public function fieldAsHeader($index, $niveau = 1, $icon = NULL, $attributes = NULL) {
157
-		return $this->_fieldAs(function ($id, $name, $value) use ($niveau, $icon) {
158
-			$header = new HtmlHeader($id, $niveau, $value);
156
+	public function fieldAsHeader($index, $niveau=1, $icon=NULL, $attributes=NULL) {
157
+		return $this->_fieldAs(function($id, $name, $value) use ($niveau, $icon) {
158
+			$header=new HtmlHeader($id, $niveau, $value);
159 159
 			if (isset($icon))
160 160
 				$header->asIcon($icon, $value);
161 161
 			return $header;
162 162
 		}, $index, $attributes, "header");
163 163
 	}
164 164
 
165
-	public function fieldAsImage($index, $size = Size::MINI, $circular = false) {
166
-		$this->setValueFunction($index, function ($img) use ($size, $circular) {
167
-			$image = new HtmlImage($this->_getFieldIdentifier("image"), $img);
165
+	public function fieldAsImage($index, $size=Size::MINI, $circular=false) {
166
+		$this->setValueFunction($index, function($img) use ($size, $circular) {
167
+			$image=new HtmlImage($this->_getFieldIdentifier("image"), $img);
168 168
 			$image->setSize($size);
169 169
 			if ($circular)
170 170
 				$image->setCircular();
@@ -174,65 +174,65 @@  discard block
 block discarded – undo
174 174
 	}
175 175
 
176 176
 	public function fieldAsFlag($index) {
177
-		$this->setValueFunction($index, function ($flag) {
178
-			$flag = new HtmlFlag($this->_getFieldIdentifier("flag"), $flag);
177
+		$this->setValueFunction($index, function($flag) {
178
+			$flag=new HtmlFlag($this->_getFieldIdentifier("flag"), $flag);
179 179
 			return $flag;
180 180
 		});
181 181
 		return $this;
182 182
 	}
183 183
 
184 184
 	public function fieldAsIcon($index) {
185
-		$this->setValueFunction($index, function ($icon) {
186
-			$icon = new HtmlIcon($this->_getFieldIdentifier("icon"), $icon);
185
+		$this->setValueFunction($index, function($icon) {
186
+			$icon=new HtmlIcon($this->_getFieldIdentifier("icon"), $icon);
187 187
 			return $icon;
188 188
 		});
189 189
 		return $this;
190 190
 	}
191 191
 
192
-	public function fieldAsAvatar($index, $attributes = NULL) {
193
-		return $this->_fieldAs(function ($id, $name, $value) {
194
-			$img = new HtmlImage($id, $value);
192
+	public function fieldAsAvatar($index, $attributes=NULL) {
193
+		return $this->_fieldAs(function($id, $name, $value) {
194
+			$img=new HtmlImage($id, $value);
195 195
 			$img->asAvatar();
196 196
 			return $img;
197 197
 		}, $index, $attributes, "avatar");
198 198
 	}
199 199
 
200
-	public function fieldAsRadio($index, $attributes = NULL) {
201
-		return $this->_fieldAs(function ($id, $name, $value) use ($attributes) {
202
-			$input = new HtmlFormRadio($id, $name, $value, $value);
200
+	public function fieldAsRadio($index, $attributes=NULL) {
201
+		return $this->_fieldAs(function($id, $name, $value) use ($attributes) {
202
+			$input=new HtmlFormRadio($id, $name, $value, $value);
203 203
 			return $this->_prepareFormFields($input, $name, $attributes);
204 204
 		}, $index, $attributes, "radio");
205 205
 	}
206 206
 
207
-	public function fieldAsRadios($index, $elements = [], $attributes = NULL) {
208
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements) {
207
+	public function fieldAsRadios($index, $elements=[], $attributes=NULL) {
208
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($elements) {
209 209
 			return HtmlFormFields::radios($name, $elements, $caption, $value);
210 210
 		}, $index, $attributes, "radios");
211 211
 	}
212 212
 
213
-	public function fieldAsList($index, $classNames = "", $attributes = NULL) {
214
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($classNames) {
215
-			$result = new HtmlList($name, $value);
213
+	public function fieldAsList($index, $classNames="", $attributes=NULL) {
214
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($classNames) {
215
+			$result=new HtmlList($name, $value);
216 216
 			$result->addClass($classNames);
217 217
 			return $result;
218 218
 		}, $index, $attributes, "list");
219 219
 	}
220 220
 
221
-	public function fieldAsInput($index, $attributes = NULL) {
222
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
223
-			$input = new HtmlFormInput($id, $caption, "text", $value);
221
+	public function fieldAsInput($index, $attributes=NULL) {
222
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes) {
223
+			$input=new HtmlFormInput($id, $caption, "text", $value);
224 224
 			return $this->_prepareFormFields($input, $name, $attributes);
225 225
 		}, $index, $attributes, "input");
226 226
 	}
227 227
 
228
-	public function fieldAsLabeledInput($index, $attributes = NULL) {
229
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
230
-			$input = new HtmlFormInput($id, '', 'text', $value, $caption);
231
-			$required = '';
228
+	public function fieldAsLabeledInput($index, $attributes=NULL) {
229
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes) {
230
+			$input=new HtmlFormInput($id, '', 'text', $value, $caption);
231
+			$required='';
232 232
 			if (isset($attributes['rules'])) {
233
-				$rules = json_encode($attributes['rules']);
234
-				if (strpos($rules, 'empty') !== false) {
235
-					$required = 'required';
233
+				$rules=json_encode($attributes['rules']);
234
+				if (strpos($rules, 'empty')!==false) {
235
+					$required='required';
236 236
 				}
237 237
 			}
238 238
 			$input->getField()
@@ -243,80 +243,80 @@  discard block
 block discarded – undo
243 243
 		}, $index, $attributes, 'input');
244 244
 	}
245 245
 
246
-	public function fieldAsDataList($index, ?array $items = [], $attributes = NULL) {
247
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $items) {
248
-			$input = new HtmlFormInput($id, $caption, "text", $value);
246
+	public function fieldAsDataList($index, ?array $items=[], $attributes=NULL) {
247
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes, $items) {
248
+			$input=new HtmlFormInput($id, $caption, "text", $value);
249 249
 			$input->getField()
250 250
 				->addDataList($items);
251 251
 			return $this->_prepareFormFields($input, $name, $attributes);
252 252
 		}, $index, $attributes, "input");
253 253
 	}
254 254
 
255
-	public function fieldAsFile($index, $attributes = NULL) {
255
+	public function fieldAsFile($index, $attributes=NULL) {
256 256
 		if (isset($this->_form)) {
257 257
 			$this->_form->setProperty('enctype', 'multipart/form-data');
258 258
 		}
259
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
260
-			$input = new HtmlFormInput($id, $caption);
259
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes) {
260
+			$input=new HtmlFormInput($id, $caption);
261 261
 			$input->asFile();
262 262
 			return $this->_prepareFormFields($input, $name, $attributes);
263 263
 		}, $index, $attributes, "input");
264 264
 	}
265 265
 
266
-	public function fieldAsTextarea($index, $attributes = NULL) {
267
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
268
-			$textarea = new HtmlFormTextarea($id, $caption, $value);
266
+	public function fieldAsTextarea($index, $attributes=NULL) {
267
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes) {
268
+			$textarea=new HtmlFormTextarea($id, $caption, $value);
269 269
 			return $this->_prepareFormFields($textarea, $name, $attributes);
270 270
 		}, $index, $attributes, "textarea");
271 271
 	}
272 272
 
273
-	public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) {
274
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) {
275
-			$div = new HtmlSemDoubleElement($id, $tagName, $baseClass);
273
+	public function fieldAsElement($index, $tagName="div", $baseClass="", $attributes=NULL) {
274
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) {
275
+			$div=new HtmlSemDoubleElement($id, $tagName, $baseClass);
276 276
 			$div->setContent(\htmlentities($value));
277
-			$textarea = new HtmlFormField("field-" . $id, $div, $caption);
277
+			$textarea=new HtmlFormField("field-".$id, $div, $caption);
278 278
 			return $this->_prepareFormFields($textarea, $name, $attributes);
279 279
 		}, $index, $attributes, "element");
280 280
 	}
281 281
 
282
-	public function fieldAsHidden($index, $attributes = NULL) {
283
-		if (! \is_array($attributes)) {
284
-			$attributes = [];
282
+	public function fieldAsHidden($index, $attributes=NULL) {
283
+		if (!\is_array($attributes)) {
284
+			$attributes=[];
285 285
 		}
286
-		$attributes["inputType"] = "hidden";
287
-		$attributes["style"] = "display:none;";
286
+		$attributes["inputType"]="hidden";
287
+		$attributes["style"]="display:none;";
288 288
 		return $this->fieldAsInput($index, $attributes);
289 289
 	}
290 290
 
291
-	public function fieldAsCheckbox($index, $attributes = NULL) {
292
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
293
-			if ($caption === null || $caption === "")
294
-				$caption = "";
295
-			$input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier());
291
+	public function fieldAsCheckbox($index, $attributes=NULL) {
292
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($attributes) {
293
+			if ($caption===null || $caption==="")
294
+				$caption="";
295
+			$input=new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier());
296 296
 			$input->setChecked(JString::isBooleanTrue($value));
297 297
 			return $this->_prepareFormFields($input, $name, $attributes);
298 298
 		}, $index, $attributes, "ck");
299 299
 	}
300 300
 
301
-	public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) {
302
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) {
303
-			$dd = new HtmlFormDropdown($id, $elements, $caption, $value ?? '');
301
+	public function fieldAsDropDown($index, $elements=[], $multiple=false, $attributes=NULL) {
302
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($elements, $multiple, $attributes) {
303
+			$dd=new HtmlFormDropdown($id, $elements, $caption, $value ?? '');
304 304
 			$dd->asSelect($name, $multiple);
305 305
 			return $this->_prepareFormFields($dd, $name, $attributes);
306 306
 		}, $index, $attributes, "dd");
307 307
 	}
308 308
 
309
-	public function fieldAsMessage($index, $attributes = NULL) {
310
-		return $this->_fieldAs(function ($id, $name, $value, $caption) {
311
-			$mess = new HtmlMessage("message-" . $id, $caption);
309
+	public function fieldAsMessage($index, $attributes=NULL) {
310
+		return $this->_fieldAs(function($id, $name, $value, $caption) {
311
+			$mess=new HtmlMessage("message-".$id, $caption);
312 312
 			$mess->addHeader($value);
313 313
 			return $mess;
314 314
 		}, $index, $attributes, "message");
315 315
 	}
316 316
 
317
-	public function fieldAsLink($index, $attributes = NULL) {
318
-		return $this->_fieldAs(function ($id, $name, $value, $caption) {
319
-			$lnk = new HtmlLink("message-" . $id, "#", $caption);
317
+	public function fieldAsLink($index, $attributes=NULL) {
318
+		return $this->_fieldAs(function($id, $name, $value, $caption) {
319
+			$lnk=new HtmlLink("message-".$id, "#", $caption);
320 320
 			return $lnk;
321 321
 		}, $index, $attributes, "link");
322 322
 	}
@@ -328,28 +328,28 @@  discard block
 block discarded – undo
328 328
 	 *        	an array or associative array $type=>$attributes
329 329
 	 */
330 330
 	public function fieldsAs(array $types) {
331
-		$i = 0;
331
+		$i=0;
332 332
 		if (JArray::isAssociative($types)) {
333 333
 			foreach ($types as $type => $attributes) {
334 334
 				if (\is_int($type))
335
-					$this->fieldAs($i ++, $attributes, []);
335
+					$this->fieldAs($i++, $attributes, []);
336 336
 				else {
337
-					$type = preg_replace('/\d/', '', $type);
338
-					$this->fieldAs($i ++, $type, $attributes);
337
+					$type=preg_replace('/\d/', '', $type);
338
+					$this->fieldAs($i++, $type, $attributes);
339 339
 				}
340 340
 			}
341 341
 		} else {
342 342
 			foreach ($types as $type) {
343
-				$this->fieldAs($i ++, $type);
343
+				$this->fieldAs($i++, $type);
344 344
 			}
345 345
 		}
346 346
 	}
347 347
 
348
-	public function fieldAs($index, $type, $attributes = NULL) {
349
-		$method = "fieldAs" . \ucfirst($type);
348
+	public function fieldAs($index, $type, $attributes=NULL) {
349
+		$method="fieldAs".\ucfirst($type);
350 350
 		if (\method_exists($this, $method)) {
351
-			if (! \is_array($attributes)) {
352
-				$attributes = [
351
+			if (!\is_array($attributes)) {
352
+				$attributes=[
353 353
 					$index
354 354
 				];
355 355
 			} else {
@@ -362,17 +362,17 @@  discard block
 block discarded – undo
362 362
 		}
363 363
 	}
364 364
 
365
-	public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) {
366
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) {
367
-			$button = new HtmlButton($id, $caption, $cssStyle);
365
+	public function fieldAsSubmit($index, $cssStyle=NULL, $url=NULL, $responseElement=NULL, $attributes=NULL) {
366
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) {
367
+			$button=new HtmlButton($id, $caption, $cssStyle);
368 368
 			$this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]);
369 369
 			return $button;
370 370
 		}, $index, $attributes, "submit");
371 371
 	}
372 372
 
373
-	public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) {
374
-		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($cssStyle) {
375
-			$button = new HtmlButton($id, $value, $cssStyle);
373
+	public function fieldAsButton($index, $cssStyle=NULL, $attributes=NULL) {
374
+		return $this->_fieldAs(function($id, $name, $value, $caption) use ($cssStyle) {
375
+			$button=new HtmlButton($id, $value, $cssStyle);
376 376
 			return $button;
377 377
 		}, $index, $attributes, "button");
378 378
 	}
Please login to merge, or discard this patch.