Skip to content

Commit 4a22088

Browse files
committed
Add Context::sortRequested() method to determine if a sort field has been requested
1 parent a9a8bb8 commit 4a22088

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/Context.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public function getPath(): string
6060
);
6161
}
6262

63+
/**
64+
* Get the parsed JSON:API payload.
65+
*/
66+
public function getBody(): ?array
67+
{
68+
return $this->request->getParsedBody() ?: json_decode($this->request->getBody()->getContents(), true);
69+
}
70+
6371
public function response(callable $callback): void
6472
{
6573
$this->listeners['response'][] = $callback;
@@ -80,18 +88,26 @@ public function fieldRequested(string $type, string $field, bool $default = true
8088
}
8189

8290
/**
83-
* Get the value of a filter.
91+
* Determine whether a sort field has been requested.
8492
*/
85-
public function filter(string $name): ?string
93+
public function sortRequested(string $field): bool
8694
{
87-
return $this->request->getQueryParams()['filter'][$name] ?? null;
95+
if ($sortString = $this->getRequest()->getQueryParams()['sort'] ?? null) {
96+
foreach (parse_sort_string($sortString) as [$name, $direction]) {
97+
if ($name === $field) {
98+
return true;
99+
}
100+
}
101+
}
102+
103+
return false;
88104
}
89105

90106
/**
91-
* Get parsed JsonApi payload
107+
* Get the value of a filter.
92108
*/
93-
public function getBody(): ?array
109+
public function filter(string $name): ?string
94110
{
95-
return $this->request->getParsedBody() ?: json_decode($this->request->getBody()->getContents(), true);
111+
return $this->request->getQueryParams()['filter'][$name] ?? null;
96112
}
97113
}

src/ResourceType.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function applySort($query, string $sortString, Context $context): void
8484
$customSorts = $schema->getSorts();
8585
$fields = $schema->getFields();
8686

87-
foreach ($this->parseSortString($sortString) as [$name, $direction]) {
87+
foreach (parse_sort_string($sortString) as [$name, $direction]) {
8888
if (
8989
isset($customSorts[$name])
9090
&& evaluate($customSorts[$name]->getVisible(), [$context])
@@ -107,17 +107,6 @@ public function applySort($query, string $sortString, Context $context): void
107107
}
108108
}
109109

110-
private function parseSortString(string $string): array
111-
{
112-
return array_map(function ($field) {
113-
if ($field[0] === '-') {
114-
return [substr($field, 1), 'desc'];
115-
} else {
116-
return [$field, 'asc'];
117-
}
118-
}, explode(',', $string));
119-
}
120-
121110
/**
122111
* Apply the resource type's filters to a query.
123112
*/

src/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,14 @@ function set_value(array &$data, Field $field, $value): void
6868
{
6969
$data[$field->getLocation()][$field->getName()] = $value;
7070
}
71+
72+
function parse_sort_string(string $string): array
73+
{
74+
return array_map(function ($field) {
75+
if ($field[0] === '-') {
76+
return [substr($field, 1), 'desc'];
77+
} else {
78+
return [$field, 'asc'];
79+
}
80+
}, explode(',', $string));
81+
}

0 commit comments

Comments
 (0)