Skip to content

Commit 8100771

Browse files
authored
Merge pull request #4 from dextermb/feature/add-disallow-ordering-by
Add overrideOrderByLogic and disallowOrderingBy
2 parents 9e7ffa7 + c9a5894 commit 8100771

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"php": "^7.0.0",
66
"illuminate/http": "^5.1",
77
"illuminate/database": "^5.1",
8-
"langleyfoxall/helpers-laravel": "^1.10"
8+
"langleyfoxall/helpers-laravel": "^1.10",
9+
"illuminate/support": "^5.1"
910
},
1011
"autoload": {
1112
"psr-4": {

src/DataTableResponder.php

+60-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Http\Request;
7+
use Illuminate\Support\Str;
78

89
/**
910
* Class DataTableResponder
@@ -26,6 +27,11 @@ class DataTableResponder
2627
*/
2728
private $queryManipulator;
2829

30+
/**
31+
* @var array|callable[]
32+
*/
33+
private $orderByOverrides = [];
34+
2935
/**
3036
* @var callable
3137
*/
@@ -80,9 +86,21 @@ public function query(callable $queryManipulator)
8086
return $this;
8187
}
8288

89+
/**
90+
* Sets the field name to callable mapping array used to override the query order by logic
91+
*
92+
* @param array|callable[] $orderByOverride
93+
* @return DataTableResponder
94+
*/
95+
public function overrideOrderByLogic(array $orderByOverrides)
96+
{
97+
$this->orderByOverrides = $orderByOverrides;
98+
return $this;
99+
}
100+
83101
/**
84102
* Sets the callable used to manipulate the query results collection
85-
*
103+
*
86104
* @param callable $collectionManipulator
87105
* @return DataTableResponder
88106
*/
@@ -106,10 +124,18 @@ private function buildQuery(Request $request)
106124
$query = $this->model->query();
107125

108126
if ($orderByField && $orderByDirection) {
109-
$query->orderBy($orderByField, $orderByDirection);
127+
if (in_array($orderByField, array_keys($this->orderByOverrides))) {
128+
call_user_func_array(
129+
$this->orderByOverrides[$orderByField],
130+
[$query, $orderByDirection]
131+
);
132+
} else {
133+
$query->orderBy($orderByField, $orderByDirection);
134+
}
110135
}
111136

112137
$queryManipulator = $this->queryManipulator;
138+
113139
if ($queryManipulator) {
114140
$queryManipulator($query);
115141
}
@@ -146,6 +172,35 @@ private function manipulateCollection($results)
146172
return $results;
147173
}
148174

175+
/**
176+
* @return array|string[]
177+
*/
178+
private function disallowOrderingBy()
179+
{
180+
$methods = get_class_methods($this->model);
181+
$customAttributes = [];
182+
183+
foreach($methods as $method) {
184+
if (!preg_match('/^get(\w+)Attribute$/', $method, $matches)) {
185+
continue;
186+
}
187+
188+
if (empty($matches[1])) {
189+
continue;
190+
}
191+
192+
$customAttribute = Str::snake($matches[1]);
193+
194+
if (in_array($customAttribute, array_keys($this->orderByOverrides))) {
195+
continue;
196+
}
197+
198+
$customAttributes[] = $customAttribute;
199+
}
200+
201+
return $customAttributes;
202+
}
203+
149204
/**
150205
* @return \Illuminate\Http\JsonResponse
151206
*/
@@ -156,6 +211,8 @@ public function respond()
156211
$results = $this->paginateQuery($query);
157212
$results = $this->manipulateCollection($results);
158213

159-
return DataTableResponse::success($results)->json();
214+
$disallowOrderingBy = $this->disallowOrderingBy();
215+
216+
return DataTableResponse::success($results, ['disallow_ordering_by' => $disallowOrderingBy])->json();
160217
}
161218
}

0 commit comments

Comments
 (0)