Skip to content

Commit

Permalink
Merge pull request #9 from langleyfoxall/feature/check-order-by-direc…
Browse files Browse the repository at this point in the history
…tion

Check order by direction
  • Loading branch information
Jordan Hall authored Apr 7, 2020
2 parents 4a7e189 + 991b5c5 commit 5a060f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Provides a Laravel API endpoint responder for the `react-dynamic-data-table` component.",
"require": {
"php": "^7.0.0",
"laravel/framework": "^5.1||^6.0",
"laravel/framework": "^5.1||^6.0||^7.0",
"langleyfoxall/helpers-laravel": "^1.10"
},
"autoload": {
Expand Down
25 changes: 16 additions & 9 deletions src/DataTableResponder.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
namespace LangleyFoxall\ReactDynamicDataTableLaravelApi;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;

/**
* Class DataTableResponder
Expand Down Expand Up @@ -57,14 +60,14 @@ class DataTableResponder
public function __construct($className, Request $request)
{
if (!class_exists($className)) {
throw new \InvalidArgumentException('Provided class does not exist.');
throw new InvalidArgumentException('Provided class does not exist.');
}

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

if (!$this->model instanceof Model) {
throw new \InvalidArgumentException('Provided class is not an Eloquent model.');
throw new InvalidArgumentException('Provided class is not an Eloquent model.');
}
}

Expand Down Expand Up @@ -118,9 +121,9 @@ public function collectionManipulator(callable $collectionManipulator)

/**
* Sets the meta for the API response
*
*
* @see DataTableResponder::makeMeta
*
*
* @param callable $collectionManipulator
* @return DataTableResponder
*/
Expand All @@ -134,13 +137,17 @@ public function setResponseMeta(array $meta = [])
* Builds the Eloquent query based on the request.
*
* @param Request $request
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
private function buildQuery(Request $request)
{
$orderByField = $request->get('orderByField');
$orderByDirection = $request->get('orderByDirection');

if (!in_array(strtolower($orderByDirection), ['asc', 'desc'])) {
throw new InvalidArgumentException('Order by direction must be either asc or desc.');
}

$query = $this->model->query();

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

/**
* @param Builder $query
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
* @return LengthAwarePaginator
*/
private function paginateQuery(Builder $query)
{
return $query->paginate($this->perPage);
}

/**
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $results
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
* @param LengthAwarePaginator $results
* @return LengthAwarePaginator
*/
private function manipulateCollection($results)
{
Expand Down Expand Up @@ -257,7 +264,7 @@ private function disallowOrderingBy()
}

/**
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function respond()
{
Expand Down

0 comments on commit 5a060f7

Please sign in to comment.