Add visibility to filters
This commit is contained in:
parent
f9bba44336
commit
5d69048ef8
|
|
@ -21,6 +21,8 @@ GET /users?filter[postCount]=>=10
|
|||
GET /users?filter[postCount]=5..15
|
||||
```
|
||||
|
||||
## Custom Filters
|
||||
|
||||
To define filters with custom logic, or ones that do not correspond to an attribute, use the `filter` method:
|
||||
|
||||
```php
|
||||
|
|
@ -28,3 +30,12 @@ $type->filter('minPosts', function ($query, $value, Context $context) {
|
|||
$query->where('postCount', '>=', $value);
|
||||
});
|
||||
```
|
||||
|
||||
Just like [fields](visibility.md), filters can be made conditionally `visible` or `hidden`:
|
||||
|
||||
```php
|
||||
$type->filter('email', $callback)
|
||||
->visible(function (Context $context) {
|
||||
return $context->getRequest()->getAttribute('isAdmin');
|
||||
});
|
||||
```
|
||||
|
|
|
|||
|
|
@ -244,7 +244,7 @@ class Index
|
|||
continue;
|
||||
}
|
||||
|
||||
if (isset($filters[$name])) {
|
||||
if (isset($filters[$name]) && evaluate($filters[$name]->getVisible(), [$this->context])) {
|
||||
$filters[$name]->getCallback()($query, $value, $context);
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of tobyz/json-api-server.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Tobyz\JsonApiServer\Schema\Concerns;
|
||||
|
||||
use function Tobyz\JsonApiServer\negate;
|
||||
|
||||
trait HasVisibility
|
||||
{
|
||||
private $visible = true;
|
||||
|
||||
/**
|
||||
* Allow this field to be seen.
|
||||
*/
|
||||
public function visible(callable $condition = null)
|
||||
{
|
||||
$this->visible = $condition ?: true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disallow this field to be seen.
|
||||
*/
|
||||
public function hidden(callable $condition = null)
|
||||
{
|
||||
$this->visible = $condition ? negate($condition) : false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getVisible()
|
||||
{
|
||||
return $this->visible;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ namespace Tobyz\JsonApiServer\Schema;
|
|||
|
||||
use Tobyz\JsonApiServer\Schema\Concerns\HasDescription;
|
||||
use Tobyz\JsonApiServer\Schema\Concerns\HasListeners;
|
||||
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;
|
||||
use function Tobyz\JsonApiServer\negate;
|
||||
use function Tobyz\JsonApiServer\wrap;
|
||||
|
||||
|
|
@ -20,11 +21,10 @@ abstract class Field
|
|||
{
|
||||
use HasListeners;
|
||||
use HasDescription;
|
||||
use HasVisibility;
|
||||
|
||||
private $name;
|
||||
private $property;
|
||||
private $visible = true;
|
||||
private $single = false;
|
||||
private $writable = false;
|
||||
private $writableOnce = false;
|
||||
private $getCallback;
|
||||
|
|
@ -54,26 +54,6 @@ abstract class Field
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow this field to be seen.
|
||||
*/
|
||||
public function visible(callable $condition = null)
|
||||
{
|
||||
$this->visible = $condition ?: true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disallow this field to be seen.
|
||||
*/
|
||||
public function hidden(callable $condition = null)
|
||||
{
|
||||
$this->visible = $condition ? negate($condition) : false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow this field to be written.
|
||||
*/
|
||||
|
|
@ -214,11 +194,6 @@ abstract class Field
|
|||
return $this->property;
|
||||
}
|
||||
|
||||
public function getVisible()
|
||||
{
|
||||
return $this->visible;
|
||||
}
|
||||
|
||||
public function getWritable()
|
||||
{
|
||||
return $this->writable;
|
||||
|
|
|
|||
|
|
@ -12,10 +12,12 @@
|
|||
namespace Tobyz\JsonApiServer\Schema;
|
||||
|
||||
use Tobyz\JsonApiServer\Schema\Concerns\HasDescription;
|
||||
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;
|
||||
|
||||
final class Filter
|
||||
{
|
||||
use HasDescription;
|
||||
use HasVisibility;
|
||||
|
||||
private $name;
|
||||
private $callback;
|
||||
|
|
|
|||
Loading…
Reference in New Issue