| Total Complexity | 57 |
| Total Lines | 363 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 2 | Features | 1 |
Complex classes like FieldAsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FieldAsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | trait FieldAsTrait { |
||
| 39 | |||
| 40 | abstract protected function _getFieldIdentifier($prefix, $name = ""); |
||
| 41 | |||
| 42 | abstract public function setValueFunction($index, $callback); |
||
| 43 | |||
| 44 | abstract protected function _getFieldName($index); |
||
| 45 | |||
| 46 | abstract protected function _getFieldCaption($index); |
||
| 47 | |||
| 48 | abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement = NULL, $parameters = NULL); |
||
| 49 | |||
| 50 | private $_speProperties; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @param HtmlFormField $element |
||
| 55 | * @param array $attributes |
||
| 56 | */ |
||
| 57 | protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance = null) { |
||
| 58 | if (isset($attributes["jsCallback"])) { |
||
| 59 | $callback = $attributes["jsCallback"]; |
||
| 60 | if (\is_callable($callback)) { |
||
| 61 | $callback($element, $instance, $index, InstanceViewer::$index); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | unset($attributes["rules"]); |
||
| 65 | unset($attributes["ajax"]); |
||
| 66 | unset($attributes["visibleHover"]); |
||
| 67 | $element->fromArray($attributes); |
||
| 68 | } |
||
| 69 | |||
| 70 | private function _getLabelField($caption, $icon = NULL) { |
||
| 71 | $label = new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon); |
||
| 72 | return $label; |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function _addRules(HtmlFormField $element, &$attributes) { |
||
| 76 | if (isset($attributes["rules"])) { |
||
| 77 | $this->_hasRules = true; |
||
| 78 | $rules = $attributes["rules"]; |
||
| 79 | if (\is_array($rules)) { |
||
| 80 | $element->addRules($rules); |
||
| 81 | } else { |
||
| 82 | $element->addRule($rules); |
||
| 83 | } |
||
| 84 | unset($attributes["rules"]); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | protected function _prepareFormFields(HtmlFormField &$field, $name, &$attributes) { |
||
| 89 | $field->setName($name); |
||
| 90 | $this->_addRules($field, $attributes); |
||
| 91 | return $field; |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function _fieldAs($elementCallback, &$index, $attributes = NULL, $prefix = null) { |
||
| 95 | $this->setValueFunction($index, function ($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) { |
||
| 96 | $caption = $this->_getFieldCaption($index); |
||
| 97 | $name = $this->_getFieldName($index); |
||
| 98 | $id = $this->_getFieldIdentifier($prefix, $name); |
||
| 99 | if (isset($attributes["name"])) { |
||
| 100 | $name = $attributes["name"]; |
||
| 101 | unset($attributes["name"]); |
||
| 102 | } |
||
| 103 | $element = $elementCallback($id, $name, $value, $caption); |
||
| 104 | if (isset($this->_speProperties[$index])) { |
||
| 105 | $attributes ??= []; |
||
| 106 | $attributes += $this->_speProperties[$index]; |
||
| 107 | } |
||
| 108 | if (\is_array($attributes)) { |
||
| 109 | $this->_applyAttributes($element, $attributes, $index, $instance); |
||
| 110 | } |
||
| 111 | $element->setDisabled(! $this->_edition); |
||
| 112 | return $element; |
||
| 113 | }); |
||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Defines the values for the fields for a property (or html attribute). |
||
| 119 | * |
||
| 120 | * @param int|string $property |
||
| 121 | * the property to update |
||
| 122 | * @param array $indexValues |
||
| 123 | * array of field=>value |
||
| 124 | */ |
||
| 125 | public function setPropertyValues($property, $indexValues) { |
||
| 126 | foreach ($indexValues as $index => $value) { |
||
| 127 | $ind = $this->_getIndex($index); |
||
|
|
|||
| 128 | $this->_speProperties[$ind][$property] = $value; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public function fieldAsProgress($index, $label = NULL, $attributes = array()) { |
||
| 133 | $this->setValueFunction($index, function ($value) use ($label, $attributes) { |
||
| 134 | $pb = new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes); |
||
| 135 | return $pb; |
||
| 136 | }); |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function fieldAsRating($index, $max = 5, $icon = "") { |
||
| 141 | $this->setValueFunction($index, function ($value) use ($max, $icon) { |
||
| 142 | $rating = new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon); |
||
| 143 | return $rating; |
||
| 144 | }); |
||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function fieldAsLabel($index, $icon = NULL, $attributes = NULL) { |
||
| 149 | return $this->_fieldAs(function ($id, $name, $value) use ($icon) { |
||
| 150 | $lbl = new HtmlLabel($id, $value); |
||
| 151 | if (isset($icon)) |
||
| 152 | $lbl->addIcon($icon); |
||
| 153 | return $lbl; |
||
| 154 | }, $index, $attributes, "label"); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function fieldAsHeader($index, $niveau = 1, $icon = NULL, $attributes = NULL) { |
||
| 158 | return $this->_fieldAs(function ($id, $name, $value) use ($niveau, $icon) { |
||
| 159 | $header = new HtmlHeader($id, $niveau, $value); |
||
| 160 | if (isset($icon)) |
||
| 161 | $header->asIcon($icon, $value); |
||
| 162 | return $header; |
||
| 163 | }, $index, $attributes, "header"); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function fieldAsImage($index, $size = Size::MINI, $circular = false) { |
||
| 167 | $this->setValueFunction($index, function ($img) use ($size, $circular) { |
||
| 168 | $image = new HtmlImage($this->_getFieldIdentifier("image"), $img); |
||
| 169 | $image->setSize($size); |
||
| 170 | if ($circular) |
||
| 171 | $image->setCircular(); |
||
| 172 | return $image; |
||
| 173 | }); |
||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function fieldAsFlag($index) { |
||
| 178 | $this->setValueFunction($index, function ($flag) { |
||
| 179 | $flag = new HtmlFlag($this->_getFieldIdentifier("flag"), $flag); |
||
| 180 | return $flag; |
||
| 181 | }); |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function fieldAsIcon($index) { |
||
| 186 | $this->setValueFunction($index, function ($icon) { |
||
| 187 | $icon = new HtmlIcon($this->_getFieldIdentifier("icon"), $icon); |
||
| 188 | return $icon; |
||
| 189 | }); |
||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function fieldAsAvatar($index, $attributes = NULL) { |
||
| 194 | return $this->_fieldAs(function ($id, $name, $value) { |
||
| 195 | $img = new HtmlImage($id, $value); |
||
| 196 | $img->asAvatar(); |
||
| 197 | return $img; |
||
| 198 | }, $index, $attributes, "avatar"); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function fieldAsRadio($index, $attributes = NULL) { |
||
| 202 | return $this->_fieldAs(function ($id, $name, $value) use ($attributes) { |
||
| 203 | $input = new HtmlFormRadio($id, $name, $value, $value); |
||
| 204 | return $this->_prepareFormFields($input, $name, $attributes); |
||
| 205 | }, $index, $attributes, "radio"); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function fieldAsRadios($index, $elements = [], $attributes = NULL) { |
||
| 209 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements) { |
||
| 210 | return HtmlFormFields::radios($name, $elements, $caption, $value); |
||
| 211 | }, $index, $attributes, "radios"); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function fieldAsList($index, $classNames = "", $attributes = NULL) { |
||
| 215 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($classNames) { |
||
| 216 | $result = new HtmlList($name, $value); |
||
| 217 | $result->addClass($classNames); |
||
| 218 | return $result; |
||
| 219 | }, $index, $attributes, "list"); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function fieldAsInput($index, $attributes = NULL) { |
||
| 227 | } |
||
| 228 | |||
| 229 | public function fieldAsLabeledInput($index, $attributes = NULL) { |
||
| 230 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
| 231 | $input = new HtmlFormInput($id, '', 'text', $value, $caption); |
||
| 232 | $required = ''; |
||
| 233 | if (isset($attributes['rules'])) { |
||
| 234 | $rules = json_encode($attributes['rules']); |
||
| 235 | if (strpos($rules, 'empty') !== false) { |
||
| 236 | $required = 'required'; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | $input->getField() |
||
| 240 | ->labeled($caption) |
||
| 241 | ->setTagName('label') |
||
| 242 | ->addClass($required); |
||
| 243 | return $this->_prepareFormFields($input, $name, $attributes); |
||
| 244 | }, $index, $attributes, 'input'); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function fieldAsDataList($index, ?array $items = [], $attributes = NULL) { |
||
| 254 | } |
||
| 255 | |||
| 256 | public function fieldAsFile($index, $attributes = NULL) { |
||
| 257 | if (isset($this->_form)) { |
||
| 258 | $this->_form->setProperty('enctype', 'multipart/form-data'); |
||
| 259 | } |
||
| 260 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
| 261 | $input = new HtmlFormInput($id, $caption); |
||
| 262 | $input->asFile(); |
||
| 263 | return $this->_prepareFormFields($input, $name, $attributes); |
||
| 264 | }, $index, $attributes, "input"); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function fieldAsTextarea($index, $attributes = NULL) { |
||
| 268 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
| 269 | $textarea = new HtmlFormTextarea($id, $caption, $value); |
||
| 270 | return $this->_prepareFormFields($textarea, $name, $attributes); |
||
| 271 | }, $index, $attributes, "textarea"); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) { |
||
| 275 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) { |
||
| 276 | $div = new HtmlSemDoubleElement($id, $tagName, $baseClass); |
||
| 277 | $div->setContent(\htmlentities($value)); |
||
| 278 | $textarea = new HtmlFormField("field-" . $id, $div, $caption); |
||
| 279 | return $this->_prepareFormFields($textarea, $name, $attributes); |
||
| 280 | }, $index, $attributes, "element"); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function fieldAsHidden($index, $attributes = NULL) { |
||
| 284 | if (! \is_array($attributes)) { |
||
| 285 | $attributes = []; |
||
| 286 | } |
||
| 287 | $attributes["inputType"] = "hidden"; |
||
| 288 | $attributes["style"] = "display:none;"; |
||
| 289 | return $this->fieldAsInput($index, $attributes); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function fieldAsCheckbox($index, $attributes = NULL) { |
||
| 293 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
| 294 | if ($caption === null || $caption === "") |
||
| 295 | $caption = ""; |
||
| 296 | $input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier()); |
||
| 297 | $input->setChecked(JString::isBooleanTrue($value)); |
||
| 298 | return $this->_prepareFormFields($input, $name, $attributes); |
||
| 299 | }, $index, $attributes, "ck"); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) { |
||
| 303 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) { |
||
| 304 | $dd = new HtmlFormDropdown($id, $elements, $caption, $value ?? ''); |
||
| 305 | $dd->asSelect($name, $multiple); |
||
| 306 | return $this->_prepareFormFields($dd, $name, $attributes); |
||
| 307 | }, $index, $attributes, "dd"); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function fieldAsMessage($index, $attributes = NULL) { |
||
| 311 | return $this->_fieldAs(function ($id, $name, $value, $caption) { |
||
| 312 | $mess = new HtmlMessage("message-" . $id, $caption); |
||
| 313 | $mess->addHeader($value); |
||
| 314 | return $mess; |
||
| 315 | }, $index, $attributes, "message"); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function fieldAsLink($index, $attributes = NULL) { |
||
| 319 | return $this->_fieldAs(function ($id, $name, $value, $caption) { |
||
| 320 | $lnk = new HtmlLink("message-" . $id, "#", $caption); |
||
| 321 | return $lnk; |
||
| 322 | }, $index, $attributes, "link"); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Change fields type |
||
| 327 | * |
||
| 328 | * @param array $types |
||
| 329 | * an array or associative array $type=>$attributes |
||
| 330 | */ |
||
| 331 | public function fieldsAs(array $types) { |
||
| 332 | $i = 0; |
||
| 333 | if (JArray::isAssociative($types)) { |
||
| 334 | foreach ($types as $type => $attributes) { |
||
| 335 | if (\is_int($type)) |
||
| 336 | $this->fieldAs($i ++, $attributes, []); |
||
| 337 | else { |
||
| 338 | $type = preg_replace('/\d/', '', $type); |
||
| 339 | $this->fieldAs($i ++, $type, $attributes); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } else { |
||
| 343 | foreach ($types as $type) { |
||
| 344 | $this->fieldAs($i ++, $type); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | public function fieldAs($index, $type, $attributes = NULL) { |
||
| 350 | $method = "fieldAs" . \ucfirst($type); |
||
| 351 | if (\method_exists($this, $method)) { |
||
| 352 | if (! \is_array($attributes)) { |
||
| 353 | $attributes = [ |
||
| 354 | $index |
||
| 355 | ]; |
||
| 356 | } else { |
||
| 357 | \array_unshift($attributes, $index); |
||
| 358 | } |
||
| 359 | \call_user_func_array([ |
||
| 360 | $this, |
||
| 361 | $method |
||
| 362 | ], $attributes); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) { |
||
| 367 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) { |
||
| 368 | $button = new HtmlButton($id, $caption, $cssStyle); |
||
| 369 | $this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]); |
||
| 370 | return $button; |
||
| 371 | }, $index, $attributes, "submit"); |
||
| 372 | } |
||
| 373 | |||
| 374 | public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) { |
||
| 375 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($cssStyle) { |
||
| 376 | $button = new HtmlButton($id, $value, $cssStyle); |
||
| 377 | return $button; |
||
| 378 | }, $index, $attributes, "button"); |
||
| 379 | } |
||
| 380 | |||
| 381 | public function fieldAsDataTable($index, $model, $instances = null, $fields = [], $attributes = NULL) { |
||
| 401 | } |
||
| 402 | } |
||
| 403 |