diff --git a/docs/filtering.md b/docs/filtering.md index cc76020..0d57bf7 100644 --- a/docs/filtering.md +++ b/docs/filtering.md @@ -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'); + }); +``` diff --git a/src/Endpoint/Index.php b/src/Endpoint/Index.php index 2dfe896..ba277be 100644 --- a/src/Endpoint/Index.php +++ b/src/Endpoint/Index.php @@ -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; } diff --git a/src/Schema/Concerns/HasVisibility.php b/src/Schema/Concerns/HasVisibility.php new file mode 100644 index 0000000..dd62122 --- /dev/null +++ b/src/Schema/Concerns/HasVisibility.php @@ -0,0 +1,44 @@ + + * + * 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; + } +} diff --git a/src/Schema/Field.php b/src/Schema/Field.php index 996fff7..90cb853 100644 --- a/src/Schema/Field.php +++ b/src/Schema/Field.php @@ -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; diff --git a/src/Schema/Filter.php b/src/Schema/Filter.php index 80ac863..c2b818f 100644 --- a/src/Schema/Filter.php +++ b/src/Schema/Filter.php @@ -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;