Completed
Push — master ( e46d32...01f863 )
by Arjay
03:35
created

Request::columnKeyword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Datatables;
4
5
use Exception;
6
use Illuminate\Http\Request as IlluminateRequest;
7
8
/**
9
 * Class Request.
10
 *
11
 * @package Yajra\Datatables
12
 * @method input($key, $default = null)
13
 * @method has($key)
14
 * @method query($key, $default = null)
15
 * @author  Arjay Angeles <[email protected]>
16
 */
17
class Request
18
{
19
    /**
20
     * @var \Illuminate\Http\Request
21
     */
22
    protected $request;
23
24
    /**
25
     * Request constructor.
26
     *
27
     * @param \Illuminate\Http\Request $request
28
     */
29
    public function __construct(IlluminateRequest $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
30
    {
31
        $this->request = $request;
32
    }
33
34
    /**
35
     * Proxy non existing method calls to request class.
36
     *
37
     * @param mixed $name
38
     * @param mixed $arguments
39
     * @return mixed
40
     */
41
    public function __call($name, $arguments)
42
    {
43
        if (method_exists($this->request, $name)) {
44
            return call_user_func_array([$this->request, $name], $arguments);
45
        }
46
47
        return null;
48
    }
49
50
    /**
51
     * Get attributes from request instance.
52
     *
53
     * @param string $name
54
     * @return mixed
55
     */
56
    public function __get($name)
57
    {
58
        return $this->request->__get($name);
59
    }
60
61
    /**
62
     * Get all columns request input.
63
     *
64
     * @return array
65
     */
66
    public function columns()
67
    {
68
        return (array) $this->request->input('columns');
69
    }
70
71
    /**
72
     * Check if request uses legacy code
73
     *
74
     * @throws Exception
75
     */
76
    public function checkLegacyCode()
77
    {
78
        if (! $this->request->input('draw') && $this->request->input('sEcho')) {
79
            throw new Exception('DataTables legacy code is not supported! Please use DataTables 1.10++ coding convention.');
80
        } elseif (! $this->request->input('draw') && ! $this->request->input('columns')) {
81
            throw new Exception('Insufficient parameters');
82
        }
83
    }
84
85
    /**
86
     * Check if Datatables is searchable.
87
     *
88
     * @return bool
89
     */
90
    public function isSearchable()
91
    {
92
        return $this->request->input('search.value') != '';
93
    }
94
95
    /**
96
     * Check if Datatables must uses regular expressions
97
     *
98
     * @param integer $index
99
     * @return string
100
     */
101
    public function isRegex($index)
102
    {
103
        return $this->request->input("columns.$index.search.regex") === 'true';
104
    }
105
106
    /**
107
     * Get orderable columns
108
     *
109
     * @return array
110
     */
111
    public function orderableColumns()
112
    {
113
        if (! $this->isOrderable()) {
114
            return [];
115
        }
116
117
        $orderable = [];
118
        for ($i = 0, $c = count($this->request->input('order')); $i < $c; $i++) {
119
            $order_col = (int) $this->request->input("order.$i.column");
120
            $order_dir = $this->request->input("order.$i.dir");
121
            if ($this->isColumnOrderable($order_col)) {
122
                $orderable[] = ['column' => $order_col, 'direction' => $order_dir];
123
            }
124
        }
125
126
        return $orderable;
127
    }
128
129
    /**
130
     * Check if Datatables ordering is enabled.
131
     *
132
     * @return bool
133
     */
134
    public function isOrderable()
135
    {
136
        return $this->request->input('order') && count($this->request->input('order')) > 0;
137
    }
138
139
    /**
140
     * Check if a column is orderable.
141
     *
142
     * @param  integer $index
143
     * @return bool
144
     */
145
    public function isColumnOrderable($index)
146
    {
147
        return $this->request->input("columns.$index.orderable") == 'true';
148
    }
149
150
    /**
151
     * Get searchable column indexes
152
     *
153
     * @return array
154
     */
155
    public function searchableColumnIndex()
156
    {
157
        $searchable = [];
158
        for ($i = 0, $c = count($this->request->input('columns')); $i < $c; $i++) {
159
            if ($this->isColumnSearchable($i, false)) {
160
                $searchable[] = $i;
161
            }
162
        }
163
164
        return $searchable;
165
    }
166
167
    /**
168
     * Check if a column is searchable.
169
     *
170
     * @param integer $i
171
     * @param bool $column_search
172
     * @return bool
173
     */
174
    public function isColumnSearchable($i, $column_search = true)
175
    {
176
        if ($column_search) {
177
            return $this->request->input("columns.$i.searchable") === 'true' && $this->columnKeyword($i) != '';
178
        }
179
180
        return $this->request->input("columns.$i.searchable") === 'true';
181
    }
182
183
    /**
184
     * Get column's search value.
185
     *
186
     * @param integer $index
187
     * @return string
188
     */
189
    public function columnKeyword($index)
190
    {
191
        return $this->request->input("columns.$index.search.value");
192
    }
193
194
    /**
195
     * Get global search keyword
196
     *
197
     * @return string
198
     */
199
    public function keyword()
200
    {
201
        return $this->request->input('search.value');
202
    }
203
204
    /**
205
     * Get column identity from input or database.
206
     *
207
     * @param integer $i
208
     * @return string
209
     */
210
    public function columnName($i)
211
    {
212
        $column = $this->request->input("columns.$i");
213
214
        return isset($column['name']) && $column['name'] <> '' ? $column['name'] : $column['data'];
215
    }
216
217
    /**
218
     * Check if Datatables allow pagination.
219
     *
220
     * @return bool
221
     */
222
    public function isPaginationable()
223
    {
224
        return ! is_null($this->request->input('start')) && ! is_null($this->request->input('length')) && $this->request->input('length') != -1;
225
    }
226
}
227