diff --git a/src/Handler/Index.php b/src/Handler/Index.php index 3997e8e..e9be6d4 100644 --- a/src/Handler/Index.php +++ b/src/Handler/Index.php @@ -27,6 +27,7 @@ use Tobyz\JsonApiServer\Schema\Attribute; use Tobyz\JsonApiServer\Schema\HasMany; use Tobyz\JsonApiServer\Schema\HasOne; use Tobyz\JsonApiServer\Serializer; +use function Tobyz\JsonApiServer\evaluate; use function Tobyz\JsonApiServer\run_callbacks; class Index implements RequestHandlerInterface @@ -162,7 +163,7 @@ class Index implements RequestHandlerInterface if ( isset($fields[$name]) && $fields[$name] instanceof Attribute - && $fields[$name]->isSortable() + && evaluate($fields[$name]->isSortable(), [$request]) ) { $adapter->sortByAttribute($query, $fields[$name], $direction); continue; @@ -249,7 +250,7 @@ class Index implements RequestHandlerInterface continue; } - if (isset($fields[$name]) && $fields[$name]->isFilterable()) { + if (isset($fields[$name]) && evaluate($fields[$name]->isFilterable(), [$request])) { if ($fields[$name] instanceof Attribute) { $adapter->filterByAttribute($query, $fields[$name], $value); } elseif ($fields[$name] instanceof HasOne) { diff --git a/src/Schema/Attribute.php b/src/Schema/Attribute.php index 2d5c20b..3180d9f 100644 --- a/src/Schema/Attribute.php +++ b/src/Schema/Attribute.php @@ -11,6 +11,8 @@ namespace Tobyz\JsonApiServer\Schema; +use function Tobyz\JsonApiServer\negate; + final class Attribute extends Field { private $sortable = false; @@ -23,9 +25,9 @@ final class Attribute extends Field /** * Allow this attribute to be used for sorting the resource listing. */ - public function sortable() + public function sortable(callable $condition = null) { - $this->sortable = true; + $this->sortable = $condition ?: true; return $this; } @@ -33,14 +35,14 @@ final class Attribute extends Field /** * Disallow this attribute to be used for sorting the resource listing. */ - public function notSortable() + public function notSortable(callable $condition = null) { - $this->sortable = false; + $this->sortable = $condition ? negate($condition) : false; return $this; } - public function isSortable(): bool + public function isSortable() { return $this->sortable; } diff --git a/src/Schema/Field.php b/src/Schema/Field.php index 0f44136..5fdf4b1 100644 --- a/src/Schema/Field.php +++ b/src/Schema/Field.php @@ -178,9 +178,9 @@ abstract class Field /** * Allow this field to be used for filtering the resource listing. */ - public function filterable() + public function filterable(callable $condition = null) { - $this->filterable = true; + $this->filterable = $condition ?: true; return $this; } @@ -188,9 +188,9 @@ abstract class Field /** * Disallow this field to be used for filtering the resource listing. */ - public function notFilterable() + public function notFilterable(callable $condition = null) { - $this->filterable = false; + $this->filterable = $condition ? negate($condition) : false; return $this; } @@ -240,7 +240,7 @@ abstract class Field return $this->defaultCallback; } - public function isFilterable(): bool + public function isFilterable() { return $this->filterable; }