Completed
Push — master ( 5fc65a...affc4f )
by Song
02:25
created

Sorter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cast of type string is incompatible with the declared type array of property $cast.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
    }
46
47
    /**
48
     * Determine if this column is currently sorted.
49
     *
50
     * @return bool
51
     */
52
    protected function isSorted()
53
    {
54
        $this->sort = app('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
Bug Best Practice introduced by
The expression $this->cast of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
80
            $sort['cast'] = $this->cast;
81
        }
82
83
        $query = app('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
}