We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 61 |
| Total Lines | 545 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 3 | Features | 1 |
Complex classes like CrudFilter 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 CrudFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CrudFilter |
||
| 10 | { |
||
| 11 | public $name; // the name of the filtered variable (db column name) |
||
| 12 | public $type = 'select2'; // the name of the filter view that will be loaded |
||
| 13 | public $key; //camelCased version of filter name to use in internal ids, js functions and css classes. |
||
| 14 | public $label; |
||
| 15 | public $placeholder; |
||
| 16 | public $values; |
||
| 17 | public $options; |
||
| 18 | public $logic; |
||
| 19 | public $fallbackLogic; |
||
| 20 | public $currentValue; |
||
| 21 | public $view; |
||
| 22 | public $viewNamespace = 'crud::filters'; |
||
| 23 | public $applied = false; |
||
| 24 | |||
| 25 | public function __construct($options, $values, $logic, $fallbackLogic) |
||
| 26 | { |
||
| 27 | // if filter exists |
||
| 28 | if ($this->crud()->hasFilterWhere('name', $options['name'])) { |
||
| 29 | $properties = get_object_vars($this->crud()->firstFilterWhere('name', $options['name'])); |
||
|
|
|||
| 30 | foreach ($properties as $property => $value) { |
||
| 31 | $this->{$property} = $value; |
||
| 32 | } |
||
| 33 | } else { |
||
| 34 | // it means we're creating the filter now, |
||
| 35 | $this->name = $options['name']; |
||
| 36 | $this->key = Str::camel($options['name']); |
||
| 37 | $this->type = $options['type'] ?? $this->type; |
||
| 38 | $this->label = $options['label'] ?? $this->crud()->makeLabel($this->name); |
||
| 39 | $this->viewNamespace = $options['view_namespace'] ?? $this->viewNamespace; |
||
| 40 | $this->view = $this->type; |
||
| 41 | $this->placeholder = $options['placeholder'] ?? ''; |
||
| 42 | |||
| 43 | $this->values = is_callable($values) ? $values() : $values; |
||
| 44 | $this->options = $options; |
||
| 45 | $this->logic = $logic; |
||
| 46 | $this->fallbackLogic = $fallbackLogic; |
||
| 47 | } |
||
| 48 | |||
| 49 | if (\Request::has($this->name)) { |
||
| 50 | $this->currentValue = \Request::input($this->name); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Check if the field is currently active. This happens when there's a GET parameter |
||
| 56 | * in the current request with the same name as the name of the field. |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function isActive() |
||
| 61 | { |
||
| 62 | if (\Request::has($this->name)) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | |||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check if the filter has already had the apply method called on it. |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function wasApplied() |
||
| 75 | { |
||
| 76 | return $this->applied; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check if the filter has not had the apply method called on it yet. |
||
| 81 | * This is the inverse of the wasApplied() method. |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | public function wasNotApplied() |
||
| 86 | { |
||
| 87 | return ! $this->applied; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Run the filter logic, default logic and/or fallback logic so that from this point on |
||
| 92 | * the CRUD query has its results filtered, according to the Request. |
||
| 93 | * |
||
| 94 | * @param array $input The GET parameters for which the filter should be applied. |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function apply($input = null) |
||
| 98 | { |
||
| 99 | // mark the field as already applied |
||
| 100 | $this->applied(true); |
||
| 101 | |||
| 102 | if (is_array($input)) { |
||
| 103 | $input = new ParameterBag($input); |
||
| 104 | } |
||
| 105 | |||
| 106 | $input = $input ?? new ParameterBag($this->crud()->getRequest()->all()); |
||
| 107 | |||
| 108 | if (! $input->has($this->name)) { |
||
| 109 | // if fallback logic was supplied and is a closure |
||
| 110 | if (is_callable($this->fallbackLogic)) { |
||
| 111 | return ($this->fallbackLogic)(); |
||
| 112 | } |
||
| 113 | |||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | // if a closure was passed as "filterLogic" |
||
| 118 | if (is_callable($this->logic)) { |
||
| 119 | return ($this->logic)($input->get($this->name)); |
||
| 120 | } else { |
||
| 121 | return $this->applyDefaultLogic($this->name, false); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the full path of the filter view, including the view namespace. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getViewWithNamespace() |
||
| 131 | { |
||
| 132 | return $this->viewNamespace.'.'.$this->view; |
||
| 133 | } |
||
| 134 | |||
| 135 | // --------------------- |
||
| 136 | // FLUENT SYNTAX METHODS |
||
| 137 | // --------------------- |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Create a CrudFilter object with the parameter as its name. |
||
| 141 | * |
||
| 142 | * @param string $name Name of the column in the db, or model attribute. |
||
| 143 | * @return CrudPanel |
||
| 144 | */ |
||
| 145 | public static function name($name) |
||
| 146 | { |
||
| 147 | return new static(compact('name'), null, null, null); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Remove the current filter from the current operation. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function remove() |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Remove an attribute from the current filter definition array. |
||
| 162 | * |
||
| 163 | * @param string $attribute Name of the attribute being removed. |
||
| 164 | * @return CrudFilter |
||
| 165 | */ |
||
| 166 | public function forget($attribute) |
||
| 167 | { |
||
| 168 | if (property_exists($this, $attribute)) { |
||
| 169 | $this->{$attribute} = false; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (isset($this->options[$attribute])) { |
||
| 173 | unset($this->options[$attribute]); |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->crud()->replaceFilter($this->name, $this); |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Remove an attribute from one field's definition array. |
||
| 183 | * @param string $field The name of the field. |
||
| 184 | * @param string $attribute The name of the attribute being removed. |
||
| 185 | */ |
||
| 186 | public function removeFilterAttribute($filter, $attribute) |
||
| 187 | { |
||
| 188 | $fields = $this->fields(); |
||
| 189 | |||
| 190 | unset($fields[$field][$attribute]); |
||
| 191 | |||
| 192 | $this->setOperationSetting('fields', $fields); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Move the current filter after another filter. |
||
| 197 | * |
||
| 198 | * @param string $destination Name of the destination filter. |
||
| 199 | * @return CrudFilter |
||
| 200 | */ |
||
| 201 | public function after($destination) |
||
| 202 | { |
||
| 203 | $this->crud()->moveFilter($this->name, 'after', $destination); |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Move the current field before another field. |
||
| 210 | * |
||
| 211 | * @param string $destination Name of the destination field. |
||
| 212 | * @return CrudFilter |
||
| 213 | */ |
||
| 214 | public function before($destination) |
||
| 215 | { |
||
| 216 | $this->crud()->moveFilter($this->name, 'before', $destination); |
||
| 217 | |||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Make the current field the first one in the fields list. |
||
| 223 | * |
||
| 224 | * @return CrudPanel |
||
| 225 | */ |
||
| 226 | public function makeFirst() |
||
| 227 | { |
||
| 228 | $this->crud()->moveFilter($this->name, 'before', $this->crud()->filters()->first()->name); |
||
| 229 | |||
| 230 | return $this; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Make the current field the last one in the fields list. |
||
| 235 | * |
||
| 236 | * @return CrudPanel |
||
| 237 | */ |
||
| 238 | public function makeLast() |
||
| 239 | { |
||
| 240 | $this->crud()->removeFilter($this->name); |
||
| 241 | $this->crud()->addCrudFilter($this); |
||
| 242 | |||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | // ----------------------- |
||
| 247 | // FILTER-SPECIFIC SETTERS |
||
| 248 | // ----------------------- |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the type of the filter. |
||
| 252 | * |
||
| 253 | * @param string $value Name of blade view that shows the field. |
||
| 254 | * @return CrudFilter |
||
| 255 | */ |
||
| 256 | public function type($value) |
||
| 257 | { |
||
| 258 | $this->type = $value; |
||
| 259 | $this->view = $value; |
||
| 260 | |||
| 261 | return $this->save(); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set the label of the filter - the element that the end-user can see and click |
||
| 266 | * to activate the filter or an input that will activate the filter. |
||
| 267 | * |
||
| 268 | * @param string $value A name for this filter that the end-user will understand. |
||
| 269 | * @return CrudFilter |
||
| 270 | */ |
||
| 271 | public function label($value) |
||
| 272 | { |
||
| 273 | $this->label = $value; |
||
| 274 | |||
| 275 | return $this->save(); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set the values for the current filter, for the filters who need values. |
||
| 280 | * For example, the dropdown, select2 and select2 filters let the user select |
||
| 281 | * pre-determined values to filter with. This is how to set those values that will be picked up. |
||
| 282 | * |
||
| 283 | * @param array|function $value Key-value array with values for the user to pick from, or a function which also return a Key-value array. |
||
| 284 | * @return CrudFilter |
||
| 285 | */ |
||
| 286 | public function values($value) |
||
| 287 | { |
||
| 288 | $this->values = (! is_string($value) && is_callable($value)) ? $value() : $value; |
||
| 289 | |||
| 290 | return $this->save(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set the blade view that will be used by the filter. |
||
| 295 | * Should NOT include the namespace, that's defined separately using 'viewNamespace'. |
||
| 296 | * |
||
| 297 | * @param string $value Path to the blade file, after the view namespace. |
||
| 298 | * @return CrudFilter |
||
| 299 | */ |
||
| 300 | public function view($value) |
||
| 301 | { |
||
| 302 | $this->view = $value; |
||
| 303 | |||
| 304 | return $this->save(); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * The path to the blade views directory where the filter file will be found. Ex: 'crud::filters' |
||
| 309 | * Useful to load filters from a different package or directory. |
||
| 310 | * |
||
| 311 | * @param string $value Blade path to the directory. |
||
| 312 | * @return CrudFilter |
||
| 313 | */ |
||
| 314 | public function viewNamespace($value) |
||
| 315 | { |
||
| 316 | $this->viewNamespace = $value; |
||
| 317 | |||
| 318 | return $this->save(); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Define what happens when the filter is active, through a closure. |
||
| 323 | * |
||
| 324 | * @param Closure $value Closure that will be called when Request has this name as GET parameter. |
||
| 325 | * @return CrudFilter |
||
| 326 | */ |
||
| 327 | public function logic($value) |
||
| 328 | { |
||
| 329 | $this->logic = $value; |
||
| 330 | |||
| 331 | return $this->save(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Define what happens when the filter is NOT active, through a closure. |
||
| 336 | * |
||
| 337 | * @param Closure $value Closure that will be called when Request does NOT have this name as GET parameter. |
||
| 338 | * @return CrudFilter |
||
| 339 | */ |
||
| 340 | public function fallbackLogic($value) |
||
| 341 | { |
||
| 342 | $this->fallbackLogic = $value; |
||
| 343 | |||
| 344 | return $this->save(); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Define if the filter has already been applied (logic or fallbackLogic called). |
||
| 349 | * |
||
| 350 | * @param bool $value Whether the filter has been run. |
||
| 351 | * @return CrudFilter |
||
| 352 | */ |
||
| 353 | public function applied($value) |
||
| 354 | { |
||
| 355 | $this->applied = $value; |
||
| 356 | |||
| 357 | return $this->save(); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Aliases of the logic() method. |
||
| 362 | */ |
||
| 363 | public function whenActive($value) |
||
| 364 | { |
||
| 365 | return $this->logic($value); |
||
| 366 | } |
||
| 367 | |||
| 368 | public function ifActive($value) |
||
| 369 | { |
||
| 370 | return $this->logic($value); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Alises of the fallbackLogic() method. |
||
| 375 | */ |
||
| 376 | public function whenInactive($value) |
||
| 377 | { |
||
| 378 | return $this->fallbackLogic($value); |
||
| 379 | } |
||
| 380 | |||
| 381 | public function whenNotActive($value) |
||
| 382 | { |
||
| 383 | return $this->fallbackLogic($value); |
||
| 384 | } |
||
| 385 | |||
| 386 | public function ifInactive($value) |
||
| 387 | { |
||
| 388 | return $this->fallbackLogic($value); |
||
| 389 | } |
||
| 390 | |||
| 391 | public function ifNotActive($value) |
||
| 392 | { |
||
| 393 | return $this->fallbackLogic($value); |
||
| 394 | } |
||
| 395 | |||
| 396 | public function else($value) |
||
| 397 | { |
||
| 398 | return $this->fallbackLogic($value); |
||
| 399 | } |
||
| 400 | |||
| 401 | // --------------- |
||
| 402 | // PRIVATE METHODS |
||
| 403 | // --------------- |
||
| 404 | |||
| 405 | private function crud() |
||
| 406 | { |
||
| 407 | return app()->make('crud'); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Set the value for a certain attribute on the CrudFilter object. |
||
| 412 | * |
||
| 413 | * @param string $attribute Name of the attribute. |
||
| 414 | * @param string $value Value of that attribute. |
||
| 415 | */ |
||
| 416 | private function setOptionValue($attribute, $value) |
||
| 417 | { |
||
| 418 | $this->options[$attribute] = $value; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Replace all field options on the CrudFilter object |
||
| 423 | * with the given array of attribute-value pairs. |
||
| 424 | * |
||
| 425 | * @param array $array Array of options and their values. |
||
| 426 | */ |
||
| 427 | private function setAllOptionsValues($array) |
||
| 428 | { |
||
| 429 | $this->options = $array; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Update the global CrudPanel object with the current field options. |
||
| 434 | * |
||
| 435 | * @return CrudFilter |
||
| 436 | */ |
||
| 437 | private function save() |
||
| 438 | { |
||
| 439 | $key = $this->name; |
||
| 440 | |||
| 441 | if ($this->crud()->hasFilterWhere('name', $key)) { |
||
| 442 | $this->crud()->modifyFilter($key, (array) $this); |
||
| 443 | } else { |
||
| 444 | $this->crud()->addCrudFilter($this); |
||
| 445 | } |
||
| 446 | |||
| 447 | return $this; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $name |
||
| 452 | * @param string $operator |
||
| 453 | * @param array $input |
||
| 454 | */ |
||
| 455 | private function applyDefaultLogic($name, $operator, $input = null) |
||
| 456 | { |
||
| 457 | $input = $input ?? $this->crud()->getRequest()->all(); |
||
| 458 | |||
| 459 | // if this filter is active (the URL has it as a GET parameter) |
||
| 460 | switch ($operator) { |
||
| 461 | // if no operator was passed, just use the equals operator |
||
| 462 | case false: |
||
| 463 | $this->crud()->addClause('where', $name, $input[$name]); |
||
| 464 | break; |
||
| 465 | |||
| 466 | case 'scope': |
||
| 467 | $this->crud()->addClause($operator); |
||
| 468 | break; |
||
| 469 | |||
| 470 | // TODO: |
||
| 471 | // whereBetween |
||
| 472 | // whereNotBetween |
||
| 473 | // whereIn |
||
| 474 | // whereNotIn |
||
| 475 | // whereNull |
||
| 476 | // whereNotNull |
||
| 477 | // whereDate |
||
| 478 | // whereMonth |
||
| 479 | // whereDay |
||
| 480 | // whereYear |
||
| 481 | // whereColumn |
||
| 482 | // like |
||
| 483 | |||
| 484 | // sql comparison operators |
||
| 485 | case '=': |
||
| 486 | case '<=>': |
||
| 487 | case '<>': |
||
| 488 | case '!=': |
||
| 489 | case '>': |
||
| 490 | case '>=': |
||
| 491 | case '<': |
||
| 492 | case '<=': |
||
| 493 | $this->crud()->addClause('where', $name, $operator, $input[$name]); |
||
| 494 | break; |
||
| 495 | |||
| 496 | default: |
||
| 497 | abort(500, 'Unknown filter operator.'); |
||
| 498 | break; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | // ----------------- |
||
| 503 | // DEBUGGING METHODS |
||
| 504 | // ----------------- |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Dump the current object to the screen, |
||
| 508 | * so that the developer can see its contents. |
||
| 509 | * |
||
| 510 | * @return CrudFilter |
||
| 511 | */ |
||
| 512 | public function dump() |
||
| 513 | { |
||
| 514 | dump($this); |
||
| 515 | |||
| 516 | return $this; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Dump and die. Duumps the current object to the screen, |
||
| 521 | * so that the developer can see its contents, then stops |
||
| 522 | * the execution. |
||
| 523 | * |
||
| 524 | * @return CrudFilter |
||
| 525 | */ |
||
| 526 | public function dd() |
||
| 527 | { |
||
| 528 | dd($this); |
||
| 529 | |||
| 530 | return $this; |
||
| 531 | } |
||
| 532 | |||
| 533 | // ------------- |
||
| 534 | // MAGIC METHODS |
||
| 535 | // ------------- |
||
| 536 | |||
| 537 | /** |
||
| 538 | * If a developer calls a method that doesn't exist, assume they want: |
||
| 539 | * - $this->options['whatever'] to be set to that value; |
||
| 540 | * - that filter be updated inside the global CrudPanel object;. |
||
| 541 | * |
||
| 542 | * Eg: type('number') will set the "type" attribute to "number" |
||
| 543 | * |
||
| 544 | * @param string $method The method being called that doesn't exist. |
||
| 545 | * @param array $parameters The arguments when that method was called. |
||
| 546 | * |
||
| 547 | * @return CrudFilter |
||
| 548 | */ |
||
| 549 | public function __call($method, $parameters) |
||
| 554 | } |
||
| 555 | } |
||
| 556 |