Skip to content

Commit 416a9c8

Browse files
committed
Add Context::filter() method
1 parent 7d7dcb3 commit 416a9c8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/requests.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ This object contains a number of useful methods:
6060
* `fieldRequested(string $type, string $field, bool $default = true): bool`
6161
Determine whether a field has been requested in a [sparse fieldset](https://jsonapi.org/format/1.1/#fetching-sparse-fieldsets).
6262

63+
* `filter(string $name): ?string`
64+
Get the value of a filter.
65+
6366
* `meta(string $name, $value): Tobyz\JsonApi\Schema\Meta`
6467
Add a meta attribute to the response document.

src/Context.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ public function __construct(JsonApi $api, ServerRequestInterface $request)
2929
$this->request = $request;
3030
}
3131

32+
/**
33+
* Get the JsonApi instance.
34+
*/
3235
public function getApi(): JsonApi
3336
{
3437
return $this->api;
3538
}
3639

40+
/**
41+
* Get the PSR-7 request instance.
42+
*/
3743
public function getRequest(): ServerRequestInterface
3844
{
3945
return $this->request;
@@ -44,6 +50,9 @@ public function withRequest(ServerRequestInterface $request): Context
4450
return new static($this->api, $request);
4551
}
4652

53+
/**
54+
* Get the request path relative to the API's base path.
55+
*/
4756
public function getPath(): string
4857
{
4958
return $this->api->stripBasePath(
@@ -56,6 +65,9 @@ public function response(callable $callback): void
5665
$this->listeners['response'][] = $callback;
5766
}
5867

68+
/**
69+
* Determine whether a field has been requested in a sparse fieldset.
70+
*/
5971
public function fieldRequested(string $type, string $field, bool $default = true): bool
6072
{
6173
$queryParams = $this->request->getQueryParams();
@@ -66,4 +78,12 @@ public function fieldRequested(string $type, string $field, bool $default = true
6678

6779
return in_array($field, explode(',', $queryParams['fields'][$type]));
6880
}
81+
82+
/**
83+
* Get the value of a filter.
84+
*/
85+
public function filter(string $name): ?string
86+
{
87+
return $this->request->getQueryParams()['filter'][$name] ?? null;
88+
}
6989
}

0 commit comments

Comments
 (0)