Skip to content

Commit 5f9c9ab

Browse files
committed
Request: remove unnecessary exception throw
Add base request test
1 parent b1bf55a commit 5f9c9ab

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

src/Utilities/Request.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Yajra\DataTables\Utilities;
44

55
use Illuminate\Http\Request as BaseRequest;
6-
use Yajra\DataTables\Exceptions\Exception;
76

87
/**
98
* @mixin \Illuminate\Http\Request
@@ -54,7 +53,7 @@ public function __get($name)
5453
*
5554
* @return array
5655
*/
57-
public function columns()
56+
public function columns(): array
5857
{
5958
return (array) $this->request->input('columns');
6059
}
@@ -64,7 +63,7 @@ public function columns()
6463
*
6564
* @return bool
6665
*/
67-
public function isSearchable()
66+
public function isSearchable(): bool
6867
{
6968
return $this->request->input('search.value') != '';
7069
}
@@ -75,7 +74,7 @@ public function isSearchable()
7574
* @param int $index
7675
* @return bool
7776
*/
78-
public function isRegex($index)
77+
public function isRegex(int $index): bool
7978
{
8079
return $this->request->input("columns.$index.search.regex") === 'true';
8180
}
@@ -85,7 +84,7 @@ public function isRegex($index)
8584
*
8685
* @return array
8786
*/
88-
public function orderableColumns()
87+
public function orderableColumns(): array
8988
{
9089
if (! $this->isOrderable()) {
9190
return [];
@@ -113,7 +112,7 @@ public function orderableColumns()
113112
*
114113
* @return bool
115114
*/
116-
public function isOrderable()
115+
public function isOrderable(): bool
117116
{
118117
return $this->request->input('order') && count((array) $this->request->input('order')) > 0;
119118
}
@@ -124,7 +123,7 @@ public function isOrderable()
124123
* @param int $index
125124
* @return bool
126125
*/
127-
public function isColumnOrderable($index)
126+
public function isColumnOrderable(int $index): bool
128127
{
129128
return $this->request->input("columns.$index.orderable", 'true') == 'true';
130129
}
@@ -133,8 +132,6 @@ public function isColumnOrderable($index)
133132
* Get searchable column indexes.
134133
*
135134
* @return array
136-
*
137-
* @throws \Yajra\DataTables\Exceptions\Exception
138135
*/
139136
public function searchableColumnIndex()
140137
{
@@ -155,10 +152,8 @@ public function searchableColumnIndex()
155152
* @param int $i
156153
* @param bool $column_search
157154
* @return bool
158-
*
159-
* @throws \Yajra\DataTables\Exceptions\Exception
160155
*/
161-
public function isColumnSearchable($i, $column_search = true)
156+
public function isColumnSearchable(int $i, bool $column_search = true): bool
162157
{
163158
if ($column_search) {
164159
return
@@ -181,11 +176,10 @@ public function isColumnSearchable($i, $column_search = true)
181176
*
182177
* @param int $index
183178
* @return string
184-
*
185-
* @throws \Yajra\DataTables\Exceptions\Exception
186179
*/
187-
public function columnKeyword($index): string
180+
public function columnKeyword(int $index): string
188181
{
182+
/** @var string $keyword */
189183
$keyword = $this->request->input("columns.$index.search.value") ?? '';
190184

191185
return $this->prepareKeyword($keyword);
@@ -194,40 +188,33 @@ public function columnKeyword($index): string
194188
/**
195189
* Prepare keyword string value.
196190
*
197-
* @param mixed $keyword
191+
* @param float|array|int|string $keyword
198192
* @return string
199-
*
200-
* @throws \Yajra\DataTables\Exceptions\Exception
201193
*/
202-
protected function prepareKeyword($keyword): string
194+
protected function prepareKeyword(float|array|int|string $keyword): string
203195
{
204196
if (is_array($keyword)) {
205197
return implode(' ', $keyword);
206198
}
207199

208-
if (is_string($keyword)) {
209-
return $keyword;
210-
}
211-
212-
throw new Exception('Invalid keyword value.');
200+
return (string) $keyword;
213201
}
214202

215203
/**
216204
* Get global search keyword.
217205
*
218206
* @return string
219-
*
220-
* @throws \Yajra\DataTables\Exceptions\Exception
221207
*/
222208
public function keyword(): string
223209
{
210+
/** @var string $keyword */
224211
$keyword = $this->request->input('search.value') ?? '';
225212

226213
return $this->prepareKeyword($keyword);
227214
}
228215

229216
/**
230-
* Get column identity from input or database.
217+
* Get column name by index.
231218
*
232219
* @param int $i
233220
* @return string|null
@@ -260,6 +247,11 @@ public function getBaseRequest(): BaseRequest
260247
return $this->request;
261248
}
262249

250+
/**
251+
* Get starting record value.
252+
*
253+
* @return int
254+
*/
263255
public function start(): int
264256
{
265257
/** @var int $start */
@@ -268,6 +260,11 @@ public function start(): int
268260
return $start;
269261
}
270262

263+
/**
264+
* Get per page length.
265+
*
266+
* @return int
267+
*/
271268
public function length(): int
272269
{
273270
/** @var int $length */
@@ -276,6 +273,11 @@ public function length(): int
276273
return $length;
277274
}
278275

276+
/**
277+
* Get draw request.
278+
*
279+
* @return int
280+
*/
279281
public function draw(): int
280282
{
281283
/** @var int $draw */

tests/Unit/RequestTest.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77

88
class RequestTest extends TestCase
99
{
10-
public function test_is_searchable()
10+
/** @test */
11+
public function it_can_get_the_base_request()
12+
{
13+
$request = $this->getRequest();
14+
15+
$this->assertInstanceOf(\Illuminate\Http\Request::class, $request->getBaseRequest());
16+
}
17+
18+
/** @test */
19+
public function it_is_searchable()
1120
{
1221
$_GET['search']['value'] = '';
1322
request()->merge($_GET);
@@ -25,7 +34,8 @@ public function test_is_searchable()
2534
$this->assertTrue($request->isSearchable());
2635
}
2736

28-
public function test_column_keyword()
37+
/** @test */
38+
public function it_can_get_column_keyword()
2939
{
3040
$_GET['columns'] = [];
3141
$_GET['columns'][] = [
@@ -45,19 +55,20 @@ public function test_column_keyword()
4555
$this->assertEquals('bar', $request->columnKeyword(1));
4656
}
4757

48-
public function test_orderable_columns()
58+
/** @test */
59+
public function it_has_orderable_columns()
4960
{
5061
$_GET['columns'] = [];
5162
$_GET['columns'][] = [
5263
'orderable' => 'true',
53-
'search' => [
64+
'search' => [
5465
'value' => 'foo',
5566
],
5667
];
5768
$_GET['order'] = [];
5869
$_GET['order'][] = [
5970
'column' => 0,
60-
'dir' => 'asc',
71+
'dir' => 'asc',
6172
];
6273
request()->merge($_GET);
6374
$request = $this->getRequest();
@@ -69,19 +80,20 @@ public function test_orderable_columns()
6980
$this->assertTrue($request->isColumnOrderable(0));
7081
}
7182

72-
public function test_orderable_columns_will_set_descending_on_other_values()
83+
/** @test */
84+
public function it_has_will_set_descending_on_other_values_on_orderable_columns()
7385
{
7486
$_GET['columns'] = [];
7587
$_GET['columns'][] = [
7688
'orderable' => 'true',
77-
'search' => [
89+
'search' => [
7890
'value' => 'foo',
7991
],
8092
];
8193
$_GET['order'] = [];
8294
$_GET['order'][] = [
8395
'column' => 0,
84-
'dir' => 'bar',
96+
'dir' => 'bar',
8597
];
8698
request()->merge($_GET);
8799
$request = $this->getRequest();
@@ -93,7 +105,8 @@ public function test_orderable_columns_will_set_descending_on_other_values()
93105
$this->assertTrue($request->isColumnOrderable(0));
94106
}
95107

96-
public function test_searchable_column_index()
108+
/** @test */
109+
public function it_has_searchable_column_index()
97110
{
98111
$_GET['columns'] = [];
99112
$_GET['columns'][] = ['name' => 'foo', 'searchable' => 'true', 'search' => ['value' => 'foo']];
@@ -112,7 +125,8 @@ public function test_searchable_column_index()
112125
$this->assertEquals('bar', $request->columnName(1));
113126
}
114127

115-
public function test_keyword()
128+
/** @test */
129+
public function it_has_keyword()
116130
{
117131
$_GET['search'] = [];
118132
$_GET['search'] = ['value' => 'foo'];
@@ -121,7 +135,8 @@ public function test_keyword()
121135
$this->assertEquals('foo', $request->keyword());
122136
}
123137

124-
public function test_is_paginationable()
138+
/** @test */
139+
public function it_is_paginationable()
125140
{
126141
$_GET['start'] = 1;
127142
$_GET['length'] = 10;
@@ -147,8 +162,6 @@ public function test_is_paginationable()
147162
*/
148163
protected function getRequest()
149164
{
150-
$request = new Request();
151-
152-
return $request;
165+
return new Request();
153166
}
154167
}

0 commit comments

Comments
 (0)