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

src/Grid/Tools/Selector.php (1 issue)

Upgrade to new PHP Analysis Engine

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\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);
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');
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
}