File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -58,5 +58,11 @@ class Context
58
58
59
59
// Get only the requested fields for the given resource
60
60
public function sparseFields(ResourceInterface $resource): array;
61
+
62
+ // Determine whether a field has been requested in a sparse fieldset
63
+ public function fieldRequested(string $type, string $field, bool $default = true): bool;
64
+
65
+ // Determine whether a sort field has been requested
66
+ public function sortRequested(string $field): bool;
61
67
}
62
68
```
Original file line number Diff line number Diff line change @@ -96,7 +96,9 @@ public function fields(ResourceInterface $resource): array
96
96
}
97
97
98
98
/**
99
+ * Get only the requested fields for the given resource, keyed by name.
99
100
*
101
+ * @return array<string, Field>
100
102
*/
101
103
public function sparseFields (ResourceInterface $ resource ): array
102
104
{
@@ -115,6 +117,34 @@ public function sparseFields(ResourceInterface $resource): array
115
117
return $ this ->sparseFields [$ resource ] = $ fields ;
116
118
}
117
119
120
+ /**
121
+ * Determine whether a field has been requested in a sparse fieldset.
122
+ */
123
+ public function fieldRequested (string $ type , string $ field , bool $ default = true ): bool
124
+ {
125
+ if ($ requested = $ this ->queryParam ('fields ' )[$ type ] ?? null ) {
126
+ return in_array ($ field , explode (', ' , $ requested ));
127
+ }
128
+
129
+ return $ default ;
130
+ }
131
+
132
+ /**
133
+ * Determine whether a sort field has been requested.
134
+ */
135
+ public function sortRequested (string $ field ): bool
136
+ {
137
+ if ($ sort = $ this ->queryParam ('sort ' )) {
138
+ foreach (parse_sort_string ($ sort ) as [$ name , $ direction ]) {
139
+ if ($ name === $ field ) {
140
+ return true ;
141
+ }
142
+ }
143
+ }
144
+
145
+ return false ;
146
+ }
147
+
118
148
public function withRequest (ServerRequestInterface $ request ): static
119
149
{
120
150
$ new = clone $ this ;
You can’t perform that action at this time.
0 commit comments