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\Column; |
||
4 | |||
5 | use Illuminate\Contracts\Support\Renderable; |
||
6 | |||
7 | class Sorter implements Renderable |
||
8 | { |
||
9 | /** |
||
10 | * Sort arguments. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $sort; |
||
15 | |||
16 | /** |
||
17 | * Cast Name. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $cast; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $sortName; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $columnName; |
||
32 | |||
33 | /** |
||
34 | * Sorter constructor. |
||
35 | * |
||
36 | * @param string $sortName |
||
37 | * @param string $columnName |
||
38 | * @param string $cast |
||
39 | */ |
||
40 | public function __construct($sortName, $columnName, $cast) |
||
41 | { |
||
42 | $this->sortName = $sortName; |
||
43 | $this->columnName = $columnName; |
||
44 | $this->cast = $cast; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Determine if this column is currently sorted. |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | protected function isSorted() |
||
53 | { |
||
54 | $this->sort = \request()->get($this->sortName); |
||
55 | |||
56 | if (empty($this->sort)) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | return isset($this->sort['column']) && $this->sort['column'] == $this->columnName; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | public function render() |
||
67 | { |
||
68 | $icon = 'fa-sort'; |
||
69 | $type = 'desc'; |
||
70 | |||
71 | if ($this->isSorted()) { |
||
72 | $type = $this->sort['type'] == 'desc' ? 'asc' : 'desc'; |
||
73 | $icon .= "-amount-{$this->sort['type']}"; |
||
74 | } |
||
75 | |||
76 | // set sort value |
||
77 | $sort = ['column' => $this->columnName, 'type' => $type]; |
||
78 | |||
79 | if ($this->cast) { |
||
0 ignored issues
–
show
|
|||
80 | $sort['cast'] = $this->cast; |
||
81 | } |
||
82 | |||
83 | $query = \request()->all(); |
||
84 | $query = array_merge($query, [$this->sortName => $sort]); |
||
85 | |||
86 | $url = url()->current().'?'.http_build_query($query); |
||
87 | |||
88 | return "<a class=\"fa fa-fw $icon\" href=\"$url\"></a>"; |
||
89 | } |
||
90 | } |
||
91 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.