Completed
Push — master ( 928d8a...2f7023 )
by Arjay
07:15
created

Request::keyword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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