These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Encore\Admin\Grid\Concerns; |
||
4 | |||
5 | use Encore\Admin\Grid; |
||
6 | use Encore\Admin\Grid\Tools\Selector; |
||
7 | |||
8 | /** |
||
9 | * @mixin Grid |
||
10 | */ |
||
11 | trait HasSelector |
||
12 | { |
||
13 | /** |
||
14 | * @var Selector |
||
15 | */ |
||
16 | protected $selector; |
||
17 | |||
18 | /** |
||
19 | * @param \Closure $closure |
||
20 | * |
||
21 | * @return $this |
||
22 | */ |
||
23 | public function selector(\Closure $closure) |
||
24 | { |
||
25 | $this->selector = new Selector(); |
||
26 | |||
27 | call_user_func($closure, $this->selector); |
||
28 | |||
29 | $this->header(function () { |
||
0 ignored issues
–
show
|
|||
30 | return $this->renderSelector(); |
||
31 | }); |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Apply selector query to grid model query. |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | protected function applySelectorQuery() |
||
42 | { |
||
43 | if (is_null($this->selector)) { |
||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | $active = Selector::parseSelected(); |
||
48 | |||
49 | $this->selector->getSelectors()->each(function ($selector, $column) use ($active) { |
||
50 | if (!array_key_exists($column, $active)) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $values = $active[$column]; |
||
55 | |||
56 | if ($selector['type'] == 'one') { |
||
57 | $values = current($values); |
||
58 | } |
||
59 | |||
60 | if (is_null($selector['query'])) { |
||
61 | $this->model()->whereIn($column, $values); |
||
0 ignored issues
–
show
It seems like
model() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
62 | } else { |
||
63 | call_user_func($selector['query'], $this->model(), $values); |
||
0 ignored issues
–
show
It seems like
model() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
64 | } |
||
65 | }); |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Render grid selector. |
||
72 | * |
||
73 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
74 | */ |
||
75 | public function renderSelector() |
||
76 | { |
||
77 | return $this->selector->render(); |
||
78 | } |
||
79 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.