File tree Expand file tree Collapse file tree 3 files changed +15
-12
lines changed Expand file tree Collapse file tree 3 files changed +15
-12
lines changed Original file line number Diff line number Diff line change 27
27
use Tobyz \JsonApiServer \Schema \HasMany ;
28
28
use Tobyz \JsonApiServer \Schema \HasOne ;
29
29
use Tobyz \JsonApiServer \Serializer ;
30
+ use function Tobyz \JsonApiServer \evaluate ;
30
31
use function Tobyz \JsonApiServer \run_callbacks ;
31
32
32
33
class Index implements RequestHandlerInterface
@@ -162,7 +163,7 @@ private function sort($query, Request $request)
162
163
if (
163
164
isset ($ fields [$ name ])
164
165
&& $ fields [$ name ] instanceof Attribute
165
- && $ fields [$ name ]->isSortable ()
166
+ && evaluate ( $ fields [$ name ]->isSortable (), [ $ request ] )
166
167
) {
167
168
$ adapter ->sortByAttribute ($ query , $ fields [$ name ], $ direction );
168
169
continue ;
@@ -249,7 +250,7 @@ private function filter($query, Request $request)
249
250
continue ;
250
251
}
251
252
252
- if (isset ($ fields [$ name ]) && $ fields [$ name ]->isFilterable ()) {
253
+ if (isset ($ fields [$ name ]) && evaluate ( $ fields [$ name ]->isFilterable (), [ $ request ] )) {
253
254
if ($ fields [$ name ] instanceof Attribute) {
254
255
$ adapter ->filterByAttribute ($ query , $ fields [$ name ], $ value );
255
256
} elseif ($ fields [$ name ] instanceof HasOne) {
Original file line number Diff line number Diff line change 11
11
12
12
namespace Tobyz \JsonApiServer \Schema ;
13
13
14
+ use function Tobyz \JsonApiServer \negate ;
15
+
14
16
final class Attribute extends Field
15
17
{
16
18
private $ sortable = false ;
@@ -23,24 +25,24 @@ public function getLocation(): string
23
25
/**
24
26
* Allow this attribute to be used for sorting the resource listing.
25
27
*/
26
- public function sortable ()
28
+ public function sortable (callable $ condition = null )
27
29
{
28
- $ this ->sortable = true ;
30
+ $ this ->sortable = $ condition ?: true ;
29
31
30
32
return $ this ;
31
33
}
32
34
33
35
/**
34
36
* Disallow this attribute to be used for sorting the resource listing.
35
37
*/
36
- public function notSortable ()
38
+ public function notSortable (callable $ condition = null )
37
39
{
38
- $ this ->sortable = false ;
40
+ $ this ->sortable = $ condition ? negate ( $ condition ) : false ;
39
41
40
42
return $ this ;
41
43
}
42
44
43
- public function isSortable (): bool
45
+ public function isSortable ()
44
46
{
45
47
return $ this ->sortable ;
46
48
}
Original file line number Diff line number Diff line change @@ -178,19 +178,19 @@ public function validate(callable $callback)
178
178
/**
179
179
* Allow this field to be used for filtering the resource listing.
180
180
*/
181
- public function filterable ()
181
+ public function filterable (callable $ condition = null )
182
182
{
183
- $ this ->filterable = true ;
183
+ $ this ->filterable = $ condition ?: true ;
184
184
185
185
return $ this ;
186
186
}
187
187
188
188
/**
189
189
* Disallow this field to be used for filtering the resource listing.
190
190
*/
191
- public function notFilterable ()
191
+ public function notFilterable (callable $ condition = null )
192
192
{
193
- $ this ->filterable = false ;
193
+ $ this ->filterable = $ condition ? negate ( $ condition ) : false ;
194
194
195
195
return $ this ;
196
196
}
@@ -240,7 +240,7 @@ public function getDefaultCallback()
240
240
return $ this ->defaultCallback ;
241
241
}
242
242
243
- public function isFilterable (): bool
243
+ public function isFilterable ()
244
244
{
245
245
return $ this ->filterable ;
246
246
}
You can’t perform that action at this time.
0 commit comments