Skip to content

Commit 5a060f7

Browse files
author
Jordan Hall
authored
Merge pull request #9 from langleyfoxall/feature/check-order-by-direction
Check order by direction
2 parents 4a7e189 + 991b5c5 commit 5a060f7

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Provides a Laravel API endpoint responder for the `react-dynamic-data-table` component.",
44
"require": {
55
"php": "^7.0.0",
6-
"laravel/framework": "^5.1||^6.0",
6+
"laravel/framework": "^5.1||^6.0||^7.0",
77
"langleyfoxall/helpers-laravel": "^1.10"
88
},
99
"autoload": {

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)