Skip to content

Commit 3bf7af3

Browse files
committed
Ensure order by direction is 'asc' or 'desc'
1 parent 4a7e189 commit 3bf7af3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/DataTableResponder.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22
namespace LangleyFoxall\ReactDynamicDataTableLaravelApi;
33

4+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
45
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Http\JsonResponse;
68
use Illuminate\Http\Request;
79
use Illuminate\Support\Collection;
810
use Illuminate\Support\Str;
11+
use InvalidArgumentException;
912

1013
/**
1114
* Class DataTableResponder
@@ -57,14 +60,14 @@ class DataTableResponder
5760
public function __construct($className, Request $request)
5861
{
5962
if (!class_exists($className)) {
60-
throw new \InvalidArgumentException('Provided class does not exist.');
63+
throw new InvalidArgumentException('Provided class does not exist.');
6164
}
6265

6366
$this->model = new $className();
6467
$this->request = $request;
6568

6669
if (!$this->model instanceof Model) {
67-
throw new \InvalidArgumentException('Provided class is not an Eloquent model.');
70+
throw new InvalidArgumentException('Provided class is not an Eloquent model.');
6871
}
6972
}
7073

@@ -118,9 +121,9 @@ public function collectionManipulator(callable $collectionManipulator)
118121

119122
/**
120123
* Sets the meta for the API response
121-
*
124+
*
122125
* @see DataTableResponder::makeMeta
123-
*
126+
*
124127
* @param callable $collectionManipulator
125128
* @return DataTableResponder
126129
*/
@@ -134,13 +137,17 @@ public function setResponseMeta(array $meta = [])
134137
* Builds the Eloquent query based on the request.
135138
*
136139
* @param Request $request
137-
* @return \Illuminate\Database\Eloquent\Builder
140+
* @return Builder
138141
*/
139142
private function buildQuery(Request $request)
140143
{
141144
$orderByField = $request->get('orderByField');
142145
$orderByDirection = $request->get('orderByDirection');
143146

147+
if (!in_array(strtolower($orderByDirection), ['asc', 'desc'])) {
148+
throw new InvalidArgumentException('Order by direction must be either asc or desc.');
149+
}
150+
144151
$query = $this->model->query();
145152

146153
if ($orderByField && $orderByDirection) {
@@ -165,16 +172,16 @@ private function buildQuery(Request $request)
165172

166173
/**
167174
* @param Builder $query
168-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
175+
* @return LengthAwarePaginator
169176
*/
170177
private function paginateQuery(Builder $query)
171178
{
172179
return $query->paginate($this->perPage);
173180
}
174181

175182
/**
176-
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $results
177-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
183+
* @param LengthAwarePaginator $results
184+
* @return LengthAwarePaginator
178185
*/
179186
private function manipulateCollection($results)
180187
{
@@ -257,7 +264,7 @@ private function disallowOrderingBy()
257264
}
258265

259266
/**
260-
* @return \Illuminate\Http\JsonResponse
267+
* @return JsonResponse
261268
*/
262269
public function respond()
263270
{

0 commit comments

Comments
 (0)