Add `Context::sortRequested()` method to determine if a sort field has been requested
This commit is contained in:
parent
a9a8bb84aa
commit
4a22088559
|
|
@ -60,6 +60,14 @@ class Context
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the parsed JSON:API payload.
|
||||||
|
*/
|
||||||
|
public function getBody(): ?array
|
||||||
|
{
|
||||||
|
return $this->request->getParsedBody() ?: json_decode($this->request->getBody()->getContents(), true);
|
||||||
|
}
|
||||||
|
|
||||||
public function response(callable $callback): void
|
public function response(callable $callback): void
|
||||||
{
|
{
|
||||||
$this->listeners['response'][] = $callback;
|
$this->listeners['response'][] = $callback;
|
||||||
|
|
@ -79,6 +87,22 @@ class Context
|
||||||
return in_array($field, explode(',', $queryParams['fields'][$type]));
|
return in_array($field, explode(',', $queryParams['fields'][$type]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether a sort field has been requested.
|
||||||
|
*/
|
||||||
|
public function sortRequested(string $field): bool
|
||||||
|
{
|
||||||
|
if ($sortString = $this->getRequest()->getQueryParams()['sort'] ?? null) {
|
||||||
|
foreach (parse_sort_string($sortString) as [$name, $direction]) {
|
||||||
|
if ($name === $field) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value of a filter.
|
* Get the value of a filter.
|
||||||
*/
|
*/
|
||||||
|
|
@ -86,12 +110,4 @@ class Context
|
||||||
{
|
{
|
||||||
return $this->request->getQueryParams()['filter'][$name] ?? null;
|
return $this->request->getQueryParams()['filter'][$name] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get parsed JsonApi payload
|
|
||||||
*/
|
|
||||||
public function getBody(): ?array
|
|
||||||
{
|
|
||||||
return $this->request->getParsedBody() ?: json_decode($this->request->getBody()->getContents(), true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ final class ResourceType
|
||||||
$customSorts = $schema->getSorts();
|
$customSorts = $schema->getSorts();
|
||||||
$fields = $schema->getFields();
|
$fields = $schema->getFields();
|
||||||
|
|
||||||
foreach ($this->parseSortString($sortString) as [$name, $direction]) {
|
foreach (parse_sort_string($sortString) as [$name, $direction]) {
|
||||||
if (
|
if (
|
||||||
isset($customSorts[$name])
|
isset($customSorts[$name])
|
||||||
&& evaluate($customSorts[$name]->getVisible(), [$context])
|
&& evaluate($customSorts[$name]->getVisible(), [$context])
|
||||||
|
|
@ -107,17 +107,6 @@ final class ResourceType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parseSortString(string $string): array
|
|
||||||
{
|
|
||||||
return array_map(function ($field) {
|
|
||||||
if ($field[0] === '-') {
|
|
||||||
return [substr($field, 1), 'desc'];
|
|
||||||
} else {
|
|
||||||
return [$field, 'asc'];
|
|
||||||
}
|
|
||||||
}, explode(',', $string));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply the resource type's filters to a query.
|
* Apply the resource type's filters to a query.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -68,3 +68,14 @@ function set_value(array &$data, Field $field, $value): void
|
||||||
{
|
{
|
||||||
$data[$field->getLocation()][$field->getName()] = $value;
|
$data[$field->getLocation()][$field->getName()] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parse_sort_string(string $string): array
|
||||||
|
{
|
||||||
|
return array_map(function ($field) {
|
||||||
|
if ($field[0] === '-') {
|
||||||
|
return [substr($field, 1), 'desc'];
|
||||||
|
} else {
|
||||||
|
return [$field, 'asc'];
|
||||||
|
}
|
||||||
|
}, explode(',', $string));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue