Skip to content

Commit 295d2be

Browse files
committed
Allow condition to be passed to sortable/filterable
1 parent fab1a86 commit 295d2be

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/Handler/Index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Tobyz\JsonApiServer\Schema\HasMany;
2828
use Tobyz\JsonApiServer\Schema\HasOne;
2929
use Tobyz\JsonApiServer\Serializer;
30+
use function Tobyz\JsonApiServer\evaluate;
3031
use function Tobyz\JsonApiServer\run_callbacks;
3132

3233
class Index implements RequestHandlerInterface
@@ -162,7 +163,7 @@ private function sort($query, Request $request)
162163
if (
163164
isset($fields[$name])
164165
&& $fields[$name] instanceof Attribute
165-
&& $fields[$name]->isSortable()
166+
&& evaluate($fields[$name]->isSortable(), [$request])
166167
) {
167168
$adapter->sortByAttribute($query, $fields[$name], $direction);
168169
continue;
@@ -249,7 +250,7 @@ private function filter($query, Request $request)
249250
continue;
250251
}
251252

252-
if (isset($fields[$name]) && $fields[$name]->isFilterable()) {
253+
if (isset($fields[$name]) && evaluate($fields[$name]->isFilterable(), [$request])) {
253254
if ($fields[$name] instanceof Attribute) {
254255
$adapter->filterByAttribute($query, $fields[$name], $value);
255256
} elseif ($fields[$name] instanceof HasOne) {

src/Schema/Attribute.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Tobyz\JsonApiServer\Schema;
1313

14+
use function Tobyz\JsonApiServer\negate;
15+
1416
final class Attribute extends Field
1517
{
1618
private $sortable = false;
@@ -23,24 +25,24 @@ public function getLocation(): string
2325
/**
2426
* Allow this attribute to be used for sorting the resource listing.
2527
*/
26-
public function sortable()
28+
public function sortable(callable $condition = null)
2729
{
28-
$this->sortable = true;
30+
$this->sortable = $condition ?: true;
2931

3032
return $this;
3133
}
3234

3335
/**
3436
* Disallow this attribute to be used for sorting the resource listing.
3537
*/
36-
public function notSortable()
38+
public function notSortable(callable $condition = null)
3739
{
38-
$this->sortable = false;
40+
$this->sortable = $condition ? negate($condition) : false;
3941

4042
return $this;
4143
}
4244

43-
public function isSortable(): bool
45+
public function isSortable()
4446
{
4547
return $this->sortable;
4648
}

src/Schema/Field.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,19 @@ public function validate(callable $callback)
178178
/**
179179
* Allow this field to be used for filtering the resource listing.
180180
*/
181-
public function filterable()
181+
public function filterable(callable $condition = null)
182182
{
183-
$this->filterable = true;
183+
$this->filterable = $condition ?: true;
184184

185185
return $this;
186186
}
187187

188188
/**
189189
* Disallow this field to be used for filtering the resource listing.
190190
*/
191-
public function notFilterable()
191+
public function notFilterable(callable $condition = null)
192192
{
193-
$this->filterable = false;
193+
$this->filterable = $condition ? negate($condition) : false;
194194

195195
return $this;
196196
}
@@ -240,7 +240,7 @@ public function getDefaultCallback()
240240
return $this->defaultCallback;
241241
}
242242

243-
public function isFilterable(): bool
243+
public function isFilterable()
244244
{
245245
return $this->filterable;
246246
}

0 commit comments

Comments
 (0)