Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

Selector::selectOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Illuminate\Contracts\Support\Renderable;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
10
class Selector implements Renderable
11
{
12
    /**
13
     * @var array|Collection
14
     */
15
    protected $selectors = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected static $selected;
21
22
    /**
23
     * Selector constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->selectors = new Collection();
28
    }
29
30
    /**
31
     * @param string $column
32
     * @param string|array $label
33
     * @param array|\Closure $options
34
     * @param null|\Closure $query
35
     *
36
     * @return $this
37
     */
38
    public function select($column, $label, $options = [], $query = null)
39
    {
40
        return $this->addSelector($column, $label, $options, $query);
0 ignored issues
show
Bug introduced by
It seems like $label defined by parameter $label on line 38 can also be of type array; however, Encore\Admin\Grid\Tools\Selector::addSelector() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $options defined by parameter $options on line 38 can also be of type object<Closure>; however, Encore\Admin\Grid\Tools\Selector::addSelector() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $query defined by parameter $query on line 38 can also be of type object<Closure>; however, Encore\Admin\Grid\Tools\Selector::addSelector() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
    }
42
43
    /**
44
     * @param string $column
45
     * @param string $label
46
     * @param array $options
47
     * @param null|\Closure $query
48
     *
49
     * @return $this
50
     */
51
    public function selectOne($column, $label, $options = [], $query = null)
52
    {
53
        return $this->addSelector($column, $label, $options, $query, 'one');
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 51 can also be of type object<Closure>; however, Encore\Admin\Grid\Tools\Selector::addSelector() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
    }
55
56
    /**
57
     * @param string $column
58
     * @param string $label
59
     * @param array $options
60
     * @param null $query
61
     * @param string $type
62
     *
63
     * @return $this
64
     */
65
    protected function addSelector($column, $label, $options = [], $query = null, $type = 'many')
66
    {
67
        if (is_array($label)) {
68
69
            if ($options instanceof \Closure) {
70
                $query = $options;
71
            }
72
73
            $options = $label;
74
            $label   = __(Str::title($column));
75
        }
76
77
        $this->selectors[$column] = compact(
78
            'label', 'options', 'type', 'query'
79
        );
80
81
        return $this;
82
    }
83
84
    /**
85
     * Get all selectors.
86
     *
87
     * @return array|Collection
88
     */
89
    public function getSelectors()
90
    {
91
        return $this->selectors;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public static function parseSelected()
98
    {
99
        if (!is_null(static::$selected)) {
100
            return static::$selected;
101
        }
102
103
        $selected = request('_selector', []);
104
105
        if (!is_array($selected)) {
106
            return [];
107
        }
108
109
        $selected = array_filter($selected, function ($value) {
110
            return !is_null($value);
111
        });
112
113
        foreach ($selected as &$value) {
114
            $value = explode(',', $value);
115
        }
116
117
        return static::$selected = $selected;
118
    }
119
120
    /**
121
     * @param string $column
122
     * @param mixed $value
123
     * @param bool $add
124
     *
125
     * @return string
126
     */
127
    public static function url($column, $value = null, $add = false)
128
    {
129
        $query    = request()->query();
130
        $selected = static::parseSelected();
131
132
        $options = Arr::get($selected, $column, []);
133
134
        if (is_null($value)) {
135
            Arr::forget($query, "_selector.{$column}");
136
137
            return request()->fullUrlWithQuery($query);
138
        }
139
140
        if (in_array($value, $options)) {
141
            array_delete($options, $value);
142
        } else {
143
            if ($add) {
144
                $options = [];
145
            }
146
147
            array_push($options, $value);
148
        }
149
150
        if (!empty($options)) {
151
            Arr::set($query, "_selector.{$column}", implode(',', $options));
152
        } else {
153
            Arr::forget($query, "_selector.{$column}");
154
        }
155
156
        return request()->fullUrlWithQuery($query);
157
    }
158
159
    /**
160
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
161
     */
162
    public function render()
163
    {
164
        return view('admin::grid.selector', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin::grid.select...tic::parseSelected())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 164 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
165
            'selectors' => $this->selectors,
166
            'selected'  => static::parseSelected(),
167
        ]);
168
    }
169
}