-
-
Notifications
You must be signed in to change notification settings - Fork 859
/
Request.php
236 lines (210 loc) · 5.35 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace Yajra\DataTables\Utilities;
/**
* @method mixed input($key, $default = null)
* @method mixed get($key, $default = null)
* @method mixed query($key, $default = null)
* @method mixed has($key)
* @method mixed merge(array $values)
* @method bool wantsJson()
* @method bool ajax()
* @method array all()
*/
class Request
{
/**
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* Request constructor.
*/
public function __construct()
{
$this->request = app('request');
}
/**
* Proxy non existing method calls to request class.
*
* @param mixed $name
* @param mixed $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
if (method_exists($this->request, $name)) {
return call_user_func_array([$this->request, $name], $arguments);
}
}
/**
* Get attributes from request instance.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->request->__get($name);
}
/**
* Get all columns request input.
*
* @return array
*/
public function columns()
{
return (array) $this->request->input('columns');
}
/**
* Check if DataTables is searchable.
*
* @return bool
*/
public function isSearchable()
{
return $this->request->input('search.value') != '';
}
/**
* Check if DataTables must uses regular expressions.
*
* @param int $index
* @return bool
*/
public function isRegex($index)
{
return $this->request->input("columns.$index.search.regex") === 'true';
}
/**
* Get orderable columns.
*
* @return array
*/
public function orderableColumns()
{
if (! $this->isOrderable()) {
return [];
}
$orderable = [];
for ($i = 0, $c = count($this->request->input('order')); $i < $c; $i++) {
$order_col = (int) $this->request->input("order.$i.column");
$order_dir = strtolower($this->request->input("order.$i.dir")) === 'asc' ? 'asc' : 'desc';
if ($this->isColumnOrderable($order_col)) {
$orderable[] = ['column' => $order_col, 'direction' => $order_dir];
}
}
return $orderable;
}
/**
* Check if DataTables ordering is enabled.
*
* @return bool
*/
public function isOrderable()
{
return $this->request->input('order') && count($this->request->input('order')) > 0;
}
/**
* Check if a column is orderable.
*
* @param int $index
* @return bool
*/
public function isColumnOrderable($index)
{
return $this->request->input("columns.$index.orderable", 'true') == 'true';
}
/**
* Get searchable column indexes.
*
* @return array
*/
public function searchableColumnIndex()
{
$searchable = [];
for ($i = 0, $c = count($this->request->input('columns')); $i < $c; $i++) {
if ($this->isColumnSearchable($i, false)) {
$searchable[] = $i;
}
}
return $searchable;
}
/**
* Check if a column is searchable.
*
* @param int $i
* @param bool $column_search
* @return bool
*/
public function isColumnSearchable($i, $column_search = true)
{
if ($column_search) {
return
(
$this->request->input("columns.$i.searchable", 'true') === 'true'
||
$this->request->input("columns.$i.searchable", 'true') === true
)
&& $this->columnKeyword($i) != '';
}
return
$this->request->input("columns.$i.searchable", 'true') === 'true'
||
$this->request->input("columns.$i.searchable", 'true') === true;
}
/**
* Get column's search value.
*
* @param int $index
* @return string
*/
public function columnKeyword($index)
{
$keyword = $this->request->input("columns.$index.search.value");
return $this->prepareKeyword($keyword);
}
/**
* Prepare keyword string value.
*
* @param string|array $keyword
* @return string
*/
protected function prepareKeyword($keyword)
{
if (is_array($keyword)) {
return implode(' ', $keyword);
}
return $keyword;
}
/**
* Get global search keyword.
*
* @return string
*/
public function keyword()
{
$keyword = $this->request->input('search.value');
return $this->prepareKeyword($keyword);
}
/**
* Get column identity from input or database.
*
* @param int $i
* @return string
*/
public function columnName($i)
{
$column = $this->request->input("columns.$i");
return isset($column['name']) && $column['name'] != '' ? $column['name'] : $column['data'];
}
/**
* Check if DataTables allow pagination.
*
* @return bool
*/
public function isPaginationable()
{
return ! is_null($this->request->input('start')) &&
! is_null($this->request->input('length')) &&
$this->request->input('length') != -1;
}
}