Allow condition to be passed to sortable/filterable
This commit is contained in:
parent
fab1a86925
commit
295d2beafd
|
|
@ -27,6 +27,7 @@ use Tobyz\JsonApiServer\Schema\Attribute;
|
||||||
use Tobyz\JsonApiServer\Schema\HasMany;
|
use Tobyz\JsonApiServer\Schema\HasMany;
|
||||||
use Tobyz\JsonApiServer\Schema\HasOne;
|
use Tobyz\JsonApiServer\Schema\HasOne;
|
||||||
use Tobyz\JsonApiServer\Serializer;
|
use Tobyz\JsonApiServer\Serializer;
|
||||||
|
use function Tobyz\JsonApiServer\evaluate;
|
||||||
use function Tobyz\JsonApiServer\run_callbacks;
|
use function Tobyz\JsonApiServer\run_callbacks;
|
||||||
|
|
||||||
class Index implements RequestHandlerInterface
|
class Index implements RequestHandlerInterface
|
||||||
|
|
@ -162,7 +163,7 @@ class Index implements RequestHandlerInterface
|
||||||
if (
|
if (
|
||||||
isset($fields[$name])
|
isset($fields[$name])
|
||||||
&& $fields[$name] instanceof Attribute
|
&& $fields[$name] instanceof Attribute
|
||||||
&& $fields[$name]->isSortable()
|
&& evaluate($fields[$name]->isSortable(), [$request])
|
||||||
) {
|
) {
|
||||||
$adapter->sortByAttribute($query, $fields[$name], $direction);
|
$adapter->sortByAttribute($query, $fields[$name], $direction);
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -249,7 +250,7 @@ class Index implements RequestHandlerInterface
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($fields[$name]) && $fields[$name]->isFilterable()) {
|
if (isset($fields[$name]) && evaluate($fields[$name]->isFilterable(), [$request])) {
|
||||||
if ($fields[$name] instanceof Attribute) {
|
if ($fields[$name] instanceof Attribute) {
|
||||||
$adapter->filterByAttribute($query, $fields[$name], $value);
|
$adapter->filterByAttribute($query, $fields[$name], $value);
|
||||||
} elseif ($fields[$name] instanceof HasOne) {
|
} elseif ($fields[$name] instanceof HasOne) {
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
namespace Tobyz\JsonApiServer\Schema;
|
namespace Tobyz\JsonApiServer\Schema;
|
||||||
|
|
||||||
|
use function Tobyz\JsonApiServer\negate;
|
||||||
|
|
||||||
final class Attribute extends Field
|
final class Attribute extends Field
|
||||||
{
|
{
|
||||||
private $sortable = false;
|
private $sortable = false;
|
||||||
|
|
@ -23,9 +25,9 @@ final class Attribute extends Field
|
||||||
/**
|
/**
|
||||||
* Allow this attribute to be used for sorting the resource listing.
|
* 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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -33,14 +35,14 @@ final class Attribute extends Field
|
||||||
/**
|
/**
|
||||||
* Disallow this attribute to be used for sorting the resource listing.
|
* 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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isSortable(): bool
|
public function isSortable()
|
||||||
{
|
{
|
||||||
return $this->sortable;
|
return $this->sortable;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,9 @@ abstract class Field
|
||||||
/**
|
/**
|
||||||
* Allow this field to be used for filtering the resource listing.
|
* 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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -188,9 +188,9 @@ abstract class Field
|
||||||
/**
|
/**
|
||||||
* Disallow this field to be used for filtering the resource listing.
|
* 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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -240,7 +240,7 @@ abstract class Field
|
||||||
return $this->defaultCallback;
|
return $this->defaultCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isFilterable(): bool
|
public function isFilterable()
|
||||||
{
|
{
|
||||||
return $this->filterable;
|
return $this->filterable;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue