Completed
Push — master ( 1fe533...f58995 )
by Arjay
01:58
created

Request::prepareKeyword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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