Completed
Push — master ( 4cc8d8...e56a52 )
by Arjay
04:43
created

Request::checkLegacyCode()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
c 1
b 0
f 0
rs 8.8571
cc 5
eloc 5
nc 3
nop 0
1
<?php
2
3
namespace Yajra\Datatables;
4
5
use Exception;
6
use Illuminate\Http\Request as IlluminateRequest;
7
8
/**
9
 * @property array columns
10
 */
11
class Request extends IlluminateRequest
12
{
13
    /**
14
     * Check if request uses legacy code
15
     *
16
     * @throws Exception
17
     */
18
    public function checkLegacyCode()
19
    {
20
        if (! $this->get('draw') && $this->get('sEcho')) {
21
            throw new Exception('DataTables legacy code is not supported! Please use DataTables 1.10++ coding convention.');
22
        } elseif (! $this->get('draw') && ! $this->get('columns')) {
23
            throw new Exception('Insufficient parameters');
24
        }
25
    }
26
27
    /**
28
     * Check if Datatables is searchable.
29
     *
30
     * @return bool
31
     */
32
    public function isSearchable()
33
    {
34
        return $this->get('search')['value'] != '';
35
    }
36
37
    /**
38
     * Get column's search value.
39
     *
40
     * @param integer $index
41
     * @return string
42
     */
43
    public function columnKeyword($index)
44
    {
45
        return $this->columns[$index]['search']['value'];
46
    }
47
48
    /**
49
     * Check if Datatables must uses regular expressions
50
     *
51
     * @param integer $index
52
     * @return string
53
     */
54
    public function isRegex($index)
55
    {
56
        return $this->columns[$index]['search']['regex'] === 'true';
57
    }
58
59
    /**
60
     * Get orderable columns
61
     *
62
     * @return array
63
     */
64
    public function orderableColumns()
65
    {
66
        if (! $this->isOrderable()) {
67
            return [];
68
        }
69
70
        $orderable = [];
71
        for ($i = 0, $c = count($this->get('order')); $i < $c; $i++) {
72
            $order_col = (int) $this->get('order')[$i]['column'];
73
            $order_dir = $this->get('order')[$i]['dir'];
74
            if ($this->isColumnOrderable($order_col)) {
75
                $orderable[] = ['column' => $order_col, 'direction' => $order_dir];
76
            }
77
        }
78
79
        return $orderable;
80
    }
81
82
    /**
83
     * Check if Datatables ordering is enabled.
84
     *
85
     * @return bool
86
     */
87
    public function isOrderable()
88
    {
89
        return $this->get('order') && count($this->get('order')) > 0;
90
    }
91
92
    /**
93
     * Check if a column is orderable.
94
     *
95
     * @param  integer $index
96
     * @return bool
97
     */
98
    public function isColumnOrderable($index)
99
    {
100
        return $this->get('columns')[$index]['orderable'] == 'true';
101
    }
102
103
    /**
104
     * Get searchable column indexes
105
     *
106
     * @return array
107
     */
108
    public function searchableColumnIndex()
109
    {
110
        $searchable = [];
111
        for ($i = 0, $c = count($this->get('columns')); $i < $c; $i++) {
112
            if ($this->isColumnSearchable($i, false)) {
113
                $searchable[] = $i;
114
            }
115
        }
116
117
        return $searchable;
118
    }
119
120
    /**
121
     * Check if a column is searchable.
122
     *
123
     * @param integer $i
124
     * @param bool $column_search
125
     * @return bool
126
     */
127
    public function isColumnSearchable($i, $column_search = true)
128
    {
129
        $columns = $this->get('columns');
130
        if ($column_search) {
131
            return $columns[$i]['searchable'] == 'true' && $columns[$i]['search']['value'] != '';
132
        }
133
134
        return $columns[$i]['searchable'] == 'true';
135
    }
136
137
    /**
138
     * Get global search keyword
139
     *
140
     * @return string
141
     */
142
    public function keyword()
143
    {
144
        return $this->get('search')['value'];
145
    }
146
147
    /**
148
     * Get column identity from input or database.
149
     *
150
     * @param integer $i
151
     * @return string
152
     */
153
    public function columnName($i)
154
    {
155
        return $this->get('columns')[$i]['name'];
156
    }
157
158
    /**
159
     * Check if Datatables allow pagination.
160
     *
161
     * @return bool
162
     */
163
    public function isPaginationable()
164
    {
165
        return ! is_null($this->get('start')) && ! is_null($this->get('length')) && $this->get('length') != -1;
166
    }
167
}
168