From 416a9c80b0e5b02bba573b36c799ee01a5ecd4bb Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 29 Aug 2021 15:41:31 +1000 Subject: [PATCH] Add Context::filter() method --- docs/requests.md | 3 +++ src/Context.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/docs/requests.md b/docs/requests.md index f96a96d..9cb42d4 100644 --- a/docs/requests.md +++ b/docs/requests.md @@ -60,5 +60,8 @@ This object contains a number of useful methods: * `fieldRequested(string $type, string $field, bool $default = true): bool` Determine whether a field has been requested in a [sparse fieldset](https://jsonapi.org/format/1.1/#fetching-sparse-fieldsets). +* `filter(string $name): ?string` + Get the value of a filter. + * `meta(string $name, $value): Tobyz\JsonApi\Schema\Meta` Add a meta attribute to the response document. diff --git a/src/Context.php b/src/Context.php index 001bac9..03009d1 100644 --- a/src/Context.php +++ b/src/Context.php @@ -29,11 +29,17 @@ class Context $this->request = $request; } + /** + * Get the JsonApi instance. + */ public function getApi(): JsonApi { return $this->api; } + /** + * Get the PSR-7 request instance. + */ public function getRequest(): ServerRequestInterface { return $this->request; @@ -44,6 +50,9 @@ class Context return new static($this->api, $request); } + /** + * Get the request path relative to the API's base path. + */ public function getPath(): string { return $this->api->stripBasePath( @@ -56,6 +65,9 @@ class Context $this->listeners['response'][] = $callback; } + /** + * Determine whether a field has been requested in a sparse fieldset. + */ public function fieldRequested(string $type, string $field, bool $default = true): bool { $queryParams = $this->request->getQueryParams(); @@ -66,4 +78,12 @@ class Context return in_array($field, explode(',', $queryParams['fields'][$type])); } + + /** + * Get the value of a filter. + */ + public function filter(string $name): ?string + { + return $this->request->getQueryParams()['filter'][$name] ?? null; + } }