3
3
namespace Tobyz \JsonApiServer \Laravel \Filter ;
4
4
5
5
use Tobyz \JsonApiServer \Context ;
6
- use Tobyz \JsonApiServer \Exception \BadRequestException ;
7
6
use Tobyz \JsonApiServer \Schema \Filter ;
8
7
9
8
class WhereBelongsTo extends Filter
10
9
{
10
+ use SupportsOperators;
11
+
12
+ public const SUPPORTED_OPERATORS = ['eq ' , 'in ' , 'ne ' , 'notin ' , 'null ' , 'notnull ' ];
13
+
11
14
protected ?string $ relationship = null ;
12
15
13
16
public static function make (string $ name ): static
@@ -25,16 +28,12 @@ public function relationship(?string $relationship): static
25
28
public function apply (object $ query , array |string $ value , Context $ context ): void
26
29
{
27
30
$ relationship = $ query ->getModel ()->{$ this ->relationship ?: $ this ->name }();
31
+ $ column = $ relationship ->getQualifiedForeignKeyName ();
28
32
29
- if (!array_is_list ($ values = (array ) $ value )) {
30
- throw (new BadRequestException ('filter value must be list ' ))->setSource ([
31
- 'parameter ' => "[ $ this ->name ] " ,
32
- ]);
33
- }
34
-
35
- $ query ->whereIn (
36
- $ relationship ->getQualifiedForeignKeyName (),
37
- array_merge (...array_map (fn ($ v ) => explode (', ' , $ v ), $ values )),
38
- );
33
+ Where::make ($ this ->name )
34
+ ->column ($ column )
35
+ ->operators ($ this ->operators )
36
+ ->commaSeparated ()
37
+ ->apply ($ query , $ value , $ context );
39
38
}
40
39
}
0 commit comments