Skip to content

Commit 5d69048

Browse files
committed
Add visibility to filters
1 parent f9bba44 commit 5d69048

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

docs/filtering.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ GET /users?filter[postCount]=>=10
2121
GET /users?filter[postCount]=5..15
2222
```
2323

24+
## Custom Filters
25+
2426
To define filters with custom logic, or ones that do not correspond to an attribute, use the `filter` method:
2527

2628
```php
2729
$type->filter('minPosts', function ($query, $value, Context $context) {
2830
$query->where('postCount', '>=', $value);
2931
});
3032
```
33+
34+
Just like [fields](visibility.md), filters can be made conditionally `visible` or `hidden`:
35+
36+
```php
37+
$type->filter('email', $callback)
38+
->visible(function (Context $context) {
39+
return $context->getRequest()->getAttribute('isAdmin');
40+
});
41+
```

src/Endpoint/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function filter($query, Context $context)
244244
continue;
245245
}
246246

247-
if (isset($filters[$name])) {
247+
if (isset($filters[$name]) && evaluate($filters[$name]->getVisible(), [$this->context])) {
248248
$filters[$name]->getCallback()($query, $value, $context);
249249
continue;
250250
}

src/Schema/Concerns/HasVisibility.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tobyz\JsonApiServer\Schema\Concerns;
13+
14+
use function Tobyz\JsonApiServer\negate;
15+
16+
trait HasVisibility
17+
{
18+
private $visible = true;
19+
20+
/**
21+
* Allow this field to be seen.
22+
*/
23+
public function visible(callable $condition = null)
24+
{
25+
$this->visible = $condition ?: true;
26+
27+
return $this;
28+
}
29+
30+
/**
31+
* Disallow this field to be seen.
32+
*/
33+
public function hidden(callable $condition = null)
34+
{
35+
$this->visible = $condition ? negate($condition) : false;
36+
37+
return $this;
38+
}
39+
40+
public function getVisible()
41+
{
42+
return $this->visible;
43+
}
44+
}

src/Schema/Field.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313

1414
use Tobyz\JsonApiServer\Schema\Concerns\HasDescription;
1515
use Tobyz\JsonApiServer\Schema\Concerns\HasListeners;
16+
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;
1617
use function Tobyz\JsonApiServer\negate;
1718
use function Tobyz\JsonApiServer\wrap;
1819

1920
abstract class Field
2021
{
2122
use HasListeners;
2223
use HasDescription;
24+
use HasVisibility;
2325

2426
private $name;
2527
private $property;
26-
private $visible = true;
27-
private $single = false;
2828
private $writable = false;
2929
private $writableOnce = false;
3030
private $getCallback;
@@ -54,26 +54,6 @@ public function property(string $property)
5454
return $this;
5555
}
5656

57-
/**
58-
* Allow this field to be seen.
59-
*/
60-
public function visible(callable $condition = null)
61-
{
62-
$this->visible = $condition ?: true;
63-
64-
return $this;
65-
}
66-
67-
/**
68-
* Disallow this field to be seen.
69-
*/
70-
public function hidden(callable $condition = null)
71-
{
72-
$this->visible = $condition ? negate($condition) : false;
73-
74-
return $this;
75-
}
76-
7757
/**
7858
* Allow this field to be written.
7959
*/
@@ -214,11 +194,6 @@ public function getProperty(): ?string
214194
return $this->property;
215195
}
216196

217-
public function getVisible()
218-
{
219-
return $this->visible;
220-
}
221-
222197
public function getWritable()
223198
{
224199
return $this->writable;

src/Schema/Filter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Tobyz\JsonApiServer\Schema;
1313

1414
use Tobyz\JsonApiServer\Schema\Concerns\HasDescription;
15+
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;
1516

1617
final class Filter
1718
{
1819
use HasDescription;
20+
use HasVisibility;
1921

2022
private $name;
2123
private $callback;

0 commit comments

Comments
 (0)